Skip to content

Commit

Permalink
fixes #13330
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 18, 2021
1 parent c9e590d commit 9e7062f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
18 changes: 15 additions & 3 deletions lib/NormalModule.js
Expand Up @@ -276,6 +276,8 @@ class NormalModule extends Module {
this._source = null;
/** @private @type {Map<string, number> | undefined} **/
this._sourceSizes = undefined;
/** @private @type {Set<string>} */
this._sourceTypes = undefined;

// Cache
this._lastSuccessfulBuildMeta = {};
Expand Down Expand Up @@ -347,12 +349,19 @@ class NormalModule extends Module {
this.resource = m.resource;
this.matchResource = m.matchResource;
this.loaders = m.loaders;
this._sourceTypes = m._sourceTypes;
this._sourceSizes = m._sourceSizes;
}

/**
* Assuming this module is in the cache. Remove internal references to allow freeing some memory.
*/
cleanupForCache() {
// Make sure to cache types and sizes before cleanup
if (this._sourceTypes === undefined) this.getSourceTypes();
for (const type of this._sourceTypes) {
this.size(type);
}
super.cleanupForCache();
this.parser = undefined;
this.parserOptions = undefined;
Expand Down Expand Up @@ -390,6 +399,7 @@ class NormalModule extends Module {
this.type,
this.generatorOptions
);
// we assume the generator behaves identically and keep cached sourceTypes/Sizes
}

/**
Expand Down Expand Up @@ -873,6 +883,7 @@ class NormalModule extends Module {
this._forceBuild = false;
this._source = null;
if (this._sourceSizes !== undefined) this._sourceSizes.clear();
this._sourceTypes = undefined;
this._ast = null;
this.error = null;
this.clearWarningsAndErrors();
Expand Down Expand Up @@ -1075,7 +1086,10 @@ class NormalModule extends Module {
* @returns {Set<string>} types available (do not mutate)
*/
getSourceTypes() {
return this.generator.getTypes(this);
if (this._sourceTypes === undefined) {
this._sourceTypes = this.generator.getTypes(this);
}
return this._sourceTypes;
}

/**
Expand Down Expand Up @@ -1264,7 +1278,6 @@ class NormalModule extends Module {
const { write } = context;
// deserialize
write(this._source);
write(this._sourceSizes);
write(this.error);
write(this._lastSuccessfulBuildMeta);
write(this._forceBuild);
Expand Down Expand Up @@ -1296,7 +1309,6 @@ class NormalModule extends Module {
deserialize(context) {
const { read } = context;
this._source = read();
this._sourceSizes = read();
this.error = read();
this._lastSuccessfulBuildMeta = read();
this._forceBuild = read();
Expand Down
5 changes: 3 additions & 2 deletions test/Compiler.test.js
Expand Up @@ -467,11 +467,12 @@ describe("Compiler", () => {
}
});
compiler.outputFileSystem = createFsFromVolume(new Volume());
compiler.run((err, stats) => {
compiler.run((err, stats1) => {
if (err) return done(err);

compiler.run((err, stats) => {
compiler.run((err, stats2) => {
if (err) return done(err);
expect(stats1.toString({ all: true })).toBeTypeOf("string");
done();
});
});
Expand Down
8 changes: 7 additions & 1 deletion test/WatchTestCases.test.js
Expand Up @@ -169,10 +169,11 @@ describe("WatchTestCases", () => {
},
(err, stats) => {
if (err) return compilationFinished(err);
if (!stats)
if (!stats) {
return compilationFinished(
new Error("No stats reported from Compiler")
);
}
if (stats.hash === lastHash) return;
lastHash = stats.hash;
if (run.done && lastHash !== stats.hash) {
Expand All @@ -192,6 +193,7 @@ describe("WatchTestCases", () => {
}
if (waitMode) return;
run.done = true;
run.stats = stats;
if (err) return compilationFinished(err);
const statOptions = {
preset: "verbose",
Expand Down Expand Up @@ -383,6 +385,10 @@ describe("WatchTestCases", () => {
);
run.it = _it;
run.getNumberOfTests = getNumberOfTests;
it(`${run.name} should allow to read stats`, done => {
if (run.stats) run.stats.toString({ all: true });
done();
});
}

afterAll(() => {
Expand Down

0 comments on commit 9e7062f

Please sign in to comment.