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

Method apply

lib/RecordIdsPlugin.js:62–222  ·  view source on GitHub ↗

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

(compiler)

Source from the content-addressed store, hash-verified

60 * @returns {void}
61 */
62 apply(compiler) {
63 const portableIds = this.options.portableIds;
64
65 const makePathsRelative =
66 identifierUtils.makePathsRelative.bindContextCache(
67 compiler.context,
68 compiler.root
69 );
70
71 /**
72 * Gets module identifier.
73 * @param {Module} module the module
74 * @returns {string} the (portable) identifier
75 */
76 const getModuleIdentifier = (module) => {
77 if (portableIds) {
78 return makePathsRelative(module.identifier());
79 }
80 return module.identifier();
81 };
82
83 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
84 compilation.hooks.recordModules.tap(PLUGIN_NAME, (modules, records) => {
85 const chunkGraph = compilation.chunkGraph;
86 if (!records.modules) records.modules = {};
87 if (!records.modules.byIdentifier) records.modules.byIdentifier = {};
88 /** @type {UsedIds} */
89 const usedIds = new Set();
90 for (const module of modules) {
91 const moduleId = chunkGraph.getModuleId(module);
92 if (typeof moduleId !== "number") continue;
93 const identifier = getModuleIdentifier(module);
94 records.modules.byIdentifier[identifier] = moduleId;
95 usedIds.add(moduleId);
96 }
97 records.modules.usedIds = [...usedIds].sort(compareNumbers);
98 });
99 compilation.hooks.reviveModules.tap(PLUGIN_NAME, (modules, records) => {
100 if (!records.modules) return;
101 if (records.modules.byIdentifier) {
102 const chunkGraph = compilation.chunkGraph;
103 /** @type {UsedIds} */
104 const usedIds = new Set();
105 for (const module of modules) {
106 const moduleId = chunkGraph.getModuleId(module);
107 if (moduleId !== null) continue;
108 const identifier = getModuleIdentifier(module);
109 const id = records.modules.byIdentifier[identifier];
110 if (id === undefined) continue;
111 if (usedIds.has(id)) continue;
112 usedIds.add(id);
113 chunkGraph.setModuleId(module, id);
114 }
115 }
116 if (Array.isArray(records.modules.usedIds)) {
117 compilation.usedModuleIds = new Set(records.modules.usedIds);
118 }
119 });

Callers

nothing calls this directly

Calls 7

tapMethod · 0.80
getModuleIdMethod · 0.80
sortMethod · 0.80
setModuleIdMethod · 0.80
isArrayMethod · 0.80
addMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected