Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook for subresource integrity #40

Closed
jscheid opened this issue Mar 16, 2018 · 7 comments
Closed

Hook for subresource integrity #40

jscheid opened this issue Mar 16, 2018 · 7 comments

Comments

@jscheid
Copy link

jscheid commented Mar 16, 2018

Hi, I'm maintaining webpack-subresource-integrity and I'd like to help make the two plugins work together, to ensure integrity for injected link rel="stylesheet" tags.

For this there would need to be a way for our plugin to add attributes (integrity and crossorigin) somewhere around here.

One approach could be a hook that receives an object with the tag name and all attributes. rel and href would be needed by our plugin, but I guess for completeness it should also include onload and onerror. It would return an object with the same structure. The code would then look something like this:

let tag = {
    tagName: 'link',
    attributes: {
        rel: '"stylesheet"',
        onload: 'resolve',
        onerror: 'reject',
        href: `${mainTemplate.requireFn}.p + ${linkHrefPath}`,
    }
};

// up for debate where this hook would be declared, what name to use,
// how to handle backward compatibility etc.
if (compilation.hooks.mutateInjectedTag) {
    tag = compilation.hooks.mutateInjectedTag.call(tag);
}

Template.indent([
    `var el = document.createElement(${JSON.stringify(tag.tagName)})`
].concat(Object.entries(tag.attributes).map(([name, value]) => (
    `el[${JSON.stringify(name)}] = ${value}`
))).concat([
    "...",
]);

This aims to be generic, so that it could be used for other plugins that inject tags or that want to modify injected tags.

It's just a suggestion of course, there are other approaches that would also work. But first, what do you think of the general idea, would you accept a PR for this?

@alexander-akait
Copy link
Member

@jscheid PR welcome 👍

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Mar 20, 2018

At best this would be usable as generic hook to add code to the JS side of affairs (e.g for HMR aswell)

@josephliccini
Copy link

This feature would be great, is it still on the roadmap for this plugin?

@jahed
Copy link
Contributor

jahed commented Jul 8, 2019

It's been a while so just wanted to know, is the approach still agreed upon? i.e. using compilation.hooks.mutateInjectedTag?

@ScriptedAlchemy
Copy link
Contributor

I wouldn't mind seeing this implemented. If i have the time ill look at doing so

patriksletmo pushed a commit to patriksletmo/mini-css-extract-plugin that referenced this issue Apr 24, 2020
Enable other plugins to tap into the tag injection mechanism in order to
customize the functionality of lazy loaded chunks.

Closes webpack-contrib#40.
@patriksletmo
Copy link

patriksletmo commented Apr 24, 2020

I discovered a need of this functionality in one of my projects, so I decided to open a PR with an implementation (#526). Along this PR I have also prepared changes to webpack-subresource-integrity that makes use of this new functionality to implement the support @jscheid referenced in the original post.

Feel free to submit feedback on the PR, I am open for suggestions!

patriksletmo pushed a commit to patriksletmo/mini-css-extract-plugin that referenced this issue Apr 12, 2021
    Enable other plugins to tap into the tag injection mechanism in order to
    customize the functionality of lazy loaded chunks.

    Closes webpack-contrib#40.
patriksletmo pushed a commit to patriksletmo/mini-css-extract-plugin that referenced this issue Apr 14, 2021
    Enable other plugins to tap into the tag injection mechanism in order to
    customize the functionality of lazy loaded chunks.

    Closes webpack-contrib#40.
@alexander-akait
Copy link
Member

Fixed by #1054, it is more simple solution that allow to insert anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants