Skip to content

Commit 49b44d6

Browse files
committed
SRI for assets added in beforeHtmlGeneration
This effectively provides compatbility with assets added by plugins such as add-asset-html-webpack-plugin. Also adds a test case for DllPlugin with html-webpack-plugin. Closes #51
1 parent 8601fe2 commit 49b44d6

File tree

16 files changed

+119
-48
lines changed

16 files changed

+119
-48
lines changed

examples/dll-plugin/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# DllPlugin Integration #hwp
2+
3+
Demonstrates how assets generated by DllPlugin can be included with
4+
SRI using html-webpack-plugin.

examples/dll-plugin/a.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'a';

examples/dll-plugin/alpha.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'alpha';

examples/dll-plugin/b.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'b';

examples/dll-plugin/beta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'beta';

examples/dll-plugin/c.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'ok';

examples/dll-plugin/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log(require('beta/beta'));
2+
console.log(require('beta/b'));
3+
console.log(require('beta/c.jsx'));

examples/dll-plugin/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var expect = require('expect');
2+
var webpackVersion = Number(
3+
require('webpack/package.json').version.split('.')[0]
4+
);
5+
6+
module.exports.skip = function skip() {
7+
// Can't use add-asset-html-webpack-plugin with older Webpack versions
8+
return webpackVersion < 4;
9+
};
10+
11+
module.exports.check = function check(stats) {
12+
expect(stats.compilation.errors).toEqual([]);
13+
expect(stats.compilation.warnings).toEqual([]);
14+
};

examples/dll-plugin/webpack.config.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var SriPlugin = require('webpack-subresource-integrity');
2+
var HtmlWebpackPlugin = require('html-webpack-plugin');
3+
var WebpackBeforeBuildPlugin = require('before-build-webpack');
4+
var webpack = require('webpack');
5+
var path = require('path');
6+
var AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
7+
8+
module.exports = {
9+
// mode: "development || "production",
10+
resolve: {
11+
extensions: ['.js', '.jsx']
12+
},
13+
entry: {
14+
alpha: ['./alpha', './a'],
15+
beta: ['./beta', './b', './c']
16+
},
17+
output: {
18+
filename: 'MyDll.[name].js',
19+
library: '[name]_[hash]'
20+
},
21+
plugins: [
22+
new webpack.DllPlugin({
23+
path: path.join(__dirname, 'dist', '[name]-manifest.json'),
24+
name: '[name]_[hash]'
25+
}),
26+
new WebpackBeforeBuildPlugin(
27+
function(_stats, callback) {
28+
webpack(
29+
{
30+
mode: 'production',
31+
entry: {
32+
index: './index.js'
33+
},
34+
output: {
35+
path: path.join(__dirname, 'dist'),
36+
crossOriginLoading: 'anonymous'
37+
},
38+
plugins: [
39+
new webpack.DllReferencePlugin({
40+
context: path.join(__dirname),
41+
manifest: require(path.join(
42+
__dirname,
43+
'dist/alpha-manifest.json'
44+
)) // eslint-disable-line
45+
}),
46+
new webpack.DllReferencePlugin({
47+
scope: 'beta',
48+
manifest: require(path.join(
49+
__dirname,
50+
'dist/beta-manifest.json'
51+
)), // eslint-disable-line
52+
extensions: ['.js', '.jsx']
53+
}),
54+
new HtmlWebpackPlugin(),
55+
new AddAssetHtmlPlugin({
56+
filepath: path.resolve(__dirname, 'dist/MyDll.*.js')
57+
}),
58+
new SriPlugin({
59+
hashFuncNames: ['sha256', 'sha384'],
60+
enabled: true
61+
})
62+
]
63+
},
64+
function afterEmit(err, stats) {
65+
if (err || stats.hasErrors() || stats.hasWarnings()) {
66+
throw err || new Error(stats.toString({ reason: true }));
67+
} else {
68+
callback();
69+
}
70+
}
71+
);
72+
},
73+
['done']
74+
)
75+
]
76+
};

examples/warn-checksum/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)