source: imaps-frontend/node_modules/webpack/lib/ids/SyncModuleIdsPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { WebpackError } = require("..");
9const { getUsedModuleIdsAndModules } = require("./IdHelpers");
10
11/** @typedef {import("../Compiler")} Compiler */
12/** @typedef {import("../Module")} Module */
13/** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
14
15const plugin = "SyncModuleIdsPlugin";
16
17class SyncModuleIdsPlugin {
18 /**
19 * @param {object} options options
20 * @param {string} options.path path to file
21 * @param {string=} options.context context for module names
22 * @param {function(Module): boolean} options.test selector for modules
23 * @param {"read" | "create" | "merge" | "update"=} options.mode operation mode (defaults to merge)
24 */
25 constructor({ path, context, test, mode }) {
26 this._path = path;
27 this._context = context;
28 this._test = test || (() => true);
29 const readAndWrite = !mode || mode === "merge" || mode === "update";
30 this._read = readAndWrite || mode === "read";
31 this._write = readAndWrite || mode === "create";
32 this._prune = mode === "update";
33 }
34
35 /**
36 * Apply the plugin
37 * @param {Compiler} compiler the compiler instance
38 * @returns {void}
39 */
40 apply(compiler) {
41 /** @type {Map<string, string | number>} */
42 let data;
43 let dataChanged = false;
44 if (this._read) {
45 compiler.hooks.readRecords.tapAsync(plugin, callback => {
46 const fs =
47 /** @type {IntermediateFileSystem} */
48 (compiler.intermediateFileSystem);
49 fs.readFile(this._path, (err, buffer) => {
50 if (err) {
51 if (err.code !== "ENOENT") {
52 return callback(err);
53 }
54 return callback();
55 }
56 const json = JSON.parse(/** @type {Buffer} */ (buffer).toString());
57 data = new Map();
58 for (const key of Object.keys(json)) {
59 data.set(key, json[key]);
60 }
61 dataChanged = false;
62 return callback();
63 });
64 });
65 }
66 if (this._write) {
67 compiler.hooks.emitRecords.tapAsync(plugin, callback => {
68 if (!data || !dataChanged) return callback();
69 /** @type {{[key: string]: string | number}} */
70 const json = {};
71 const sorted = Array.from(data).sort(([a], [b]) => (a < b ? -1 : 1));
72 for (const [key, value] of sorted) {
73 json[key] = value;
74 }
75 const fs =
76 /** @type {IntermediateFileSystem} */
77 (compiler.intermediateFileSystem);
78 fs.writeFile(this._path, JSON.stringify(json), callback);
79 });
80 }
81 compiler.hooks.thisCompilation.tap(plugin, compilation => {
82 const associatedObjectForCache = compiler.root;
83 const context = this._context || compiler.context;
84 if (this._read) {
85 compilation.hooks.reviveModules.tap(plugin, (_1, _2) => {
86 if (!data) return;
87 const { chunkGraph } = compilation;
88 const [usedIds, modules] = getUsedModuleIdsAndModules(
89 compilation,
90 this._test
91 );
92 for (const module of modules) {
93 const name = module.libIdent({
94 context,
95 associatedObjectForCache
96 });
97 if (!name) continue;
98 const id = data.get(name);
99 const idAsString = `${id}`;
100 if (usedIds.has(idAsString)) {
101 const err = new WebpackError(
102 `SyncModuleIdsPlugin: Unable to restore id '${id}' from '${this._path}' as it's already used.`
103 );
104 err.module = module;
105 compilation.errors.push(err);
106 }
107 chunkGraph.setModuleId(module, /** @type {string | number} */ (id));
108 usedIds.add(idAsString);
109 }
110 });
111 }
112 if (this._write) {
113 compilation.hooks.recordModules.tap(plugin, modules => {
114 const { chunkGraph } = compilation;
115 let oldData = data;
116 if (!oldData) {
117 oldData = data = new Map();
118 } else if (this._prune) {
119 data = new Map();
120 }
121 for (const module of modules) {
122 if (this._test(module)) {
123 const name = module.libIdent({
124 context,
125 associatedObjectForCache
126 });
127 if (!name) continue;
128 const id = chunkGraph.getModuleId(module);
129 if (id === null) continue;
130 const oldId = oldData.get(name);
131 if (oldId !== id) {
132 dataChanged = true;
133 } else if (data === oldData) {
134 continue;
135 }
136 data.set(name, id);
137 }
138 }
139 if (data.size !== oldData.size) dataChanged = true;
140 });
141 }
142 });
143 }
144}
145
146module.exports = SyncModuleIdsPlugin;
Note: See TracBrowser for help on using the repository browser.