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