[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Florent Cailhol @ooflorent
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { compareChunksNatural } = require("../util/comparators");
|
---|
| 9 | const {
|
---|
| 10 | getFullChunkName,
|
---|
| 11 | getUsedChunkIds,
|
---|
| 12 | assignDeterministicIds
|
---|
| 13 | } = require("./IdHelpers");
|
---|
| 14 |
|
---|
| 15 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 16 | /** @typedef {import("../Module")} Module */
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * @typedef {object} DeterministicChunkIdsPluginOptions
|
---|
| 20 | * @property {string=} context context for ids
|
---|
| 21 | * @property {number=} maxLength maximum length of ids
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | class DeterministicChunkIdsPlugin {
|
---|
| 25 | /**
|
---|
| 26 | * @param {DeterministicChunkIdsPluginOptions} [options] options
|
---|
| 27 | */
|
---|
| 28 | constructor(options = {}) {
|
---|
| 29 | this.options = options;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Apply the plugin
|
---|
| 34 | * @param {Compiler} compiler the compiler instance
|
---|
| 35 | * @returns {void}
|
---|
| 36 | */
|
---|
| 37 | apply(compiler) {
|
---|
| 38 | compiler.hooks.compilation.tap(
|
---|
| 39 | "DeterministicChunkIdsPlugin",
|
---|
| 40 | compilation => {
|
---|
| 41 | compilation.hooks.chunkIds.tap(
|
---|
| 42 | "DeterministicChunkIdsPlugin",
|
---|
| 43 | chunks => {
|
---|
| 44 | const chunkGraph = compilation.chunkGraph;
|
---|
| 45 | const context = this.options.context
|
---|
| 46 | ? this.options.context
|
---|
| 47 | : compiler.context;
|
---|
| 48 | const maxLength = this.options.maxLength || 3;
|
---|
| 49 |
|
---|
| 50 | const compareNatural = compareChunksNatural(chunkGraph);
|
---|
| 51 |
|
---|
| 52 | const usedIds = getUsedChunkIds(compilation);
|
---|
| 53 | assignDeterministicIds(
|
---|
| 54 | Array.from(chunks).filter(chunk => chunk.id === null),
|
---|
| 55 | chunk =>
|
---|
| 56 | getFullChunkName(chunk, chunkGraph, context, compiler.root),
|
---|
| 57 | compareNatural,
|
---|
| 58 | (chunk, id) => {
|
---|
| 59 | const size = usedIds.size;
|
---|
| 60 | usedIds.add(`${id}`);
|
---|
| 61 | if (size === usedIds.size) return false;
|
---|
| 62 | chunk.id = id;
|
---|
| 63 | chunk.ids = [id];
|
---|
| 64 | return true;
|
---|
| 65 | },
|
---|
| 66 | [10 ** maxLength],
|
---|
| 67 | 10,
|
---|
| 68 | usedIds.size
|
---|
| 69 | );
|
---|
| 70 | }
|
---|
| 71 | );
|
---|
| 72 | }
|
---|
| 73 | );
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | module.exports = DeterministicChunkIdsPlugin;
|
---|