Skip to content

Commit

Permalink
Merge pull request #8029 from webpack/bugfix/hash-bootstrap
Browse files Browse the repository at this point in the history
hash content of bootstrap code
  • Loading branch information
sokra committed Sep 13, 2018
2 parents c2cfa7e + 13b2cdd commit aecdeb3
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 132 deletions.
6 changes: 5 additions & 1 deletion lib/ChunkTemplate.js
Expand Up @@ -9,6 +9,7 @@ const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Module")} Module} */
/** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate} */
/** @typedef {import("./util/createHash").Hash} Hash} */

/**
Expand Down Expand Up @@ -71,12 +72,15 @@ module.exports = class ChunkTemplate extends Tapable {
}

/**
* TODO webpack 5: remove moduleTemplate and dependencyTemplates
* Updates hash with chunk-specific information from this template
* @param {Hash} hash the hash to update
* @param {Chunk} chunk the chunk
* @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
* @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
* @returns {void}
*/
updateHashForChunk(hash, chunk) {
updateHashForChunk(hash, chunk, moduleTemplate, dependencyTemplates) {
this.updateHash(hash);
this.hooks.hashForChunk.call(hash, chunk);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Compilation.js
Expand Up @@ -455,9 +455,10 @@ class Compilation extends Tapable {
this.children = [];
/** @type {Map<DepConstructor, ModuleFactory>} */
this.dependencyFactories = new Map();
/** @type {Map<DepConstructor|string, DependencyTemplate|string>} */
/** @type {Map<DepConstructor, DependencyTemplate>} */
this.dependencyTemplates = new Map();
// TODO refactor this in webpack 5 to a custom DependencyTemplates class with a hash property
// @ts-ignore
this.dependencyTemplates.set("hash", "");
this.childrenCounters = {};
/** @type {Set<number|string>} */
Expand Down Expand Up @@ -2259,7 +2260,12 @@ class Compilation extends Tapable {
const template = chunk.hasRuntime()
? this.mainTemplate
: this.chunkTemplate;
template.updateHashForChunk(chunkHash, chunk);
template.updateHashForChunk(
chunkHash,
chunk,
this.moduleTemplates.javascript,
this.dependencyTemplates
);
this.hooks.chunkHash.call(chunk, chunkHash);
chunk.hash = chunkHash.digest(hashDigest);
hash.update(chunk.hash);
Expand Down
7 changes: 6 additions & 1 deletion lib/JavascriptModulesPlugin.js
Expand Up @@ -137,7 +137,12 @@ class JavascriptModulesPlugin {
: compilation.chunkTemplate;
hash.update(`${chunk.id} `);
hash.update(chunk.ids ? chunk.ids.join(",") : "");
template.updateHashForChunk(hash, chunk);
template.updateHashForChunk(
hash,
chunk,
compilation.moduleTemplates.javascript,
compilation.dependencyTemplates
);
for (const m of chunk.modulesIterable) {
if (typeof m.source === "function") {
hash.update(m.hash);
Expand Down
37 changes: 32 additions & 5 deletions lib/MainTemplate.js
Expand Up @@ -361,14 +361,14 @@ module.exports = class MainTemplate extends Tapable {
}

/**
*
* TODO webpack 5: remove moduleTemplate and dependencyTemplates
* @param {string} hash hash to be used for render call
* @param {Chunk} chunk Chunk instance
* @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
* @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
* @returns {ConcatSource} the newly generated source from rendering
* @returns {string[]} the generated source of the bootstrap code
*/
render(hash, chunk, moduleTemplate, dependencyTemplates) {
renderBootstrap(hash, chunk, moduleTemplate, dependencyTemplates) {
const buf = [];
buf.push(
this.hooks.bootstrap.call(
Expand All @@ -392,6 +392,23 @@ module.exports = class MainTemplate extends Tapable {
buf.push("");
buf.push(Template.asString(this.hooks.beforeStartup.call("", chunk, hash)));
buf.push(Template.asString(this.hooks.startup.call("", chunk, hash)));
return buf;
}

/**
* @param {string} hash hash to be used for render call
* @param {Chunk} chunk Chunk instance
* @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
* @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
* @returns {ConcatSource} the newly generated source from rendering
*/
render(hash, chunk, moduleTemplate, dependencyTemplates) {
const buf = this.renderBootstrap(
hash,
chunk,
moduleTemplate,
dependencyTemplates
);
let source = this.hooks.render.call(
new OriginalSource(
Template.prefix(buf, " \t") + "\n",
Expand Down Expand Up @@ -486,19 +503,29 @@ module.exports = class MainTemplate extends Tapable {
updateHash(hash) {
hash.update("maintemplate");
hash.update("3");
hash.update(this.outputOptions.publicPath + "");
this.hooks.hash.call(hash);
}

/**
* TODO webpack 5: remove moduleTemplate and dependencyTemplates
* Updates hash with chunk-specific information from this template
* @param {Hash} hash the hash to update
* @param {Chunk} chunk the chunk
* @param {ModuleTemplate} moduleTemplate ModuleTemplate instance for render
* @param {Map<Function, DependencyTemplate>} dependencyTemplates dependency templates
* @returns {void}
*/
updateHashForChunk(hash, chunk) {
updateHashForChunk(hash, chunk, moduleTemplate, dependencyTemplates) {
this.updateHash(hash);
this.hooks.hashForChunk.call(hash, chunk);
for (const line of this.renderBootstrap(
"0000",
chunk,
moduleTemplate,
dependencyTemplates
)) {
hash.update(line);
}
}

useChunkHash(chunk) {
Expand Down
4 changes: 1 addition & 3 deletions lib/node/NodeMainTemplatePlugin.js
Expand Up @@ -315,9 +315,7 @@ module.exports = class NodeMainTemplatePlugin {
);
mainTemplate.hooks.hash.tap("NodeMainTemplatePlugin", hash => {
hash.update("node");
hash.update("3");
hash.update(mainTemplate.outputOptions.filename + "");
hash.update(mainTemplate.outputOptions.chunkFilename + "");
hash.update("4");
});
}
};
17 changes: 1 addition & 16 deletions lib/wasm/WasmMainTemplatePlugin.js
Expand Up @@ -335,23 +335,8 @@ class WasmMainTemplatePlugin {
);
mainTemplate.hooks.hash.tap("WasmMainTemplatePlugin", hash => {
hash.update("WasmMainTemplatePlugin");
hash.update("1");
hash.update(`${mainTemplate.outputOptions.webassemblyModuleFilename}`);
hash.update(`${this.mangleImports}`);
hash.update("2");
});
mainTemplate.hooks.hashForChunk.tap(
"WasmMainTemplatePlugin",
(hash, chunk) => {
const chunkModuleMaps = chunk.getChunkModuleMaps(m =>
m.type.startsWith("webassembly")
);
hash.update(JSON.stringify(chunkModuleMaps.id));
const wasmModules = getAllWasmModules(chunk);
for (const module of wasmModules) {
hash.update(module.hash);
}
}
);
}
}

Expand Down
6 changes: 1 addition & 5 deletions lib/web/JsonpMainTemplatePlugin.js
Expand Up @@ -581,11 +581,7 @@ ${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
);
mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => {
hash.update("jsonp");
hash.update("5");
hash.update(`${mainTemplate.outputOptions.globalObject}`);
hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
hash.update("6");
});
}
}
Expand Down
7 changes: 1 addition & 6 deletions lib/webworker/WebWorkerMainTemplatePlugin.js
Expand Up @@ -184,12 +184,7 @@ class WebWorkerMainTemplatePlugin {
);
mainTemplate.hooks.hash.tap("WebWorkerMainTemplatePlugin", hash => {
hash.update("webworker");
hash.update("3");
hash.update(`${mainTemplate.outputOptions.publicPath}`);
hash.update(`${mainTemplate.outputOptions.filename}`);
hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
hash.update(`${mainTemplate.outputOptions.chunkCallbackName}`);
hash.update(`${mainTemplate.outputOptions.globalObject}`);
hash.update("4");
});
}
}
Expand Down

0 comments on commit aecdeb3

Please sign in to comment.