source: frontend/node_modules/webpack/lib/ids/ChunkModuleIdRangePlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { find } = require("../util/SetHelpers");
9const {
10 compareModulesByPostOrderIndexOrIdentifier,
11 compareModulesByPreOrderIndexOrIdentifier
12} = require("../util/comparators");
13
14/** @typedef {import("../Compiler")} Compiler */
15/** @typedef {import("../Module")} Module */
16/** @typedef {import("../ChunkGraph").ModuleComparator} ModuleComparator */
17
18/**
19 * Defines the chunk module id range plugin options type used by this module.
20 * @typedef {object} ChunkModuleIdRangePluginOptions
21 * @property {string} name the chunk name
22 * @property {("index" | "index2" | "preOrderIndex" | "postOrderIndex")=} order order
23 * @property {number=} start start id
24 * @property {number=} end end id
25 */
26
27const PLUGIN_NAME = "ChunkModuleIdRangePlugin";
28
29class ChunkModuleIdRangePlugin {
30 /**
31 * Creates an instance of ChunkModuleIdRangePlugin.
32 * @param {ChunkModuleIdRangePluginOptions} options options object
33 */
34 constructor(options) {
35 /** @type {ChunkModuleIdRangePluginOptions} */
36 this.options = options;
37 }
38
39 /**
40 * Applies the plugin by registering its hooks on the compiler.
41 * @param {Compiler} compiler the compiler instance
42 * @returns {void}
43 */
44 apply(compiler) {
45 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
46 const moduleGraph = compilation.moduleGraph;
47 compilation.hooks.moduleIds.tap(PLUGIN_NAME, (modules) => {
48 const chunkGraph = compilation.chunkGraph;
49 const chunk = find(
50 compilation.chunks,
51 (chunk) => chunk.name === this.options.name
52 );
53 if (!chunk) {
54 throw new Error(
55 `${PLUGIN_NAME}: Chunk with name '${this.options.name}"' was not found`
56 );
57 }
58
59 /** @type {Module[]} */
60 let chunkModules;
61 if (this.options.order) {
62 /** @type {ModuleComparator} */
63 let cmpFn;
64 switch (this.options.order) {
65 case "index":
66 case "preOrderIndex":
67 cmpFn = compareModulesByPreOrderIndexOrIdentifier(moduleGraph);
68 break;
69 case "index2":
70 case "postOrderIndex":
71 cmpFn = compareModulesByPostOrderIndexOrIdentifier(moduleGraph);
72 break;
73 default:
74 throw new Error(`${PLUGIN_NAME}: unexpected value of order`);
75 }
76 chunkModules = chunkGraph.getOrderedChunkModules(chunk, cmpFn);
77 } else {
78 chunkModules = [...modules]
79 .filter((m) => chunkGraph.isModuleInChunk(m, chunk))
80 .sort(compareModulesByPreOrderIndexOrIdentifier(moduleGraph));
81 }
82
83 let currentId = this.options.start || 0;
84 for (let i = 0; i < chunkModules.length; i++) {
85 const m = chunkModules[i];
86 if (m.needId && chunkGraph.getModuleId(m) === null) {
87 chunkGraph.setModuleId(m, currentId++);
88 }
89 if (this.options.end && currentId > this.options.end) break;
90 }
91 });
92 });
93 }
94}
95
96module.exports = ChunkModuleIdRangePlugin;
Note: See TracBrowser for help on using the repository browser.