| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { compareChunksNatural } = require("../util/comparators");
|
|---|
| 9 | const { assignAscendingChunkIds } = require("./IdHelpers");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../../declarations/plugins/ids/OccurrenceChunkIdsPlugin").OccurrenceChunkIdsPluginOptions} OccurrenceChunkIdsPluginOptions */
|
|---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 13 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 14 |
|
|---|
| 15 | const PLUGIN_NAME = "OccurrenceChunkIdsPlugin";
|
|---|
| 16 |
|
|---|
| 17 | class OccurrenceChunkIdsPlugin {
|
|---|
| 18 | /**
|
|---|
| 19 | * Creates an instance of OccurrenceChunkIdsPlugin.
|
|---|
| 20 | * @param {OccurrenceChunkIdsPluginOptions=} options options object
|
|---|
| 21 | */
|
|---|
| 22 | constructor(options = {}) {
|
|---|
| 23 | /** @type {OccurrenceChunkIdsPluginOptions} */
|
|---|
| 24 | this.options = options;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 29 | * @param {Compiler} compiler the compiler instance
|
|---|
| 30 | * @returns {void}
|
|---|
| 31 | */
|
|---|
| 32 | apply(compiler) {
|
|---|
| 33 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 34 | compiler.validate(
|
|---|
| 35 | () =>
|
|---|
| 36 | require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json"),
|
|---|
| 37 | this.options,
|
|---|
| 38 | {
|
|---|
| 39 | name: "Occurrence Order Chunk Ids Plugin",
|
|---|
| 40 | baseDataPath: "options"
|
|---|
| 41 | },
|
|---|
| 42 | (options) =>
|
|---|
| 43 | require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.check")(
|
|---|
| 44 | options
|
|---|
| 45 | )
|
|---|
| 46 | );
|
|---|
| 47 | });
|
|---|
| 48 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 49 | compilation.hooks.chunkIds.tap(PLUGIN_NAME, (chunks) => {
|
|---|
| 50 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 51 |
|
|---|
| 52 | /** @type {Map<Chunk, number>} */
|
|---|
| 53 | const occursInInitialChunksMap = new Map();
|
|---|
| 54 |
|
|---|
| 55 | const compareNatural = compareChunksNatural(chunkGraph);
|
|---|
| 56 |
|
|---|
| 57 | for (const c of chunks) {
|
|---|
| 58 | let occurs = 0;
|
|---|
| 59 | for (const chunkGroup of c.groupsIterable) {
|
|---|
| 60 | for (const parent of chunkGroup.parentsIterable) {
|
|---|
| 61 | if (parent.isInitial()) occurs++;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | occursInInitialChunksMap.set(c, occurs);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** @type {Chunk[]} */
|
|---|
| 68 | const chunksInOccurrenceOrder = [...chunks].sort((a, b) => {
|
|---|
| 69 | if (this.options.prioritiseInitial) {
|
|---|
| 70 | const aEntryOccurs =
|
|---|
| 71 | /** @type {number} */
|
|---|
| 72 | (occursInInitialChunksMap.get(a));
|
|---|
| 73 | const bEntryOccurs =
|
|---|
| 74 | /** @type {number} */
|
|---|
| 75 | (occursInInitialChunksMap.get(b));
|
|---|
| 76 | if (aEntryOccurs > bEntryOccurs) return -1;
|
|---|
| 77 | if (aEntryOccurs < bEntryOccurs) return 1;
|
|---|
| 78 | }
|
|---|
| 79 | const aOccurs = a.getNumberOfGroups();
|
|---|
| 80 | const bOccurs = b.getNumberOfGroups();
|
|---|
| 81 | if (aOccurs > bOccurs) return -1;
|
|---|
| 82 | if (aOccurs < bOccurs) return 1;
|
|---|
| 83 | return compareNatural(a, b);
|
|---|
| 84 | });
|
|---|
| 85 | assignAscendingChunkIds(chunksInOccurrenceOrder, compilation);
|
|---|
| 86 | });
|
|---|
| 87 | });
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | module.exports = OccurrenceChunkIdsPlugin;
|
|---|