[6a3a178] | 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 createSchemaValidation = require("../util/create-schema-validation");
|
---|
| 10 | const { assignAscendingChunkIds } = require("./IdHelpers");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("../../declarations/plugins/ids/OccurrenceChunkIdsPlugin").OccurrenceChunkIdsPluginOptions} OccurrenceChunkIdsPluginOptions */
|
---|
| 13 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 14 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 15 | /** @typedef {import("../Module")} Module */
|
---|
| 16 |
|
---|
| 17 | const validate = createSchemaValidation(
|
---|
| 18 | require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.check.js"),
|
---|
| 19 | () => require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json"),
|
---|
| 20 | {
|
---|
| 21 | name: "Occurrence Order Chunk Ids Plugin",
|
---|
| 22 | baseDataPath: "options"
|
---|
| 23 | }
|
---|
| 24 | );
|
---|
| 25 |
|
---|
| 26 | class OccurrenceChunkIdsPlugin {
|
---|
| 27 | /**
|
---|
| 28 | * @param {OccurrenceChunkIdsPluginOptions=} options options object
|
---|
| 29 | */
|
---|
| 30 | constructor(options = {}) {
|
---|
| 31 | validate(options);
|
---|
| 32 | this.options = options;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Apply the plugin
|
---|
| 37 | * @param {Compiler} compiler the compiler instance
|
---|
| 38 | * @returns {void}
|
---|
| 39 | */
|
---|
| 40 | apply(compiler) {
|
---|
| 41 | const prioritiseInitial = this.options.prioritiseInitial;
|
---|
| 42 | compiler.hooks.compilation.tap("OccurrenceChunkIdsPlugin", compilation => {
|
---|
| 43 | compilation.hooks.chunkIds.tap("OccurrenceChunkIdsPlugin", chunks => {
|
---|
| 44 | const chunkGraph = compilation.chunkGraph;
|
---|
| 45 |
|
---|
| 46 | /** @type {Map<Chunk, number>} */
|
---|
| 47 | const occursInInitialChunksMap = new Map();
|
---|
| 48 |
|
---|
| 49 | const compareNatural = compareChunksNatural(chunkGraph);
|
---|
| 50 |
|
---|
| 51 | for (const c of chunks) {
|
---|
| 52 | let occurs = 0;
|
---|
| 53 | for (const chunkGroup of c.groupsIterable) {
|
---|
| 54 | for (const parent of chunkGroup.parentsIterable) {
|
---|
| 55 | if (parent.isInitial()) occurs++;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | occursInInitialChunksMap.set(c, occurs);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | const chunksInOccurrenceOrder = Array.from(chunks).sort((a, b) => {
|
---|
| 62 | if (prioritiseInitial) {
|
---|
| 63 | const aEntryOccurs = occursInInitialChunksMap.get(a);
|
---|
| 64 | const bEntryOccurs = occursInInitialChunksMap.get(b);
|
---|
| 65 | if (aEntryOccurs > bEntryOccurs) return -1;
|
---|
| 66 | if (aEntryOccurs < bEntryOccurs) return 1;
|
---|
| 67 | }
|
---|
| 68 | const aOccurs = a.getNumberOfGroups();
|
---|
| 69 | const bOccurs = b.getNumberOfGroups();
|
---|
| 70 | if (aOccurs > bOccurs) return -1;
|
---|
| 71 | if (aOccurs < bOccurs) return 1;
|
---|
| 72 | return compareNatural(a, b);
|
---|
| 73 | });
|
---|
| 74 | assignAscendingChunkIds(chunksInOccurrenceOrder, compilation);
|
---|
| 75 | });
|
---|
| 76 | });
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | module.exports = OccurrenceChunkIdsPlugin;
|
---|