Skip to content

Commit

Permalink
Add integrity to link preload tags
Browse files Browse the repository at this point in the history
Closes #111
  • Loading branch information
jscheid committed Aug 30, 2019
1 parent 9f448b7 commit 0863fde
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/webpack-preload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# With `/* webpackPreload: "true" */`

https://github.com/waysact/webpack-subresource-integrity/issues/111
1 change: 1 addition & 0 deletions examples/webpack-preload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('./lazy-chunk-1.js');
5 changes: 5 additions & 0 deletions examples/webpack-preload/lazy-chunk-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
setTimeout(() => {
import(/* webpackPreload: true */ './lazy-chunk-2.js').then(mod =>
mod.test()
);
}, 750);
16 changes: 16 additions & 0 deletions examples/webpack-preload/lazy-chunk-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports.test = () => {
const linkTag = Array.from(document.getElementsByTagName('link')).find(
el => el.rel === 'preload'
);
const scriptTag = Array.from(document.getElementsByTagName('script')).find(
el => linkTag && el.src === linkTag.href
);
console.log(
scriptTag &&
linkTag &&
scriptTag.integrity &&
scriptTag.integrity === linkTag.integrity
? 'ok'
: 'error'
);
};
8 changes: 8 additions & 0 deletions examples/webpack-preload/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// webpack-preload tag requires Webpack 4
return webpackVersion < 4;
};
18 changes: 18 additions & 0 deletions examples/webpack-preload/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var SriPlugin = require('webpack-subresource-integrity');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: {
index: './index.js'
},
output: {
crossOriginLoading: 'anonymous'
},
plugins: [
new SriPlugin({
hashFuncNames: ['sha256', 'sha384'],
enabled: true
}),
new HtmlWebpackPlugin()
]
};
20 changes: 20 additions & 0 deletions jmtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
return source;
};

/*
* Patch jsonp-script code to add the integrity attribute.
*/
WebIntegrityJsonpMainTemplatePlugin.prototype.linkPreloadPlugin =
function linkPreloadPlugin(mainTemplate, source) {
if (!mainTemplate.outputOptions.crossOriginLoading) {
this.sriPlugin.errorOnce(
this.compilation,
'webpack option output.crossOriginLoading not set, code splitting will not work!'
);
}
return (Template.asString || mainTemplate.asString)([
source,
'link.integrity = sriHashes[chunkId];',
'link.crossOrigin = ' + JSON.stringify(mainTemplate.outputOptions.crossOriginLoading) + ';',
]);
};

/*
* Patch jsonp-script code to add the integrity attribute.
*/
Expand All @@ -65,13 +83,15 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(
mainTemplate
) {
var jsonpScriptPlugin = this.jsonpScriptPlugin.bind(this, mainTemplate);
var linkPreloadPlugin = this.linkPreloadPlugin.bind(this, mainTemplate);
var addSriHashes = this.addSriHashes.bind(this, mainTemplate);

if (!mainTemplate.hooks) {
mainTemplate.plugin('jsonp-script', jsonpScriptPlugin);
mainTemplate.plugin('local-vars', addSriHashes);
} else if (mainTemplate.hooks.jsonpScript && mainTemplate.hooks.localVars) {
mainTemplate.hooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin);
mainTemplate.hooks.linkPreload.tap('SriPlugin', linkPreloadPlugin);
mainTemplate.hooks.localVars.tap('SriPlugin', addSriHashes);
} else {
this.sriPlugin.warnOnce(
Expand Down

0 comments on commit 0863fde

Please sign in to comment.