registerExternalSerializer.js 8.8 KB

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