registerExternalSerializer.js 7.5 KB

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