registerExternalSerializer.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { register } = require("./serialization");
  7. const Position = /** @type {TODO} */ (require("acorn")).Position;
  8. const SourceLocation = require("acorn").SourceLocation;
  9. const ValidationError = require("schema-utils/dist/ValidationError").default;
  10. const {
  11. CachedSource,
  12. ConcatSource,
  13. OriginalSource,
  14. PrefixSource,
  15. RawSource,
  16. ReplaceSource,
  17. SourceMapSource
  18. } = require("webpack-sources");
  19. /** @typedef {import("acorn").Position} Position */
  20. /** @typedef {import("../Dependency").RealDependencyLocation} RealDependencyLocation */
  21. /** @typedef {import("../Dependency").SourcePosition} SourcePosition */
  22. /** @typedef {import("./serialization").ObjectDeserializerContext} ObjectDeserializerContext */
  23. /** @typedef {import("./serialization").ObjectSerializerContext} ObjectSerializerContext */
  24. /** @typedef {ObjectSerializerContext & { writeLazy?: (any) => void }} WebpackObjectSerializerContext */
  25. const CURRENT_MODULE = "webpack/lib/util/registerExternalSerializer";
  26. register(
  27. CachedSource,
  28. CURRENT_MODULE,
  29. "webpack-sources/CachedSource",
  30. new (class CachedSourceSerializer {
  31. /**
  32. * @param {CachedSource} source the cached source to be serialized
  33. * @param {WebpackObjectSerializerContext} 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. * @param {ObjectDeserializerContext} context context
  46. * @returns {CachedSource} cached source
  47. */
  48. deserialize({ read }) {
  49. const source = read();
  50. const cachedData = read();
  51. return new CachedSource(source, cachedData);
  52. }
  53. })()
  54. );
  55. register(
  56. RawSource,
  57. CURRENT_MODULE,
  58. "webpack-sources/RawSource",
  59. new (class RawSourceSerializer {
  60. /**
  61. * @param {RawSource} source the raw source to be serialized
  62. * @param {WebpackObjectSerializerContext} context context
  63. * @returns {void}
  64. */
  65. serialize(source, { write }) {
  66. write(source.buffer());
  67. write(!source.isBuffer());
  68. }
  69. /**
  70. * @param {ObjectDeserializerContext} context context
  71. * @returns {RawSource} raw source
  72. */
  73. deserialize({ read }) {
  74. const source = read();
  75. const convertToString = read();
  76. return new RawSource(source, convertToString);
  77. }
  78. })()
  79. );
  80. register(
  81. ConcatSource,
  82. CURRENT_MODULE,
  83. "webpack-sources/ConcatSource",
  84. new (class ConcatSourceSerializer {
  85. /**
  86. * @param {ConcatSource} source the concat source to be serialized
  87. * @param {WebpackObjectSerializerContext} context context
  88. * @returns {void}
  89. */
  90. serialize(source, { write }) {
  91. write(source.getChildren());
  92. }
  93. /**
  94. * @param {ObjectDeserializerContext} context context
  95. * @returns {ConcatSource} concat source
  96. */
  97. deserialize({ read }) {
  98. const source = new ConcatSource();
  99. source.addAllSkipOptimizing(read());
  100. return source;
  101. }
  102. })()
  103. );
  104. register(
  105. PrefixSource,
  106. CURRENT_MODULE,
  107. "webpack-sources/PrefixSource",
  108. new (class PrefixSourceSerializer {
  109. /**
  110. * @param {PrefixSource} source the prefix source to be serialized
  111. * @param {WebpackObjectSerializerContext} context context
  112. * @returns {void}
  113. */
  114. serialize(source, { write }) {
  115. write(source.getPrefix());
  116. write(source.original());
  117. }
  118. /**
  119. * @param {ObjectDeserializerContext} context context
  120. * @returns {PrefixSource} prefix source
  121. */
  122. deserialize({ read }) {
  123. return new PrefixSource(read(), read());
  124. }
  125. })()
  126. );
  127. register(
  128. ReplaceSource,
  129. CURRENT_MODULE,
  130. "webpack-sources/ReplaceSource",
  131. new (class ReplaceSourceSerializer {
  132. /**
  133. * @param {ReplaceSource} source the replace source to be serialized
  134. * @param {WebpackObjectSerializerContext} context context
  135. * @returns {void}
  136. */
  137. serialize(source, { write }) {
  138. write(source.original());
  139. write(source.getName());
  140. const replacements = source.getReplacements();
  141. write(replacements.length);
  142. for (const repl of replacements) {
  143. write(repl.start);
  144. write(repl.end);
  145. }
  146. for (const repl of replacements) {
  147. write(repl.content);
  148. write(repl.name);
  149. }
  150. }
  151. /**
  152. * @param {ObjectDeserializerContext} context context
  153. * @returns {ReplaceSource} replace source
  154. */
  155. deserialize({ read }) {
  156. const source = new ReplaceSource(read(), read());
  157. const len = read();
  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 {WebpackObjectSerializerContext} 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 {WebpackObjectSerializerContext} 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 {WebpackObjectSerializerContext} 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 {WebpackObjectSerializerContext} 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. // TODO error should be ValidationError, but this fails the type checks
  289. /**
  290. * @param {TODO} error the source map source to be serialized
  291. * @param {WebpackObjectSerializerContext} context context
  292. * @returns {void}
  293. */
  294. serialize(error, { write }) {
  295. write(error.errors);
  296. write(error.schema);
  297. write({
  298. name: error.headerName,
  299. baseDataPath: error.baseDataPath,
  300. postFormatter: error.postFormatter
  301. });
  302. }
  303. /**
  304. * @param {ObjectDeserializerContext} context context
  305. * @returns {TODO} error
  306. */
  307. deserialize({ read }) {
  308. return new ValidationError(read(), read(), read());
  309. }
  310. })()
  311. );