Skip to content

Commit

Permalink
Merge pull request #9318 from jamesgeorge007/hotfix/ease-access-of-em…
Browse files Browse the repository at this point in the history
…itted-assets

chore: Makes it easier to access contents of emitted assets
  • Loading branch information
sokra committed Aug 1, 2019
2 parents ba20513 + 53a7a34 commit ed7d815
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/Compiler.js
Expand Up @@ -55,6 +55,8 @@ class Compiler extends Tapable {
run: new AsyncSeriesHook(["compiler"]),
/** @type {AsyncSeriesHook<Compilation>} */
emit: new AsyncSeriesHook(["compilation"]),
/** @type {AsyncSeriesHook<string, Buffer>} */
assetEmitted: new AsyncSeriesHook(["file", "content"]),
/** @type {AsyncSeriesHook<Compilation>} */
afterEmit: new AsyncSeriesHook(["compilation"]),

Expand Down Expand Up @@ -430,7 +432,7 @@ class Compiler extends Tapable {
: targetFileGeneration + 1;
cacheEntry.writtenTo.set(targetPath, newGeneration);
this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
callback();
this.hooks.assetEmitted.callAsync(file, content, callback);
});
} else {
if (source.existsAt === targetPath) {
Expand All @@ -445,7 +447,10 @@ class Compiler extends Tapable {

source.existsAt = targetPath;
source.emitted = true;
this.outputFileSystem.writeFile(targetPath, content, callback);
this.outputFileSystem.writeFile(targetPath, content, err => {
if (err) return callback(err);
this.hooks.assetEmitted.callAsync(file, content, callback);
});
}
};

Expand Down
3 changes: 3 additions & 0 deletions test/configCases/asset-emitted/futureEmitAssets/index.js
@@ -0,0 +1,3 @@
import("./module");

it("should run", () => {});
Empty file.
21 changes: 21 additions & 0 deletions test/configCases/asset-emitted/futureEmitAssets/webpack.config.js
@@ -0,0 +1,21 @@
module.exports = {
output: {
futureEmitAssets: true
},
plugins: [
compiler => {
const files = {};
compiler.hooks.assetEmitted.tap("Test", (file, buffer) => {
files[file] = Buffer.isBuffer(buffer);
});
compiler.hooks.afterEmit.tap("Test", () => {
expect(files).toMatchInlineSnapshot(`
Object {
"1.bundle0.js": true,
"bundle0.js": true,
}
`);
});
}
]
};
3 changes: 3 additions & 0 deletions test/configCases/asset-emitted/normal/index.js
@@ -0,0 +1,3 @@
import("./module");

it("should run", () => {});
Empty file.
18 changes: 18 additions & 0 deletions test/configCases/asset-emitted/normal/webpack.config.js
@@ -0,0 +1,18 @@
module.exports = {
plugins: [
compiler => {
const files = {};
compiler.hooks.assetEmitted.tap("Test", (file, buffer) => {
files[file] = Buffer.isBuffer(buffer);
});
compiler.hooks.afterEmit.tap("Test", () => {
expect(files).toMatchInlineSnapshot(`
Object {
"1.bundle0.js": true,
"bundle0.js": true,
}
`);
});
}
]
};

0 comments on commit ed7d815

Please sign in to comment.