Skip to content

Commit

Permalink
Merge pull request #16944 from snitin315/fix/read-records-callback
Browse files Browse the repository at this point in the history
fix: handle callback correctly in the `readRecords` compiler hook
  • Loading branch information
TheLarkInn committed Apr 12, 2023
2 parents 66f6472 + d011d86 commit 69d9c40
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/Compiler.js
Expand Up @@ -970,10 +970,13 @@ ${other}`);
readRecords(callback) {
if (this.hooks.readRecords.isUsed()) {
if (this.recordsInputPath) {
asyncLib.parallel([
cb => this.hooks.readRecords.callAsync(cb),
this._readRecords.bind(this)
]);
asyncLib.parallel(
[
cb => this.hooks.readRecords.callAsync(cb),
this._readRecords.bind(this)
],
err => callback(err)
);
} else {
this.records = {};
this.hooks.readRecords.callAsync(callback);
Expand Down
@@ -0,0 +1,12 @@
class ReadRecordsPlugin {
apply(compiler) {
compiler.hooks.readRecords.tapAsync("ReadRecordsPlugin", callback => {
setTimeout(() => {
console.log("Done with reading records.");
callback();
}, 1000);
});
}
}

module.exports = ReadRecordsPlugin;
1 change: 1 addition & 0 deletions test/configCases/records/with-readRecords-hook/async.js
@@ -0,0 +1 @@
import "vendor";
3 changes: 3 additions & 0 deletions test/configCases/records/with-readRecords-hook/index.js
@@ -0,0 +1,3 @@
it("should load fine", () => {
return import(/* webpackChunkName: "async" */"./async");
});
Empty file.
10 changes: 10 additions & 0 deletions test/configCases/records/with-readRecords-hook/records.json
@@ -0,0 +1,10 @@
{
"chunks": {
"byName": {
"vendors~async": 123
},
"bySource": {
"1 index.js ./async": 123
}
}
}
17 changes: 17 additions & 0 deletions test/configCases/records/with-readRecords-hook/webpack.config.js
@@ -0,0 +1,17 @@
const path = require("path");
const ReadRecordsPlugin = require("./ReadRecordsPlugin");

/** @type {import("../../../../").Configuration} */
module.exports = {
entry: "./index",
recordsInputPath: path.resolve(__dirname, "records.json"),
output: {
chunkFilename: "[name]-[chunkhash].js"
},
plugins: [new ReadRecordsPlugin()],
optimization: {
splitChunks: {
minSize: 0
}
}
};

0 comments on commit 69d9c40

Please sign in to comment.