registerExternalSerializer.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. const startEndBuffer = [];
  158. for (let i = 0; i < len; i++) {
  159. startEndBuffer.push(read(), read());
  160. }
  161. let j = 0;
  162. for (let i = 0; i < len; i++) {
  163. source.replace(
  164. startEndBuffer[j++],
  165. startEndBuffer[j++],
  166. read(),
  167. read()
  168. );
  169. }
  170. return source;
  171. }
  172. })()
  173. );
  174. register(
  175. OriginalSource,
  176. CURRENT_MODULE,
  177. "webpack-sources/OriginalSource",
  178. new (class OriginalSourceSerializer {
  179. /**
  180. * @param {OriginalSource} source the original source to be serialized
  181. * @param {ObjectSerializerContext} context context
  182. * @returns {void}
  183. */
  184. serialize(source, { write }) {
  185. write(source.buffer());
  186. write(source.getName());
  187. }
  188. /**
  189. * @param {ObjectDeserializerContext} context context
  190. * @returns {OriginalSource} original source
  191. */
  192. deserialize({ read }) {
  193. const buffer = read();
  194. const name = read();
  195. return new OriginalSource(buffer, name);
  196. }
  197. })()
  198. );
  199. register(
  200. SourceLocation,
  201. CURRENT_MODULE,
  202. "acorn/SourceLocation",
  203. new (class SourceLocationSerializer {
  204. /**
  205. * @param {SourceLocation} loc the location to be serialized
  206. * @param {ObjectSerializerContext} context context
  207. * @returns {void}
  208. */
  209. serialize(loc, { write }) {
  210. write(loc.start.line);
  211. write(loc.start.column);
  212. write(loc.end.line);
  213. write(loc.end.column);
  214. }
  215. /**
  216. * @param {ObjectDeserializerContext} context context
  217. * @returns {RealDependencyLocation} location
  218. */
  219. deserialize({ read }) {
  220. return {
  221. start: {
  222. line: read(),
  223. column: read()
  224. },
  225. end: {
  226. line: read(),
  227. column: read()
  228. }
  229. };
  230. }
  231. })()
  232. );
  233. register(
  234. Position,
  235. CURRENT_MODULE,
  236. "acorn/Position",
  237. new (class PositionSerializer {
  238. /**
  239. * @param {Position} pos the position to be serialized
  240. * @param {ObjectSerializerContext} context context
  241. * @returns {void}
  242. */
  243. serialize(pos, { write }) {
  244. write(pos.line);
  245. write(pos.column);
  246. }
  247. /**
  248. * @param {ObjectDeserializerContext} context context
  249. * @returns {SourcePosition} position
  250. */
  251. deserialize({ read }) {
  252. return {
  253. line: read(),
  254. column: read()
  255. };
  256. }
  257. })()
  258. );
  259. register(
  260. SourceMapSource,
  261. CURRENT_MODULE,
  262. "webpack-sources/SourceMapSource",
  263. new (class SourceMapSourceSerializer {
  264. /**
  265. * @param {SourceMapSource} source the source map source to be serialized
  266. * @param {ObjectSerializerContext} context context
  267. * @returns {void}
  268. */
  269. serialize(source, { write }) {
  270. write(source.getArgsAsBuffers());
  271. }
  272. /**
  273. * @param {ObjectDeserializerContext} context context
  274. * @returns {SourceMapSource} source source map source
  275. */
  276. deserialize({ read }) {
  277. // @ts-expect-error
  278. return new SourceMapSource(...read());
  279. }
  280. })()
  281. );
  282. register(
  283. ValidationError,
  284. CURRENT_MODULE,
  285. "schema-utils/ValidationError",
  286. new (class ValidationErrorSerializer {
  287. /**
  288. * @param {ValidationError} error the source map source to be serialized
  289. * @param {ObjectSerializerContext} context context
  290. * @returns {void}
  291. */
  292. serialize(error, { write }) {
  293. write(error.errors);
  294. write(error.schema);
  295. write({
  296. name: error.headerName,
  297. baseDataPath: error.baseDataPath,
  298. postFormatter: error.postFormatter
  299. });
  300. }
  301. /**
  302. * @param {ObjectDeserializerContext} context context
  303. * @returns {ValidationError} error
  304. */
  305. deserialize({ read }) {
  306. return new ValidationError(read(), read(), read());
  307. }
  308. })()
  309. );