Skip to content

Commit

Permalink
Add integrity to link preload tags
Browse files Browse the repository at this point in the history
Note that the test case isn't very useful as an automatic test because
behaviour differs between browser models and versions, and the test is
only run in a single Chromium version via Puppeteer. The test is still
useful when run manually, like so:

  npm install  # install requirements
  npm install webpack@4 \
    extract-text-webpack-plugin@4.0.0-alpha.0 \
    html-webpack-plugin@3  # upgrade to Webpack 4
  npm run mocha  # Run mocha tests
  (cd examples/webpack-preload/dist/ && python -m SimpleHTTPServer)
  # Visit http://localhost:8000/ with different browsers

See #111
  • Loading branch information
jscheid committed Aug 30, 2019
1 parent 9f448b7 commit 1c6e643
Show file tree
Hide file tree
Showing 7 changed files with 52 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');
1 change: 1 addition & 0 deletions examples/webpack-preload/lazy-chunk-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import(/* webpackPreload: true */ './lazy-chunk-2.js');
1 change: 1 addition & 0 deletions examples/webpack-preload/lazy-chunk-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('ok');
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 breaks Webpack 1
return webpackVersion < 2;
};
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 1c6e643

Please sign in to comment.