2018-12-07 19:26:35 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Florent Cailhol @ooflorent
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { compareChunksNatural } = require("../util/comparators");
|
|
|
|
const {
|
|
|
|
getFullChunkName,
|
|
|
|
getUsedChunkIds,
|
|
|
|
assignDeterministicIds
|
|
|
|
} = require("./IdHelpers");
|
|
|
|
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
/** @typedef {import("../Module")} Module */
|
|
|
|
|
|
|
|
class DeterministicChunkIdsPlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"DeterministicChunkIdsPlugin",
|
|
|
|
compilation => {
|
|
|
|
compilation.hooks.chunkIds.tap(
|
|
|
|
"DeterministicChunkIdsPlugin",
|
|
|
|
chunks => {
|
|
|
|
const chunkGraph = compilation.chunkGraph;
|
2018-12-07 23:07:40 +08:00
|
|
|
const context = this.options.context
|
|
|
|
? this.options.context
|
|
|
|
: compiler.context;
|
2018-12-07 19:26:35 +08:00
|
|
|
|
|
|
|
const compareNatural = compareChunksNatural(chunkGraph);
|
|
|
|
|
|
|
|
assignDeterministicIds(
|
|
|
|
Array.from(chunks).filter(chunk => {
|
|
|
|
return chunk.id === null;
|
|
|
|
}),
|
2018-12-08 01:12:04 +08:00
|
|
|
chunk =>
|
|
|
|
getFullChunkName(chunk, chunkGraph, context, compiler.root),
|
2018-12-07 19:26:35 +08:00
|
|
|
compareNatural,
|
2018-12-07 21:47:25 +08:00
|
|
|
this.options.maxLength || 3,
|
2018-12-07 19:26:35 +08:00
|
|
|
getUsedChunkIds(compilation),
|
|
|
|
(chunk, id) => {
|
|
|
|
chunk.id = id;
|
|
|
|
chunk.ids = [id];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DeterministicChunkIdsPlugin;
|