[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { register } = require("./serialization");
|
---|
| 9 |
|
---|
| 10 | const Position = /** @type {TODO} */ (require("acorn")).Position;
|
---|
| 11 | const SourceLocation = require("acorn").SourceLocation;
|
---|
| 12 | const ValidationError = require("schema-utils/dist/ValidationError").default;
|
---|
| 13 | const {
|
---|
| 14 | CachedSource,
|
---|
| 15 | ConcatSource,
|
---|
| 16 | OriginalSource,
|
---|
| 17 | PrefixSource,
|
---|
| 18 | RawSource,
|
---|
| 19 | ReplaceSource,
|
---|
| 20 | SourceMapSource
|
---|
| 21 | } = require("webpack-sources");
|
---|
| 22 |
|
---|
| 23 | /** @typedef {import("acorn").Position} Position */
|
---|
| 24 | /** @typedef {import("../Dependency").RealDependencyLocation} RealDependencyLocation */
|
---|
| 25 | /** @typedef {import("../Dependency").SourcePosition} SourcePosition */
|
---|
| 26 | /** @typedef {import("./serialization").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 27 | /** @typedef {import("./serialization").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 28 |
|
---|
| 29 | /** @typedef {ObjectSerializerContext & { writeLazy?: (any) => void }} WebpackObjectSerializerContext */
|
---|
| 30 |
|
---|
| 31 | const CURRENT_MODULE = "webpack/lib/util/registerExternalSerializer";
|
---|
| 32 |
|
---|
| 33 | register(
|
---|
| 34 | CachedSource,
|
---|
| 35 | CURRENT_MODULE,
|
---|
| 36 | "webpack-sources/CachedSource",
|
---|
| 37 | new (class CachedSourceSerializer {
|
---|
| 38 | /**
|
---|
| 39 | * @param {CachedSource} source the cached source to be serialized
|
---|
| 40 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 41 | * @returns {void}
|
---|
| 42 | */
|
---|
| 43 | serialize(source, { write, writeLazy }) {
|
---|
| 44 | if (writeLazy) {
|
---|
| 45 | writeLazy(source.originalLazy());
|
---|
| 46 | } else {
|
---|
| 47 | write(source.original());
|
---|
| 48 | }
|
---|
| 49 | write(source.getCachedData());
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * @param {ObjectDeserializerContext} context context
|
---|
| 54 | * @returns {CachedSource} cached source
|
---|
| 55 | */
|
---|
| 56 | deserialize({ read }) {
|
---|
| 57 | const source = read();
|
---|
| 58 | const cachedData = read();
|
---|
| 59 | return new CachedSource(source, cachedData);
|
---|
| 60 | }
|
---|
| 61 | })()
|
---|
| 62 | );
|
---|
| 63 |
|
---|
| 64 | register(
|
---|
| 65 | RawSource,
|
---|
| 66 | CURRENT_MODULE,
|
---|
| 67 | "webpack-sources/RawSource",
|
---|
| 68 | new (class RawSourceSerializer {
|
---|
| 69 | /**
|
---|
| 70 | * @param {RawSource} source the raw source to be serialized
|
---|
| 71 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 72 | * @returns {void}
|
---|
| 73 | */
|
---|
| 74 | serialize(source, { write }) {
|
---|
| 75 | write(source.buffer());
|
---|
| 76 | write(!source.isBuffer());
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @param {ObjectDeserializerContext} context context
|
---|
| 81 | * @returns {RawSource} raw source
|
---|
| 82 | */
|
---|
| 83 | deserialize({ read }) {
|
---|
| 84 | const source = read();
|
---|
| 85 | const convertToString = read();
|
---|
| 86 | return new RawSource(source, convertToString);
|
---|
| 87 | }
|
---|
| 88 | })()
|
---|
| 89 | );
|
---|
| 90 |
|
---|
| 91 | register(
|
---|
| 92 | ConcatSource,
|
---|
| 93 | CURRENT_MODULE,
|
---|
| 94 | "webpack-sources/ConcatSource",
|
---|
| 95 | new (class ConcatSourceSerializer {
|
---|
| 96 | /**
|
---|
| 97 | * @param {ConcatSource} source the concat source to be serialized
|
---|
| 98 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 99 | * @returns {void}
|
---|
| 100 | */
|
---|
| 101 | serialize(source, { write }) {
|
---|
| 102 | write(source.getChildren());
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * @param {ObjectDeserializerContext} context context
|
---|
| 107 | * @returns {ConcatSource} concat source
|
---|
| 108 | */
|
---|
| 109 | deserialize({ read }) {
|
---|
| 110 | const source = new ConcatSource();
|
---|
| 111 | source.addAllSkipOptimizing(read());
|
---|
| 112 | return source;
|
---|
| 113 | }
|
---|
| 114 | })()
|
---|
| 115 | );
|
---|
| 116 |
|
---|
| 117 | register(
|
---|
| 118 | PrefixSource,
|
---|
| 119 | CURRENT_MODULE,
|
---|
| 120 | "webpack-sources/PrefixSource",
|
---|
| 121 | new (class PrefixSourceSerializer {
|
---|
| 122 | /**
|
---|
| 123 | * @param {PrefixSource} source the prefix source to be serialized
|
---|
| 124 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 125 | * @returns {void}
|
---|
| 126 | */
|
---|
| 127 | serialize(source, { write }) {
|
---|
| 128 | write(source.getPrefix());
|
---|
| 129 | write(source.original());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * @param {ObjectDeserializerContext} context context
|
---|
| 134 | * @returns {PrefixSource} prefix source
|
---|
| 135 | */
|
---|
| 136 | deserialize({ read }) {
|
---|
| 137 | return new PrefixSource(read(), read());
|
---|
| 138 | }
|
---|
| 139 | })()
|
---|
| 140 | );
|
---|
| 141 |
|
---|
| 142 | register(
|
---|
| 143 | ReplaceSource,
|
---|
| 144 | CURRENT_MODULE,
|
---|
| 145 | "webpack-sources/ReplaceSource",
|
---|
| 146 | new (class ReplaceSourceSerializer {
|
---|
| 147 | /**
|
---|
| 148 | * @param {ReplaceSource} source the replace source to be serialized
|
---|
| 149 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 150 | * @returns {void}
|
---|
| 151 | */
|
---|
| 152 | serialize(source, { write }) {
|
---|
| 153 | write(source.original());
|
---|
| 154 | write(source.getName());
|
---|
| 155 | const replacements = source.getReplacements();
|
---|
| 156 | write(replacements.length);
|
---|
| 157 | for (const repl of replacements) {
|
---|
| 158 | write(repl.start);
|
---|
| 159 | write(repl.end);
|
---|
| 160 | }
|
---|
| 161 | for (const repl of replacements) {
|
---|
| 162 | write(repl.content);
|
---|
| 163 | write(repl.name);
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * @param {ObjectDeserializerContext} context context
|
---|
| 169 | * @returns {ReplaceSource} replace source
|
---|
| 170 | */
|
---|
| 171 | deserialize({ read }) {
|
---|
| 172 | const source = new ReplaceSource(read(), read());
|
---|
| 173 | const len = read();
|
---|
| 174 | const startEndBuffer = [];
|
---|
| 175 | for (let i = 0; i < len; i++) {
|
---|
| 176 | startEndBuffer.push(read(), read());
|
---|
| 177 | }
|
---|
| 178 | let j = 0;
|
---|
| 179 | for (let i = 0; i < len; i++) {
|
---|
| 180 | source.replace(
|
---|
| 181 | startEndBuffer[j++],
|
---|
| 182 | startEndBuffer[j++],
|
---|
| 183 | read(),
|
---|
| 184 | read()
|
---|
| 185 | );
|
---|
| 186 | }
|
---|
| 187 | return source;
|
---|
| 188 | }
|
---|
| 189 | })()
|
---|
| 190 | );
|
---|
| 191 |
|
---|
| 192 | register(
|
---|
| 193 | OriginalSource,
|
---|
| 194 | CURRENT_MODULE,
|
---|
| 195 | "webpack-sources/OriginalSource",
|
---|
| 196 | new (class OriginalSourceSerializer {
|
---|
| 197 | /**
|
---|
| 198 | * @param {OriginalSource} source the original source to be serialized
|
---|
| 199 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 200 | * @returns {void}
|
---|
| 201 | */
|
---|
| 202 | serialize(source, { write }) {
|
---|
| 203 | write(source.buffer());
|
---|
| 204 | write(source.getName());
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * @param {ObjectDeserializerContext} context context
|
---|
| 209 | * @returns {OriginalSource} original source
|
---|
| 210 | */
|
---|
| 211 | deserialize({ read }) {
|
---|
| 212 | const buffer = read();
|
---|
| 213 | const name = read();
|
---|
| 214 | return new OriginalSource(buffer, name);
|
---|
| 215 | }
|
---|
| 216 | })()
|
---|
| 217 | );
|
---|
| 218 |
|
---|
| 219 | register(
|
---|
| 220 | SourceLocation,
|
---|
| 221 | CURRENT_MODULE,
|
---|
| 222 | "acorn/SourceLocation",
|
---|
| 223 | new (class SourceLocationSerializer {
|
---|
| 224 | /**
|
---|
| 225 | * @param {SourceLocation} loc the location to be serialized
|
---|
| 226 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 227 | * @returns {void}
|
---|
| 228 | */
|
---|
| 229 | serialize(loc, { write }) {
|
---|
| 230 | write(loc.start.line);
|
---|
| 231 | write(loc.start.column);
|
---|
| 232 | write(loc.end.line);
|
---|
| 233 | write(loc.end.column);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * @param {ObjectDeserializerContext} context context
|
---|
| 238 | * @returns {RealDependencyLocation} location
|
---|
| 239 | */
|
---|
| 240 | deserialize({ read }) {
|
---|
| 241 | return {
|
---|
| 242 | start: {
|
---|
| 243 | line: read(),
|
---|
| 244 | column: read()
|
---|
| 245 | },
|
---|
| 246 | end: {
|
---|
| 247 | line: read(),
|
---|
| 248 | column: read()
|
---|
| 249 | }
|
---|
| 250 | };
|
---|
| 251 | }
|
---|
| 252 | })()
|
---|
| 253 | );
|
---|
| 254 |
|
---|
| 255 | register(
|
---|
| 256 | Position,
|
---|
| 257 | CURRENT_MODULE,
|
---|
| 258 | "acorn/Position",
|
---|
| 259 | new (class PositionSerializer {
|
---|
| 260 | /**
|
---|
| 261 | * @param {Position} pos the position to be serialized
|
---|
| 262 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 263 | * @returns {void}
|
---|
| 264 | */
|
---|
| 265 | serialize(pos, { write }) {
|
---|
| 266 | write(pos.line);
|
---|
| 267 | write(pos.column);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /**
|
---|
| 271 | * @param {ObjectDeserializerContext} context context
|
---|
| 272 | * @returns {SourcePosition} position
|
---|
| 273 | */
|
---|
| 274 | deserialize({ read }) {
|
---|
| 275 | return {
|
---|
| 276 | line: read(),
|
---|
| 277 | column: read()
|
---|
| 278 | };
|
---|
| 279 | }
|
---|
| 280 | })()
|
---|
| 281 | );
|
---|
| 282 |
|
---|
| 283 | register(
|
---|
| 284 | SourceMapSource,
|
---|
| 285 | CURRENT_MODULE,
|
---|
| 286 | "webpack-sources/SourceMapSource",
|
---|
| 287 | new (class SourceMapSourceSerializer {
|
---|
| 288 | /**
|
---|
| 289 | * @param {SourceMapSource} source the source map source to be serialized
|
---|
| 290 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 291 | * @returns {void}
|
---|
| 292 | */
|
---|
| 293 | serialize(source, { write }) {
|
---|
| 294 | write(source.getArgsAsBuffers());
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /**
|
---|
| 298 | * @param {ObjectDeserializerContext} context context
|
---|
| 299 | * @returns {SourceMapSource} source source map source
|
---|
| 300 | */
|
---|
| 301 | deserialize({ read }) {
|
---|
| 302 | // @ts-expect-error
|
---|
| 303 | return new SourceMapSource(...read());
|
---|
| 304 | }
|
---|
| 305 | })()
|
---|
| 306 | );
|
---|
| 307 |
|
---|
| 308 | register(
|
---|
| 309 | ValidationError,
|
---|
| 310 | CURRENT_MODULE,
|
---|
| 311 | "schema-utils/ValidationError",
|
---|
| 312 | new (class ValidationErrorSerializer {
|
---|
| 313 | // TODO error should be ValidationError, but this fails the type checks
|
---|
| 314 | /**
|
---|
| 315 | * @param {TODO} error the source map source to be serialized
|
---|
| 316 | * @param {WebpackObjectSerializerContext} context context
|
---|
| 317 | * @returns {void}
|
---|
| 318 | */
|
---|
| 319 | serialize(error, { write }) {
|
---|
| 320 | write(error.errors);
|
---|
| 321 | write(error.schema);
|
---|
| 322 | write({
|
---|
| 323 | name: error.headerName,
|
---|
| 324 | baseDataPath: error.baseDataPath,
|
---|
| 325 | postFormatter: error.postFormatter
|
---|
| 326 | });
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /**
|
---|
| 330 | * @param {ObjectDeserializerContext} context context
|
---|
| 331 | * @returns {TODO} error
|
---|
| 332 | */
|
---|
| 333 | deserialize({ read }) {
|
---|
| 334 | return new ValidationError(read(), read(), read());
|
---|
| 335 | }
|
---|
| 336 | })()
|
---|
| 337 | );
|
---|