Skip to content

Commit

Permalink
Merge pull request #130 from waysact/webpack-5
Browse files Browse the repository at this point in the history
Webpack 5 support
  • Loading branch information
jscheid authored Oct 13, 2020
2 parents f9d76c0 + 1631ca6 commit 1d6bfcd
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 107 deletions.
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ node_js:
- 6
- 8
- 10
- 12
addons:
firefox: latest
env:
- WEBPACK_SUFFIX="@1" ETWP_SUFFIX="@1" HWP_SUFFIX="@2"
- WEBPACK_SUFFIX="@2" ETWP_SUFFIX="@2" HWP_SUFFIX="@2"
- WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2"
- WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3"
- WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2"
- WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4"
- WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4"
matrix:
exclude:
- node_js: 4
env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3"
- node_js: 4
env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2"
env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4"
- node_js: 4
env: WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4"
- node_js: 6
env: WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4"
- node_js: 8
env: WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4"
install:
- npm install || true
- npm rm webpack extract-text-webpack-plugin eslint
Expand All @@ -29,7 +37,6 @@ before_script:
services:
- xvfb
script:
- ./node_modules/karma/bin/karma start --single-run --browsers Firefox
- $(npm bin)/nyc $(npm bin)/mocha --exit --timeout 20000 && $(npm bin)/nyc report --reporter=text-lcov | $(npm bin)/coveralls
notifications:
webhooks:
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ manipulation.

## Features

- Optional integration with [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin)
- Automatic support for code splitting (integrity for lazy-loaded chunks)
- Compatible with Webpack 1.x, 2.x, 3.x and 4.x
- Optional integration with [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin).
- Automatic support for dynamic imports (also known as code splitting.)
- Compatible with all major Webpack versions, up to and including Webpack 5.

## Installation

```shell
npm install webpack-subresource-integrity --save-dev
```

```shell
yarn add --dev webpack-subresource-integrity
```

### Webpack Configuration Example

```javascript
Expand Down Expand Up @@ -81,9 +85,11 @@ template as follows:
#### Without HtmlWebpackPlugin

The correct value for the `integrity` attribute can be retrieved from
the `integrity` property of Webpack assets. However, that property is
not copied over by Webpack's `stats` module so you'll have to access
the "original" asset on the `compilation` object. For example:
the `integrity` property of Webpack assets.

Note that with Webpack versions before 5, that property is not copied over by
Webpack's `stats` module so you'll have to access the "original" asset on the
`compilation` object. For example:

```javascript
compiler.plugin("done", stats => {
Expand Down
61 changes: 41 additions & 20 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
# Test against this version of Node.js
environment:
matrix:
- nodejs_version: 4
WEBPACK_SUFFIX: '@1'
ETWP_SUFFIX: '@1'
FL_SUFFIX: '@0'
HWP_SUFFIX: '@2'
- nodejs_version: 4
WEBPACK_SUFFIX: '@2'
ETWP_SUFFIX: '@2'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@2'
- nodejs_version: 4
WEBPACK_SUFFIX: '@3'
ETWP_SUFFIX: '@3'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@2'
- nodejs_version: 6
WEBPACK_SUFFIX: '@1'
ETWP_SUFFIX: '@1'
Expand All @@ -40,7 +25,7 @@ environment:
WEBPACK_SUFFIX: '@4'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@4.0.0-alpha.2'
HWP_SUFFIX: '@4'
- nodejs_version: 8
WEBPACK_SUFFIX: '@1'
ETWP_SUFFIX: '@1'
Expand All @@ -65,7 +50,7 @@ environment:
WEBPACK_SUFFIX: '@4'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@4.0.0-alpha.2'
HWP_SUFFIX: '@4'
- nodejs_version: 10
WEBPACK_SUFFIX: '@1'
ETWP_SUFFIX: '@1'
Expand All @@ -90,14 +75,50 @@ environment:
WEBPACK_SUFFIX: '@4'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@4.0.0-alpha.2'
HWP_SUFFIX: '@4'
- nodejs_version: 10
WEBPACK_SUFFIX: '@5'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@4'
- nodejs_version: 12
WEBPACK_SUFFIX: '@1'
ETWP_SUFFIX: '@1'
FL_SUFFIX: '@0'
HWP_SUFFIX: '@2'
- nodejs_version: 12
WEBPACK_SUFFIX: '@2'
ETWP_SUFFIX: '@2'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@2'
- nodejs_version: 12
WEBPACK_SUFFIX: '@3'
ETWP_SUFFIX: '@3'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@2'
- nodejs_version: 12
WEBPACK_SUFFIX: '@4'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@3'
- nodejs_version: 12
WEBPACK_SUFFIX: '@4'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@4'
- nodejs_version: 12
WEBPACK_SUFFIX: '@5'
ETWP_SUFFIX: '@4.0.0-alpha.0'
FL_SUFFIX: '@1'
HWP_SUFFIX: '@4'

# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version
# install modules
- appveyor-retry npm install --global npm@3
- npm cache clean --force
- appveyor-retry npm install --global npm@6
- npm install || true
- npm rm webpack extract-text-webpack-plugin file-loader eslint
- npm install "webpack%WEBPACK_SUFFIX%" --ignore-scripts --loglevel=error || true
Expand All @@ -111,7 +132,7 @@ test_script:
- node --version
- npm --version
# run tests
- npm run mocha
- npm run test

# Don't actually build.
build: off
14 changes: 1 addition & 13 deletions examples/disabled/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
var SriPlugin = require('webpack-subresource-integrity');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var createExtractTextLoader = require('../utils').createExtractTextLoader;
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js'
},
module:
webpackVersion > 1
? { rules: [{ test: /\.css$/, use: createExtractTextLoader() }] }
: { loaders: [{ test: /\.css$/, loader: createExtractTextLoader() }] },
plugins: [
new ExtractTextPlugin('styles.css'),
new SriPlugin({ hashFuncNames: ['sha256'], enabled: false })
]
plugins: [new SriPlugin({ hashFuncNames: ['sha256'], enabled: false })]
};
8 changes: 8 additions & 0 deletions examples/etp-content-hash/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
var expect = require('expect');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// Can't use extract-text-webpack-plugin with Webpack > 4.
return webpackVersion > 4;
};

