马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const { register } = require("./serialization");
 
const Position = /** @type {TODO} */ (require("acorn")).Position;
const SourceLocation = require("acorn").SourceLocation;
const ValidationError = require("schema-utils/dist/ValidationError").default;
const {
    CachedSource,
    ConcatSource,
    OriginalSource,
    PrefixSource,
    RawSource,
    ReplaceSource,
    SourceMapSource
} = require("webpack-sources");
 
/** @typedef {import("acorn").Position} Position */
/** @typedef {import("../Dependency").RealDependencyLocation} RealDependencyLocation */
/** @typedef {import("../Dependency").SourcePosition} SourcePosition */
/** @typedef {import("./serialization").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization").ObjectSerializerContext} ObjectSerializerContext */
 
/** @typedef {ObjectSerializerContext & { writeLazy?: (any) => void }} WebpackObjectSerializerContext */
 
const CURRENT_MODULE = "webpack/lib/util/registerExternalSerializer";
 
register(
    CachedSource,
    CURRENT_MODULE,
    "webpack-sources/CachedSource",
    new (class CachedSourceSerializer {
        /**
         * @param {CachedSource} source the cached source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write, writeLazy }) {
            if (writeLazy) {
                writeLazy(source.originalLazy());
            } else {
                write(source.original());
            }
            write(source.getCachedData());
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {CachedSource} cached source
         */
        deserialize({ read }) {
            const source = read();
            const cachedData = read();
            return new CachedSource(source, cachedData);
        }
    })()
);
 
register(
    RawSource,
    CURRENT_MODULE,
    "webpack-sources/RawSource",
    new (class RawSourceSerializer {
        /**
         * @param {RawSource} source the raw source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write }) {
            write(source.buffer());
            write(!source.isBuffer());
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {RawSource} raw source
         */
        deserialize({ read }) {
            const source = read();
            const convertToString = read();
            return new RawSource(source, convertToString);
        }
    })()
);
 
register(
    ConcatSource,
    CURRENT_MODULE,
    "webpack-sources/ConcatSource",
    new (class ConcatSourceSerializer {
        /**
         * @param {ConcatSource} source the concat source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write }) {
            write(source.getChildren());
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {ConcatSource} concat source
         */
        deserialize({ read }) {
            const source = new ConcatSource();
            source.addAllSkipOptimizing(read());
            return source;
        }
    })()
);
 
register(
    PrefixSource,
    CURRENT_MODULE,
    "webpack-sources/PrefixSource",
    new (class PrefixSourceSerializer {
        /**
         * @param {PrefixSource} source the prefix source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write }) {
            write(source.getPrefix());
            write(source.original());
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {PrefixSource} prefix source
         */
        deserialize({ read }) {
            return new PrefixSource(read(), read());
        }
    })()
);
 
register(
    ReplaceSource,
    CURRENT_MODULE,
    "webpack-sources/ReplaceSource",
    new (class ReplaceSourceSerializer {
        /**
         * @param {ReplaceSource} source the replace source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write }) {
            write(source.original());
            write(source.getName());
            const replacements = source.getReplacements();
            write(replacements.length);
            for (const repl of replacements) {
                write(repl.start);
                write(repl.end);
            }
            for (const repl of replacements) {
                write(repl.content);
                write(repl.name);
            }
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {ReplaceSource} replace source
         */
        deserialize({ read }) {
            const source = new ReplaceSource(read(), read());
            const len = read();
            const startEndBuffer = [];
            for (let i = 0; i < len; i++) {
                startEndBuffer.push(read(), read());
            }
            let j = 0;
            for (let i = 0; i < len; i++) {
                source.replace(
                    startEndBuffer[j++],
                    startEndBuffer[j++],
                    read(),
                    read()
                );
            }
            return source;
        }
    })()
);
 
register(
    OriginalSource,
    CURRENT_MODULE,
    "webpack-sources/OriginalSource",
    new (class OriginalSourceSerializer {
        /**
         * @param {OriginalSource} source the original source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write }) {
            write(source.buffer());
            write(source.getName());
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {OriginalSource} original source
         */
        deserialize({ read }) {
            const buffer = read();
            const name = read();
            return new OriginalSource(buffer, name);
        }
    })()
);
 
register(
    SourceLocation,
    CURRENT_MODULE,
    "acorn/SourceLocation",
    new (class SourceLocationSerializer {
        /**
         * @param {SourceLocation} loc the location to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(loc, { write }) {
            write(loc.start.line);
            write(loc.start.column);
            write(loc.end.line);
            write(loc.end.column);
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {RealDependencyLocation} location
         */
        deserialize({ read }) {
            return {
                start: {
                    line: read(),
                    column: read()
                },
                end: {
                    line: read(),
                    column: read()
                }
            };
        }
    })()
);
 
register(
    Position,
    CURRENT_MODULE,
    "acorn/Position",
    new (class PositionSerializer {
        /**
         * @param {Position} pos the position to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(pos, { write }) {
            write(pos.line);
            write(pos.column);
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {SourcePosition} position
         */
        deserialize({ read }) {
            return {
                line: read(),
                column: read()
            };
        }
    })()
);
 
register(
    SourceMapSource,
    CURRENT_MODULE,
    "webpack-sources/SourceMapSource",
    new (class SourceMapSourceSerializer {
        /**
         * @param {SourceMapSource} source the source map source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(source, { write }) {
            write(source.getArgsAsBuffers());
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {SourceMapSource} source source map source
         */
        deserialize({ read }) {
            // @ts-expect-error
            return new SourceMapSource(...read());
        }
    })()
);
 
register(
    ValidationError,
    CURRENT_MODULE,
    "schema-utils/ValidationError",
    new (class ValidationErrorSerializer {
        // TODO error should be ValidationError, but this fails the type checks
        /**
         * @param {TODO} error the source map source to be serialized
         * @param {WebpackObjectSerializerContext} context context
         * @returns {void}
         */
        serialize(error, { write }) {
            write(error.errors);
            write(error.schema);
            write({
                name: error.headerName,
                baseDataPath: error.baseDataPath,
                postFormatter: error.postFormatter
            });
        }
 
        /**
         * @param {ObjectDeserializerContext} context context
         * @returns {TODO} error
         */
        deserialize({ read }) {
            return new ValidationError(read(), read(), read());
        }
    })()
);