registerExternalSerializer.js 8.9 KB

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