JavascriptGenerator.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const { RawSource, ReplaceSource } = require("webpack-sources");
  8. const Generator = require("../Generator");
  9. const InitFragment = require("../InitFragment");
  10. const { JS_TYPES } = require("../ModuleSourceTypesConstants");
  11. const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
  12. /** @typedef {import("webpack-sources").Source} Source */
  13. /** @typedef {import("../DependenciesBlock")} DependenciesBlock */
  14. /** @typedef {import("../Dependency")} Dependency */
  15. /** @typedef {import("../DependencyTemplate")} DependencyTemplate */
  16. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  17. /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
  18. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  19. /** @typedef {import("../Module")} Module */
  20. /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
  21. /** @typedef {import("../Module").SourceTypes} SourceTypes */
  22. /** @typedef {import("../NormalModule")} NormalModule */
  23. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  24. // TODO: clean up this file
  25. // replace with newer constructs
  26. const deprecatedGetInitFragments = util.deprecate(
  27. /**
  28. * @param {DependencyTemplate} template template
  29. * @param {Dependency} dependency dependency
  30. * @param {DependencyTemplateContext} templateContext template context
  31. * @returns {InitFragment<GenerateContext>[]} init fragments
  32. */
  33. (template, dependency, templateContext) =>
  34. /** @type {DependencyTemplate & { getInitFragments: function(Dependency, DependencyTemplateContext): InitFragment<GenerateContext>[] }} */
  35. (template).getInitFragments(dependency, templateContext),
  36. "DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)",
  37. "DEP_WEBPACK_JAVASCRIPT_GENERATOR_GET_INIT_FRAGMENTS"
  38. );
  39. class JavascriptGenerator extends Generator {
  40. /**
  41. * @param {NormalModule} module fresh module
  42. * @returns {SourceTypes} available types (do not mutate)
  43. */
  44. getTypes(module) {
  45. return JS_TYPES;
  46. }
  47. /**
  48. * @param {NormalModule} module the module
  49. * @param {string=} type source type
  50. * @returns {number} estimate size of the module
  51. */
  52. getSize(module, type) {
  53. const originalSource = module.originalSource();
  54. if (!originalSource) {
  55. return 39;
  56. }
  57. return originalSource.size();
  58. }
  59. /**
  60. * @param {NormalModule} module module for which the bailout reason should be determined
  61. * @param {ConcatenationBailoutReasonContext} context context
  62. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  63. */
  64. getConcatenationBailoutReason(module, context) {
  65. // Only harmony modules are valid for optimization
  66. if (
  67. !module.buildMeta ||
  68. module.buildMeta.exportsType !== "namespace" ||
  69. module.presentationalDependencies === undefined ||
  70. !module.presentationalDependencies.some(
  71. d => d instanceof HarmonyCompatibilityDependency
  72. )
  73. ) {
  74. return "Module is not an ECMAScript module";
  75. }
  76. // Some expressions are not compatible with module concatenation
  77. // because they may produce unexpected results. The plugin bails out
  78. // if some were detected upfront.
  79. if (module.buildInfo && module.buildInfo.moduleConcatenationBailout) {
  80. return `Module uses ${module.buildInfo.moduleConcatenationBailout}`;
  81. }
  82. }
  83. /**
  84. * @param {NormalModule} module module for which the code should be generated
  85. * @param {GenerateContext} generateContext context for generate
  86. * @returns {Source | null} generated code
  87. */
  88. generate(module, generateContext) {
  89. const originalSource = module.originalSource();
  90. if (!originalSource) {
  91. return new RawSource("throw new Error('No source available');");
  92. }
  93. const source = new ReplaceSource(originalSource);
  94. /** @type {InitFragment<GenerateContext>[]} */
  95. const initFragments = [];
  96. this.sourceModule(module, initFragments, source, generateContext);
  97. return InitFragment.addToSource(source, initFragments, generateContext);
  98. }
  99. /**
  100. * @param {Module} module the module to generate
  101. * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
  102. * @param {ReplaceSource} source the current replace source which can be modified
  103. * @param {GenerateContext} generateContext the generateContext
  104. * @returns {void}
  105. */
  106. sourceModule(module, initFragments, source, generateContext) {
  107. for (const dependency of module.dependencies) {
  108. this.sourceDependency(
  109. module,
  110. dependency,
  111. initFragments,
  112. source,
  113. generateContext
  114. );
  115. }
  116. if (module.presentationalDependencies !== undefined) {
  117. for (const dependency of module.presentationalDependencies) {
  118. this.sourceDependency(
  119. module,
  120. dependency,
  121. initFragments,
  122. source,
  123. generateContext
  124. );
  125. }
  126. }
  127. for (const childBlock of module.blocks) {
  128. this.sourceBlock(
  129. module,
  130. childBlock,
  131. initFragments,
  132. source,
  133. generateContext
  134. );
  135. }
  136. }
  137. /**
  138. * @param {Module} module the module to generate
  139. * @param {DependenciesBlock} block the dependencies block which will be processed
  140. * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
  141. * @param {ReplaceSource} source the current replace source which can be modified
  142. * @param {GenerateContext} generateContext the generateContext
  143. * @returns {void}
  144. */
  145. sourceBlock(module, block, initFragments, source, generateContext) {
  146. for (const dependency of block.dependencies) {
  147. this.sourceDependency(
  148. module,
  149. dependency,
  150. initFragments,
  151. source,
  152. generateContext
  153. );
  154. }
  155. for (const childBlock of block.blocks) {
  156. this.sourceBlock(
  157. module,
  158. childBlock,
  159. initFragments,
  160. source,
  161. generateContext
  162. );
  163. }
  164. }
  165. /**
  166. * @param {Module} module the current module
  167. * @param {Dependency} dependency the dependency to generate
  168. * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
  169. * @param {ReplaceSource} source the current replace source which can be modified
  170. * @param {GenerateContext} generateContext the render context
  171. * @returns {void}
  172. */
  173. sourceDependency(module, dependency, initFragments, source, generateContext) {
  174. const constructor =
  175. /** @type {new (...args: EXPECTED_ANY[]) => Dependency} */
  176. (dependency.constructor);
  177. const template = generateContext.dependencyTemplates.get(constructor);
  178. if (!template) {
  179. throw new Error(
  180. `No template for dependency: ${dependency.constructor.name}`
  181. );
  182. }
  183. /** @type {InitFragment<GenerateContext>[] | undefined} */
  184. let chunkInitFragments;
  185. /** @type {DependencyTemplateContext} */
  186. const templateContext = {
  187. runtimeTemplate: generateContext.runtimeTemplate,
  188. dependencyTemplates: generateContext.dependencyTemplates,
  189. moduleGraph: generateContext.moduleGraph,
  190. chunkGraph: generateContext.chunkGraph,
  191. module,
  192. runtime: generateContext.runtime,
  193. runtimeRequirements: generateContext.runtimeRequirements,
  194. concatenationScope: generateContext.concatenationScope,
  195. codeGenerationResults:
  196. /** @type {NonNullable<GenerateContext["codeGenerationResults"]>} */
  197. (generateContext.codeGenerationResults),
  198. initFragments,
  199. get chunkInitFragments() {
  200. if (!chunkInitFragments) {
  201. const data =
  202. /** @type {NonNullable<GenerateContext["getData"]>} */
  203. (generateContext.getData)();
  204. chunkInitFragments = data.get("chunkInitFragments");
  205. if (!chunkInitFragments) {
  206. chunkInitFragments = [];
  207. data.set("chunkInitFragments", chunkInitFragments);
  208. }
  209. }
  210. return chunkInitFragments;
  211. }
  212. };
  213. template.apply(dependency, source, templateContext);
  214. // TODO remove in webpack 6
  215. if ("getInitFragments" in template) {
  216. const fragments = deprecatedGetInitFragments(
  217. template,
  218. dependency,
  219. templateContext
  220. );
  221. if (fragments) {
  222. for (const fragment of fragments) {
  223. initFragments.push(fragment);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. module.exports = JavascriptGenerator;