[79a0317] | 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 { find } = require("../util/SetHelpers");
|
---|
| 9 | const {
|
---|
| 10 | compareModulesByPreOrderIndexOrIdentifier,
|
---|
| 11 | compareModulesByPostOrderIndexOrIdentifier
|
---|
| 12 | } = require("../util/comparators");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @typedef {object} ChunkModuleIdRangePluginOptions
|
---|
| 18 | * @property {string} name the chunk name
|
---|
| 19 | * @property {("index" | "index2" | "preOrderIndex" | "postOrderIndex")=} order order
|
---|
| 20 | * @property {number=} start start id
|
---|
| 21 | * @property {number=} end end id
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | class ChunkModuleIdRangePlugin {
|
---|
| 25 | /**
|
---|
| 26 | * @param {ChunkModuleIdRangePluginOptions} options options object
|
---|
| 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 | const options = this.options;
|
---|
| 39 | compiler.hooks.compilation.tap("ChunkModuleIdRangePlugin", compilation => {
|
---|
| 40 | const moduleGraph = compilation.moduleGraph;
|
---|
| 41 | compilation.hooks.moduleIds.tap("ChunkModuleIdRangePlugin", modules => {
|
---|
| 42 | const chunkGraph = compilation.chunkGraph;
|
---|
| 43 | const chunk = find(
|
---|
| 44 | compilation.chunks,
|
---|
| 45 | chunk => chunk.name === options.name
|
---|
| 46 | );
|
---|
| 47 | if (!chunk) {
|
---|
| 48 | throw new Error(
|
---|
| 49 | `ChunkModuleIdRangePlugin: Chunk with name '${options.name}"' was not found`
|
---|
| 50 | );
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | let chunkModules;
|
---|
| 54 | if (options.order) {
|
---|
| 55 | let cmpFn;
|
---|
| 56 | switch (options.order) {
|
---|
| 57 | case "index":
|
---|
| 58 | case "preOrderIndex":
|
---|
| 59 | cmpFn = compareModulesByPreOrderIndexOrIdentifier(moduleGraph);
|
---|
| 60 | break;
|
---|
| 61 | case "index2":
|
---|
| 62 | case "postOrderIndex":
|
---|
| 63 | cmpFn = compareModulesByPostOrderIndexOrIdentifier(moduleGraph);
|
---|
| 64 | break;
|
---|
| 65 | default:
|
---|
| 66 | throw new Error(
|
---|
| 67 | "ChunkModuleIdRangePlugin: unexpected value of order"
|
---|
| 68 | );
|
---|
| 69 | }
|
---|
| 70 | chunkModules = chunkGraph.getOrderedChunkModules(chunk, cmpFn);
|
---|
| 71 | } else {
|
---|
| 72 | chunkModules = Array.from(modules)
|
---|
| 73 | .filter(m => chunkGraph.isModuleInChunk(m, chunk))
|
---|
| 74 | .sort(compareModulesByPreOrderIndexOrIdentifier(moduleGraph));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | let currentId = options.start || 0;
|
---|
| 78 | for (let i = 0; i < chunkModules.length; i++) {
|
---|
| 79 | const m = chunkModules[i];
|
---|
| 80 | if (m.needId && chunkGraph.getModuleId(m) === null) {
|
---|
| 81 | chunkGraph.setModuleId(m, currentId++);
|
---|
| 82 | }
|
---|
| 83 | if (options.end && currentId > options.end) break;
|
---|
| 84 | }
|
---|
| 85 | });
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | module.exports = ChunkModuleIdRangePlugin;
|
---|