MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/ids/OccurrenceChunkIdsPlugin.js:32–88  ·  view source on GitHub ↗

* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

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}

Callers

nothing calls this directly

Calls 10

compareChunksNaturalFunction · 0.85
assignAscendingChunkIdsFunction · 0.85
tapMethod · 0.80
validateMethod · 0.80
sortMethod · 0.80
getNumberOfGroupsMethod · 0.80
requireFunction · 0.50
isInitialMethod · 0.45
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected