Skip to content

Commit

Permalink
avoid copying source types and sizes cache from unsafe cache to cache…
Browse files Browse the repository at this point in the history
…d module

fixes #13827

avoid pre-computing source types and sizes on cleanup for not-built modules (e. g. from unsafe cache)
improve eror message when asset rendering fails
  • Loading branch information
sokra committed Jul 22, 2021
1 parent 937957f commit cdc9efe
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 26 deletions.
14 changes: 8 additions & 6 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,20 @@ 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);
// Make sure to cache types and sizes before cleanup when this module has been built
// They are accessed by the stats and we don't want them to crash after cleanup
// TODO reconsider this for webpack 6
if (this.buildInfo) {
if (this._sourceTypes === undefined) this.getSourceTypes();
for (const type of this._sourceTypes) {
this.size(type);
}
}
super.cleanupForCache();
this.parser = undefined;
Expand Down
41 changes: 23 additions & 18 deletions lib/asset/AssetModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,29 @@ class AssetModulesPlugin {
);
if (modules) {
for (const module of modules) {
const codeGenResult = codeGenerationResults.get(
module,
chunk.runtime
);
result.push({
render: () => codeGenResult.sources.get(type),
filename:
module.buildInfo.filename ||
codeGenResult.data.get("filename"),
info:
module.buildInfo.assetInfo ||
codeGenResult.data.get("assetInfo"),
auxiliary: true,
identifier: `assetModule${chunkGraph.getModuleId(module)}`,
hash:
module.buildInfo.fullContentHash ||
codeGenResult.data.get("fullContentHash")
});
try {
const codeGenResult = codeGenerationResults.get(
module,
chunk.runtime
);
result.push({
render: () => codeGenResult.sources.get(type),
filename:
module.buildInfo.filename ||
codeGenResult.data.get("filename"),
info:
module.buildInfo.assetInfo ||
codeGenResult.data.get("assetInfo"),
auxiliary: true,
identifier: `assetModule${chunkGraph.getModuleId(module)}`,
hash:
module.buildInfo.fullContentHash ||
codeGenResult.data.get("fullContentHash")
});
} catch (e) {
e.message += `\nduring rendering of asset ${module.identifier()}`;
throw e;
}
}
}

Expand Down
18 changes: 16 additions & 2 deletions test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const describeCases = config => {
rimraf.sync(outputDirectory);
fs.mkdirSync(outputDirectory, { recursive: true });
const deprecationTracker = deprecationTracking.start();
require("..")(options, (err, stats) => {
const onCompiled = (err, stats) => {
const deprecations = deprecationTracker();
if (err) return handleFatalError(err, done);
const statOptions = {
Expand Down Expand Up @@ -570,7 +570,21 @@ const describeCases = config => {
done();
})
.catch(done);
});
};
if (config.cache) {
const compiler = require("..")(options);
compiler.run(err => {
if (err) return handleFatalError(err, done);
compiler.run((error, stats) => {
compiler.close(err => {
if (err) return handleFatalError(err, done);
onCompiled(error, stats);
});
});
});
} else {
require("..")(options, onCompiled);
}
}, 30000);

const {
Expand Down
5 changes: 5 additions & 0 deletions test/configCases/asset-modules/unsafe-cache-13827/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import url from "package";

it("should create a data url", () => {
expect(url.protocol).toBe("data:");
});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
module: {
rules: [
{
dependency: "url",
type: "asset"
}
]
}
};

0 comments on commit cdc9efe

Please sign in to comment.