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