Skip to content

Commit

Permalink
Merge pull request #89 from waysact/87-non-web-build-warning
Browse files Browse the repository at this point in the history
Fix crash in non-web builds, emit warning instead
  • Loading branch information
kalmbach authored Oct 3, 2018
2 parents f434b35 + 22fa253 commit e5388c3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 50 deletions.
3 changes: 3 additions & 0 deletions examples/non-web-build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Non-Web build

Ensure the plugin doesn't crash in a build for a non-web target.
Empty file added examples/non-web-build/index.js
Empty file.
17 changes: 17 additions & 0 deletions examples/non-web-build/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var expect = require('expect');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// No reliable way to detect JsonWebTemplate usage on Webpack < 4.
return webpackVersion < 4;
};

module.exports.check = function check(stats) {
expect(stats.compilation.errors).toEqual([]);
expect(stats.compilation.warnings.length).toEqual(1);
expect(stats.compilation.warnings[0].message).toMatch(
/This plugin is not useful for non-web targets/
);
};
19 changes: 19 additions & 0 deletions examples/non-web-build/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var SriPlugin = require('webpack-subresource-integrity');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: {
index: './index.js'
},
target: 'node',
output: {
crossOriginLoading: 'anonymous'
},
plugins: [
new SriPlugin({
hashFuncNames: ['sha256', 'sha384'],
enabled: true
}),
new HtmlWebpackPlugin()
]
};
98 changes: 48 additions & 50 deletions jmtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,69 @@ function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) {
this.compilation = compilation;
}

function addSriHashes(plugin, chunk, source) {
var allChunks = util.findChunks(chunk);
WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
function addSriHashes(mainTemplate, source, chunk) {
var allChunks = util.findChunks(chunk);

if (allChunks.size > 0) {
return (Template.asString || plugin.asString)([
source,
'var sriHashes = ' +
JSON.stringify(
Array.from(allChunks).reduce(function chunkIdReducer(
sriHashes,
depChunk
) {
if (chunk !== depChunk) {
// eslint-disable-next-line no-param-reassign
sriHashes[depChunk.id] = util.makePlaceholder(depChunk.id);
}
return sriHashes;
},
{})
) +
';'
]);
}

return source;
}
if (allChunks.size > 0) {
return (Template.asString || mainTemplate.asString)([
source,
'var sriHashes = ' +
JSON.stringify(
Array.from(allChunks).reduce(function chunkIdReducer(
sriHashes,
depChunk
) {
if (chunk !== depChunk) {
// eslint-disable-next-line no-param-reassign
sriHashes[depChunk.id] = util.makePlaceholder(depChunk.id);
}
return sriHashes;
},
{})
) +
';'
]);
}

WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(
mainTemplate
) {
var self = this;
return source;
};

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

/*
* Patch local-vars code to add a mapping from chunk ID to SRIs.
* Since SRIs haven't been computed at this point, we're using
* magic placeholders for SRI values and going to replace them
* later.
*/
function localVarsPlugin(source, chunk) {
return addSriHashes(this, chunk, source);
}
WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(
mainTemplate
) {
var jsonpScriptPlugin = this.jsonpScriptPlugin.bind(this, mainTemplate);
var addSriHashes = this.addSriHashes.bind(this, mainTemplate);

if (mainTemplate.hooks) {
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.localVars.tap('SriPlugin', localVarsPlugin);
mainTemplate.hooks.localVars.tap('SriPlugin', addSriHashes);
} else {
mainTemplate.plugin('jsonp-script', jsonpScriptPlugin);
mainTemplate.plugin('local-vars', localVarsPlugin);
this.sriPlugin.warnOnce(
this.compilation,
'This plugin is not useful for non-web targets.'
);
}
};

Expand Down

0 comments on commit e5388c3

Please sign in to comment.