Skip to content

Commit

Permalink
Emit string assets only once
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Nov 10, 2020
1 parent a74cdab commit 78311a3
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions index.js
Expand Up @@ -2,6 +2,11 @@

const {Compilation, sources: {RawSource}} = require('webpack');

const tapOptions = {
name: 'AddAssetPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
};

module.exports = class AddAssetPlugin {
constructor(filePath, source) {
this.filePath = filePath;
Expand All @@ -10,11 +15,19 @@ module.exports = class AddAssetPlugin {

apply(compiler) {
compiler.hooks.compilation.tap('AddAssetPlugin', compilation => {
compilation.hooks.processAssets.tapPromise({
name: 'AddAssetPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
}, async () => {
const source = typeof this.source === 'string' ? this.source : await this.source(compilation);
compilation.hooks.processAssets.tapPromise(tapOptions, async () => {
let source;
if (typeof this.source === 'string') {
if (compilation.getAsset(this.filePath)) {
// Skip emitting the asset again because it's immutable
return;
}

source = this.source;
} else {
source = await this.source(compilation);
}

compilation.emitAsset(this.filePath, new RawSource(source));
});
});
Expand Down

0 comments on commit 78311a3

Please sign in to comment.