module.exports.check = function check(stats) {
expect(stats.compilation.warnings).toEqual([]);
Expand Down
8 changes: 8 additions & 0 deletions examples/etp-path-join/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
var expect = require('expect');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// Can't use extract-text-webpack-plugin with Webpack > 4.
return webpackVersion > 4;
};

module.exports.check = function check(stats) {
expect(stats.compilation.warnings).toEqual([]);
Expand Down
8 changes: 8 additions & 0 deletions examples/hwp-custom-template/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ var select = require('soupselect').select;
var expect = require('expect');
var htmlparser = require('htmlparser');
var fs = require('fs');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// Can't use extract-text-webpack-plugin with Webpack > 4.
return webpackVersion > 4;
};

module.exports.check = function check(stats) {
var jsIntegrity;
Expand Down
8 changes: 8 additions & 0 deletions examples/hwp-integrity-attributes/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ var select = require('soupselect').select;
var expect = require('expect');
var htmlparser = require('htmlparser');
var fs = require('fs');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// Can't use extract-text-webpack-plugin with Webpack > 4.
return webpackVersion > 4;
};

module.exports.check = function check(stats) {
var jsIntegrity;
Expand Down
5 changes: 4 additions & 1 deletion examples/hwp-output-parent-dir/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ module.exports.check = function check(stats) {
var jsIntegrity;

expect(stats.compilation.warnings).toEqual([]);
jsIntegrity = stats.compilation.assets['bundle.js'].integrity;

jsIntegrity =
stats.toJson().assets.find(asset => asset.name === 'bundle.js').integrity ||
stats.compilation.assets['bundle.js'].integrity;
expect(jsIntegrity).toMatch(/^sha/);

return new Promise((resolve, reject) => {
Expand Down
4 changes: 3 additions & 1 deletion examples/hwp-public-path/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module.exports.check = function check(stats) {
var jsIntegrity;

expect(stats.compilation.warnings).toEqual([]);
jsIntegrity = stats.compilation.assets['bundle.js'].integrity;
jsIntegrity =
stats.toJson().assets.find(asset => asset.name === 'bundle.js').integrity ||
stats.compilation.assets['bundle.js'].integrity;
expect(jsIntegrity).toMatch(/^sha/);

return new Promise((resolve, reject) => {
Expand Down
4 changes: 3 additions & 1 deletion examples/hwp-subdirectories/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var htmlparser = require('htmlparser');
var fs = require('fs');

module.exports.check = function check(stats) {
var jsIntegrity = stats.compilation.assets['subdir/bundle.js'].integrity;
var jsIntegrity =
stats.toJson().assets.find(asset => asset.name === 'subdir/bundle.js')
.integrity || stats.compilation.assets['subdir/bundle.js'].integrity;
expect(jsIntegrity).toMatch(/^sha/);

return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/mini-css-extract-plugin/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ var webpackVersion = Number(

module.exports.skip = function skip() {
// mini-css-extract-plugin needs Webpack 4
return webpackVersion < 4;
return webpackVersion !== 4;
};
8 changes: 8 additions & 0 deletions examples/no-error-invalid-config/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
var expect = require('expect');
var ChunkRenderError = require('webpack/lib/ChunkRenderError');
var webpackVersion = Number(
require('webpack/package.json').version.split('.')[0]
);

module.exports.skip = function skip() {
// Error doesn't get triggered with Webpack > 4.
return webpackVersion > 4;
};

module.exports.ignoreErrors = true;

Expand Down
1 change: 0 additions & 1 deletion examples/no-error-invalid-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var SriPlugin = require('webpack-subresource-integrity');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: './index.js',
Expand Down
2 changes: 1 addition & 1 deletion examples/sourcemap-code-splitting/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ module.exports.check = function check() {
}

var sriHashesInSource = findAndStripSriHashString('dist/index.js', 'sha256-', -10);
var sriHashesInMap = findAndStripSriHashString('dist/index.js.map', 'var sriHashes = ');
var sriHashesInMap = findAndStripSriHashString('dist/index.js.map', '__webpack_require__.sriHashes = ');
expect(sriHashesInSource.length).toEqual(sriHashesInMap.length);
};
Loading

0 comments on commit 1d6bfcd

Please sign in to comment.