* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 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) { |
nothing calls this directly
no test coverage detected