Skip to content

Commit 2068887

Browse files
authored
Update for Webpack 5, emit string assets only once (#6)
1 parent b6b3924 commit 2068887

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
22
node_js:
3+
- '14'
4+
- '12'
35
- '10'
4-
- '8'

fixture/webpack.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module.exports = {
77
},
88
entry: __dirname,
99
plugins: [
10-
new AddAssetPlugin('rainbow.js', '🌈'),
11-
new AddAssetPlugin('cake.js', () => '🎂'),
12-
new AddAssetPlugin('cat.js', () => Promise.resolve('🐈'))
10+
new AddAssetPlugin('rainbow.js', 'console.log("🌈")'),
11+
new AddAssetPlugin('cake.js', () => 'console.log("🎂")'),
12+
new AddAssetPlugin('cat.js', () => Promise.resolve('console.log("🐈")'))
1313
]
1414
};

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
'use strict';
22

3+
const {Compilation, sources: {RawSource}} = require('webpack');
4+
5+
const tapOptions = {
6+
name: 'AddAssetPlugin',
7+
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
8+
};
9+
310
module.exports = class AddAssetPlugin {
411
constructor(filePath, source) {
512
this.filePath = filePath;
613
this.source = source;
714
}
815

916
apply(compiler) {
10-
compiler.hooks.emit.tapPromise('AddAssetPlugin', async compilation => {
11-
const source = typeof this.source === 'string' ? this.source : await this.source(compilation);
17+
compiler.hooks.compilation.tap('AddAssetPlugin', compilation => {
18+
compilation.hooks.processAssets.tapPromise(tapOptions, async () => {
19+
let source;
20+
if (typeof this.source === 'string') {
21+
if (compilation.getAsset(this.filePath)) {
22+
// Skip emitting the asset again because it's immutable
23+
return;
24+
}
25+
26+
source = this.source;
27+
} else {
28+
source = await this.source(compilation);
29+
}
1230

13-
compilation.assets[this.filePath] = {
14-
source: () => source,
15-
size: () => source.length
16-
};
31+
compilation.emitAsset(this.filePath, new RawSource(source));
32+
});
1733
});
1834
}
1935
};

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=8"
13+
"node": ">=10.13.0"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -30,12 +30,12 @@
3030
],
3131
"devDependencies": {
3232
"ava": "^1.4.1",
33-
"pify": "^4.0.1",
33+
"pify": "^5.0.0",
3434
"tempy": "^0.3.0",
35-
"webpack": "^4.30.0",
35+
"webpack": "^5.4.0",
3636
"xo": "^0.24.0"
3737
},
3838
"peerDependencies": {
39-
"webpack": ">=4"
39+
"webpack": ">=5"
4040
}
4141
}

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test('main', async t => {
1111
config.output.path = cwd;
1212
await pify(webpack)(config);
1313
t.true(fs.readFileSync(path.join(cwd, 'unicorn.js'), 'utf8').includes('🦄'));
14-
t.is(fs.readFileSync(path.join(cwd, 'rainbow.js'), 'utf8'), '🌈');
15-
t.is(fs.readFileSync(path.join(cwd, 'cake.js'), 'utf8'), '🎂');
16-
t.is(fs.readFileSync(path.join(cwd, 'cat.js'), 'utf8'), '🐈');
14+
t.true(fs.readFileSync(path.join(cwd, 'rainbow.js'), 'utf8').includes('🌈'));
15+
t.true(fs.readFileSync(path.join(cwd, 'cake.js'), 'utf8').includes('🎂'));
16+
t.true(fs.readFileSync(path.join(cwd, 'cat.js'), 'utf8').includes('🐈'));
1717
});

0 commit comments

Comments
 (0)