Skip to content

Commit

Permalink
fix caching of asset modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 4, 2021
1 parent 53f10fd commit d9ef855
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/Generator.js
Expand Up @@ -28,6 +28,7 @@
* @property {RuntimeSpec} runtime the runtime
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
* @property {string} type which kind of code should be generated
* @property {function(): Map<string, any>=} getData get access to the code generation data
*/

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/NormalModule.js
Expand Up @@ -993,6 +993,13 @@ class NormalModule extends Module {
runtimeRequirements.add(RuntimeGlobals.thisAsExports);
}

/** @type {Map<string, any>} */
let data;
const getData = () => {
if (data === undefined) data = new Map();
return data;
};

const sources = new Map();
for (const type of this.generator.getTypes(this)) {
const source = this.error
Expand All @@ -1007,6 +1014,7 @@ class NormalModule extends Module {
runtimeRequirements,
runtime,
concatenationScope,
getData,
type
});

Expand All @@ -1018,7 +1026,8 @@ class NormalModule extends Module {
/** @type {CodeGenerationResult} */
const resultEntry = {
sources,
runtimeRequirements
runtimeRequirements,
data
};
return resultEntry;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/asset/AssetGenerator.js
Expand Up @@ -47,7 +47,7 @@ class AssetGenerator extends Generator {
*/
generate(
module,
{ runtime, chunkGraph, runtimeTemplate, runtimeRequirements, type }
{ runtime, chunkGraph, runtimeTemplate, runtimeRequirements, type, getData }
) {
switch (type) {
case "asset":
Expand Down Expand Up @@ -146,6 +146,15 @@ class AssetGenerator extends Generator {
sourceFilename,
...info
};
if (getData) {
// Due to code generation caching module.buildInfo.XXX can't used to store such information
// It need to be stored in the code generation results instead, where it's cached too
// TODO webpack 6 For back-compat reasons we also store in on module.buildInfo
const data = getData();
data.set("fullContentHash", fullHash);
data.set("filename", filename);
data.set("assetInfo", info);
}

runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p

Expand Down
19 changes: 14 additions & 5 deletions lib/asset/AssetModulesPlugin.js
Expand Up @@ -145,14 +145,23 @@ class AssetModulesPlugin {
);
if (modules) {
for (const module of modules) {
const codeGenResult = codeGenerationResults.get(
module,
chunk.runtime
);
result.push({
render: () =>
codeGenerationResults.getSource(module, chunk.runtime, type),
filename: module.buildInfo.filename,
info: module.buildInfo.assetInfo,
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
hash:
module.buildInfo.fullContentHash ||
codeGenResult.data.get("fullContentHash")
});
}
}
Expand Down
12 changes: 9 additions & 3 deletions lib/cache/IdleFileCachePlugin.js
Expand Up @@ -127,9 +127,15 @@ class IdleFileCachePlugin {
});
return;
}
currentIdlePromise = currentIdlePromise.then(() =>
strategy.afterAllStored()
);
currentIdlePromise = currentIdlePromise
.then(() => strategy.afterAllStored())
.catch(err => {
const logger = compiler.getInfrastructureLogger(
"IdleFileCachePlugin"
);
logger.warn(`Background tasks during idle failed: ${err.message}`);
logger.debug(err.stack);
});
isInitialStore = false;
}
};
Expand Down
5 changes: 5 additions & 0 deletions types.d.ts
Expand Up @@ -3796,6 +3796,11 @@ declare interface GenerateContext {
* which kind of code should be generated
*/
type: string;

/**
* get access to the code generation data
*/
getData?: () => Map<string, any>;
}
declare class Generator {
constructor();
Expand Down

0 comments on commit d9ef855

Please sign in to comment.