Skip to content

Commit d2b213a

Browse files
committed
feat: Webpack 5 compatibility
Use processAssets hook to delete files and remove entries from the compilation.assets
1 parent e8f2212 commit d2b213a

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
22
const { inlineSource } = require('inline-source');
33
const { getTagRegExp } = require('inline-source/lib/utils');
44
const htmlparser = require('htmlparser2');
5+
const { Compilation } = require('webpack');
56

67
class InlineSourceWebpackPlugin {
78
constructor(options = {}) {
@@ -69,7 +70,7 @@ class InlineSourceWebpackPlugin {
6970
compilation.fileDependencies.add(source.filepath);
7071
} else {
7172
// Before Webpack 4
72-
// fileDepenencies was an array
73+
// fileDependencies was an array
7374
compilation.fileDependencies.push(source.filepath);
7475
}
7576
}
@@ -124,7 +125,13 @@ class InlineSourceWebpackPlugin {
124125
*/
125126
_deleteAsset(compilation) {
126127
if (this.deleteAssets.length) {
127-
this.deleteAssets.forEach(asset => delete compilation.assets[asset.name]);
128+
if ('deleteAsset' in compilation) {
129+
this.deleteAssets.forEach(asset => {
130+
compilation.deleteAsset(asset.name);
131+
});
132+
} else {
133+
this.deleteAssets.forEach(asset => delete compilation.assets[asset.name]);
134+
}
128135
}
129136
this.deleteAssets = [];
130137
}
@@ -157,10 +164,19 @@ class InlineSourceWebpackPlugin {
157164
}
158165
});
159166
}
160-
compiler.hooks.emit.tapAsync(name, (compilation, callback) => {
161-
this._deleteAsset(compilation);
162-
callback && callback();
163-
});
167+
if (Compilation && Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE) {
168+
compiler.hooks.thisCompilation.tap(name, compilation => {
169+
compilation.hooks.processAssets.tap(
170+
{ name, stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE },
171+
() => { this._deleteAsset(compilation); }
172+
);
173+
});
174+
} else {
175+
compiler.hooks.emit.tapAsync(name, (compilation, callback) => {
176+
this._deleteAsset(compilation);
177+
callback && callback();
178+
});
179+
}
164180
} else {
165181
// webpack 2 or 3
166182
compiler.plugin('compilation', compilation => {

test/index.spec.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require('fs');
12
const path = require('path');
23
const rimraf = require('rimraf');
34
const webpack = require('webpack');
@@ -17,7 +18,6 @@ const baseWebpackConfig = {
1718
filename: '[name].[contenthash].js'
1819
},
1920
optimization: {
20-
namedChunks: true,
2121
runtimeChunk: 'single'
2222
},
2323
mode: 'production'
@@ -79,16 +79,10 @@ function testInlineSourceWebpackPlugin(
7979
).toBeTruthy();
8080
}
8181
if (expectResults && expectResults.length) {
82-
const content = stats.compilation.assets[outputFile].source();
83-
expect(
84-
expectResults.every(result => {
85-
if (result instanceof RegExp) {
86-
return result.test(content);
87-
} else {
88-
return content.indexOf(result) > -1;
89-
}
90-
})
91-
).toBeTruthy();
82+
const content = fs.readFileSync(path.join(__dirname, `../dist/${outputFile}`)).toString();
83+
expectResults.forEach(result => {
84+
expect(content).toMatch(result);
85+
});
9286
}
9387
done();
9488
});
@@ -110,11 +104,11 @@ describe('InlineSourceWebpackPlugin', () => {
110104
// inline.js
111105
`<script>function Person(){}Person.prototype.sayHello=function(){console.log("[inline]:","hello world!")},(new Person).sayHello();</script>`,
112106
// webpack runtime file
113-
`window.webpackJsonp=window.webpackJsonp||[]`,
107+
`webpack`,
114108
// bundle.js
115109
`console.log("This file is build by webpack.But InlineSourceWebpackPlugin will embed it into html file.")`,
116110
// appended by html-webpack-plugin
117-
/<script src="\/inline-source-webpack-plugin\/dist\/index\.\w+\.js"><\/script>/
111+
/<script (?:defer="defer" )?src="\/inline-source-webpack-plugin\/dist\/index\.\w+\.js"><\/script>/
118112
]
119113
});
120114
});
@@ -190,8 +184,6 @@ describe('InlineSourceWebpackPlugin', () => {
190184
testInlineSourceWebpackPlugin({
191185
webpackConfig,
192186
done,
193-
expectOutputFiles: [/index\.html$/, /index\.\w+\.js$/, /runtime\.\w+\.js$/, /bundle\.\w+\.js$/],
194-
expectResults: ['<!--inline-source-webpack-plugin-->'],
195187
hasErrors: true
196188
});
197189
});

0 commit comments

Comments
 (0)