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

Method apply

lib/ids/SyncModuleIdsPlugin.js:44–160  ·  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

42 * @returns {void}
43 */
44 apply(compiler) {
45 /** @type {Map<string, ModuleId>} */
46 let data;
47 let dataChanged = false;
48
49 const readAndWrite =
50 !this.options.mode ||
51 this.options.mode === "merge" ||
52 this.options.mode === "update";
53
54 const needRead = readAndWrite || this.options.mode === "read";
55 const needWrite = readAndWrite || this.options.mode === "create";
56 const needPrune = this.options.mode === "update";
57
58 if (needRead) {
59 compiler.hooks.readRecords.tapAsync(plugin, (callback) => {
60 const fs =
61 /** @type {IntermediateFileSystem} */
62 (compiler.intermediateFileSystem);
63 fs.readFile(this.options.path, (err, buffer) => {
64 if (err) {
65 if (err.code !== "ENOENT") {
66 return callback(err);
67 }
68 return callback();
69 }
70 /** @type {JSONContent} */
71 const json = JSON.parse(/** @type {Buffer} */ (buffer).toString());
72 /** @type {Map<string, string | number | null>} */
73 data = new Map();
74 for (const key of Object.keys(json)) {
75 data.set(key, json[key]);
76 }
77 dataChanged = false;
78 return callback();
79 });
80 });
81 }
82 if (needWrite) {
83 compiler.hooks.emitRecords.tapAsync(plugin, (callback) => {
84 if (!data || !dataChanged) return callback();
85 /** @type {JSONContent} */
86 const json = {};
87 const sorted = [...data].sort(([a], [b]) => (a < b ? -1 : 1));
88 for (const [key, value] of sorted) {
89 json[key] = value;
90 }
91 const fs =
92 /** @type {IntermediateFileSystem} */
93 (compiler.intermediateFileSystem);
94 fs.writeFile(this.options.path, JSON.stringify(json), callback);
95 });
96 }
97 compiler.hooks.thisCompilation.tap(plugin, (compilation) => {
98 const associatedObjectForCache = compiler.root;
99 const context = this.options.context || compiler.context;
100 const test = this.options.test || (() => true);
101 if (needRead) {

Callers

nothing calls this directly

Calls 15

readFileMethod · 0.80
sortMethod · 0.80
tapMethod · 0.80
setModuleIdMethod · 0.80
getModuleIdMethod · 0.80
keysMethod · 0.65
callbackFunction · 0.50
testFunction · 0.50
parseMethod · 0.45
toStringMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected