Skip to content

Commit

Permalink
Fix crash in non-web builds, emit warning instead
Browse files Browse the repository at this point in the history
Closes #87
  • Loading branch information
jscheid committed Oct 2, 2018
1 parent 2ddc979 commit e5a464e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 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()
]
};
11 changes: 9 additions & 2 deletions jmtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(
}

if (mainTemplate.hooks) {
mainTemplate.hooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin);
mainTemplate.hooks.localVars.tap('SriPlugin', localVarsPlugin);
if (mainTemplate.hooks.jsonpScript && mainTemplate.hooks.localVars) {
mainTemplate.hooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin);
mainTemplate.hooks.localVars.tap('SriPlugin', localVarsPlugin);
} else {
this.sriPlugin.warnOnce(
this.compilation,
'This plugin is not useful for non-web targets.'
);
}
} else {
mainTemplate.plugin('jsonp-script', jsonpScriptPlugin);
mainTemplate.plugin('local-vars', localVarsPlugin);
Expand Down

0 comments on commit e5a464e

Please sign in to comment.