serial.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. const relateContext = require('./relate-context');
  2. const pipe = (exports.pipe = (...fns) => ({
  3. freeze(arg, module, extra, methods) {
  4. for (const fn of fns) {
  5. arg = fn.freeze(arg, module, extra, methods);
  6. }
  7. return arg;
  8. },
  9. thaw(arg, frozen, extra, methods) {
  10. for (const fn of fns) {
  11. arg = fn.thaw(arg, frozen, extra, methods);
  12. }
  13. return arg;
  14. },
  15. }));
  16. const serialMap = (exports.map = (keyOp, valueOp) => ({
  17. freeze(arg, module, extra) {
  18. const resolved = [];
  19. for (const key in arg) {
  20. resolved.push([
  21. keyOp.freeze(key, key, extra),
  22. valueOp.freeze(arg[key], arg[key], extra),
  23. ]);
  24. }
  25. return resolved;
  26. },
  27. thaw(arg, frozen, extra) {
  28. const resolved = {};
  29. for (const item of arg) {
  30. const key = keyOp.thaw(item[0], item[0], extra);
  31. const value = valueOp.thaw(item[1], item[1], extra);
  32. resolved[key] = value;
  33. }
  34. return resolved;
  35. },
  36. }));
  37. const contextual = (exports.contextual = fnname => {
  38. const relate = relateContext[`relateNormal${fnname}`];
  39. const context = relateContext[`contextNormal${fnname}`];
  40. return {
  41. freeze: (arg, module, { compiler, compilation }, methods) =>
  42. arg ? relate(compiler || compilation.compiler, arg) : arg,
  43. thaw: (arg, module, { compiler, compilation }, methods) =>
  44. arg ? context(compiler || compilation.compiler, arg) : arg,
  45. };
  46. });
  47. const archetype = (exports.archetype = type => ({
  48. freeze: (arg, module, extra, { freeze }) => freeze(type, null, arg, extra),
  49. thaw: (arg, module, extra, { thaw }) => thaw(type, null, arg, extra),
  50. }));
  51. const mapArchetype = (exports.mapArchetype = type => ({
  52. freeze: (arg, module, extra, { mapFreeze }) =>
  53. mapFreeze(type, null, arg, extra),
  54. thaw: (arg, module, extra, { mapThaw }) => mapThaw(type, null, arg, extra),
  55. }));
  56. const path = (exports.path = contextual('Path'));
  57. const pathArray = (exports.pathArray = contextual('PathArray'));
  58. const pathSet = (exports.pathSet = contextual('PathSet'));
  59. const request = (exports.request = contextual('Request'));
  60. const _loaders = (exports._loaders = contextual('Loaders'));
  61. const loaders = (exports.loaders = {
  62. freeze: _loaders.freeze,
  63. thaw(arg, frozen, extra, methods) {
  64. return _loaders.thaw(arg, frozen, extra, methods).map(loader => {
  65. if (loader.ident) {
  66. let ruleSet =
  67. extra.normalModuleFactory && extra.normalModuleFactory.ruleSet;
  68. if (!ruleSet) {
  69. ruleSet = extra.compiler.__hardSource_ruleSet;
  70. if (!ruleSet) {
  71. const RuleSet = require('webpack/lib/RuleSet');
  72. if (extra.compiler.options.module.defaultRules) {
  73. // webpack 4
  74. ruleSet = extra.compiler.__hardSource_ruleSet = new RuleSet(
  75. extra.compiler.options.module.defaultRules.concat(
  76. extra.compiler.options.module.rules,
  77. ),
  78. );
  79. } else {
  80. // webpack <4
  81. ruleSet = extra.compiler.__hardSource_ruleSet = new RuleSet(
  82. extra.compiler.options.module.rules ||
  83. extra.compiler.options.module.loaders,
  84. );
  85. }
  86. }
  87. }
  88. return {
  89. loader: loader.loader,
  90. ident: loader.ident,
  91. options: ruleSet.findOptionsByIdent(loader.ident),
  92. };
  93. }
  94. return loader;
  95. });
  96. },
  97. });
  98. const regExp = (exports.regExp = {
  99. freeze: arg => (arg ? arg.source : false),
  100. thaw: arg => (arg ? new RegExp(arg) : false),
  101. });
  102. const parser = (exports.parser = archetype('Parser'));
  103. const generator = (exports.generator = archetype('Generator'));
  104. const source = (exports.source = archetype('Source'));
  105. const moduleAssets = (exports.moduleAssets = archetype('ModuleAssets'));
  106. const moduleError = (exports.moduleError = mapArchetype('ModuleError'));
  107. const moduleWarning = (exports.moduleWarning = mapArchetype('ModuleWarning'));
  108. const dependencyBlock = (exports.dependencyBlock = {
  109. freeze: (arg, module, extra, { freeze }) =>
  110. freeze('DependencyBlock', null, arg, extra),
  111. thaw: (arg, module, extra, { thaw }) =>
  112. thaw('DependencyBlock', arg, module, extra),
  113. });
  114. const _null = (exports.null = {
  115. freeze: () => null,
  116. thaw: () => null,
  117. });
  118. const identity = (exports.identity = {
  119. freeze: (arg, module, extra, methods) => arg,
  120. thaw: (arg, module, extra, methods) => arg,
  121. });
  122. const assigned = (exports.assigned = members => ({
  123. freeze(arg, module, extra, methods) {
  124. const out = {};
  125. for (const key in members) {
  126. out[key] = members[key].freeze(arg[key], module, extra, methods);
  127. }
  128. return out;
  129. },
  130. thaw(arg, frozen, extra, methods) {
  131. for (const key in members) {
  132. arg[key] = members[key].thaw(frozen[key], frozen, extra, methods);
  133. }
  134. return arg;
  135. },
  136. }));
  137. const created = (exports.created = members => ({
  138. freeze(arg, module, extra, methods) {
  139. if (!arg) {
  140. return null;
  141. }
  142. const out = {};
  143. for (const key in members) {
  144. out[key] = members[key].freeze(arg[key], module, extra, methods);
  145. }
  146. return out;
  147. },
  148. thaw(arg, frozen, extra, methods) {
  149. if (!arg) {
  150. return null;
  151. }
  152. const out = {};
  153. for (const key in members) {
  154. out[key] = members[key].thaw(arg[key], frozen, extra, methods);
  155. }
  156. return out;
  157. },
  158. }));
  159. const objectAssign = (exports.objectAssign = opts => ({
  160. freeze(arg, module, extra) {
  161. const out = Object.assign({}, arg);
  162. for (const key in opts) {
  163. out[key] = opts[key].freeze(arg[key], arg, extra);
  164. }
  165. return out;
  166. },
  167. thaw(arg, frozen, extra) {
  168. const out = Object.assign({}, arg);
  169. for (const key in opts) {
  170. out[key] = opts[key].thaw(arg[key], arg, extra);
  171. }
  172. return out;
  173. },
  174. }));
  175. const constructed = (exports.constructed = (Type, args) => ({
  176. freeze(arg, module, extra, methods) {
  177. const out = {};
  178. for (const key in args) {
  179. out[key] = args[key].freeze(arg[key], module, extra, methods);
  180. }
  181. return out;
  182. },
  183. thaw(arg, frozen, extra, methods) {
  184. const newArgs = [];
  185. for (const key in args) {
  186. newArgs.push(args[key].thaw(frozen[key], frozen, extra, methods));
  187. }
  188. return new Type(...newArgs);
  189. },
  190. }));
  191. const serial = (exports.serial = (name, stages) => ({
  192. freeze(arg, module, extra, methods) {
  193. const out = {
  194. type: name,
  195. };
  196. for (const key in stages) {
  197. out[key] = stages[key].freeze(module, module, extra, methods);
  198. }
  199. return out;
  200. },
  201. thaw(arg, frozen, extra, methods) {
  202. let out = arg;
  203. for (const key in stages) {
  204. out = stages[key].thaw(out, frozen[key], extra, methods);
  205. }
  206. return out;
  207. },
  208. }));