HarmonyExportExpressionDependency.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConcatenationScope = require("../ConcatenationScope");
  7. const InitFragment = require("../InitFragment");
  8. const RuntimeGlobals = require("../RuntimeGlobals");
  9. const makeSerializable = require("../util/makeSerializable");
  10. const { propertyAccess } = require("../util/property");
  11. const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
  12. const NullDependency = require("./NullDependency");
  13. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  14. /** @typedef {import("../Dependency")} Dependency */
  15. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  16. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  17. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  18. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  19. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  20. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  21. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  22. /** @typedef {import("./HarmonyExportInitFragment").ExportMap} ExportMap */
  23. class HarmonyExportExpressionDependency extends NullDependency {
  24. /**
  25. * Creates an instance of HarmonyExportExpressionDependency.
  26. * @param {Range} range range
  27. * @param {Range} rangeStatement range statement
  28. * @param {string} prefix prefix
  29. * @param {string | { id?: string | undefined, range: Range, prefix: string, suffix: string }=} declarationId declaration id
  30. */
  31. constructor(range, rangeStatement, prefix, declarationId) {
  32. super();
  33. this.range = range;
  34. this.rangeStatement = rangeStatement;
  35. this.prefix = prefix;
  36. this.declarationId = declarationId;
  37. this.isAnonymousDefault = false;
  38. }
  39. get type() {
  40. return "harmony export expression";
  41. }
  42. /**
  43. * Returns the exported names
  44. * @param {ModuleGraph} moduleGraph module graph
  45. * @returns {ExportsSpec | undefined} export names
  46. */
  47. getExports(moduleGraph) {
  48. return {
  49. exports: ["default"],
  50. priority: 1,
  51. terminalBinding: true,
  52. dependencies: undefined
  53. };
  54. }
  55. /**
  56. * Gets module evaluation side effects state.
  57. * @param {ModuleGraph} moduleGraph the module graph
  58. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  59. */
  60. getModuleEvaluationSideEffectsState(moduleGraph) {
  61. // The expression/declaration is already covered by SideEffectsFlagPlugin
  62. return false;
  63. }
  64. /**
  65. * Serializes this instance into the provided serializer context.
  66. * @param {ObjectSerializerContext} context context
  67. */
  68. serialize(context) {
  69. const { write } = context;
  70. write(this.range);
  71. write(this.rangeStatement);
  72. write(this.prefix);
  73. write(this.declarationId);
  74. write(this.isAnonymousDefault);
  75. super.serialize(context);
  76. }
  77. /**
  78. * Restores this instance from the provided deserializer context.
  79. * @param {ObjectDeserializerContext} context context
  80. */
  81. deserialize(context) {
  82. const { read } = context;
  83. this.range = read();
  84. this.rangeStatement = read();
  85. this.prefix = read();
  86. this.declarationId = read();
  87. this.isAnonymousDefault = read();
  88. super.deserialize(context);
  89. }
  90. }
  91. makeSerializable(
  92. HarmonyExportExpressionDependency,
  93. "webpack/lib/dependencies/HarmonyExportExpressionDependency"
  94. );
  95. HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTemplate extends (
  96. NullDependency.Template
  97. ) {
  98. /**
  99. * Applies the plugin by registering its hooks on the compiler.
  100. * @param {Dependency} dependency the dependency for which the template should be applied
  101. * @param {ReplaceSource} source the current replace source which can be modified
  102. * @param {DependencyTemplateContext} templateContext the context object
  103. * @returns {void}
  104. */
  105. apply(
  106. dependency,
  107. source,
  108. {
  109. module,
  110. moduleGraph,
  111. runtimeTemplate,
  112. runtimeRequirements,
  113. initFragments,
  114. runtime,
  115. concatenationScope
  116. }
  117. ) {
  118. const dep = /** @type {HarmonyExportExpressionDependency} */ (dependency);
  119. const { declarationId } = dep;
  120. const exportsName = module.exportsArgument;
  121. if (declarationId) {
  122. /** @type {string} */
  123. let name;
  124. if (typeof declarationId === "string") {
  125. name = declarationId;
  126. } else {
  127. name = ConcatenationScope.DEFAULT_EXPORT;
  128. source.replace(
  129. declarationId.range[0],
  130. declarationId.range[1] - 1,
  131. `${declarationId.prefix}${name}${declarationId.suffix}`
  132. );
  133. }
  134. if (concatenationScope) {
  135. concatenationScope.registerExport("default", name);
  136. } else {
  137. const used = moduleGraph
  138. .getExportsInfo(module)
  139. .getUsedName("default", runtime);
  140. if (used) {
  141. /** @type {ExportMap} */
  142. const map = new Map();
  143. map.set(used, `/* export default binding */ ${name}`);
  144. initFragments.push(new HarmonyExportInitFragment(exportsName, map));
  145. }
  146. }
  147. source.replace(
  148. dep.rangeStatement[0],
  149. dep.range[0] - 1,
  150. `/* harmony default export */ ${dep.prefix}`
  151. );
  152. if (typeof declarationId !== "string" && dep.isAnonymousDefault) {
  153. // Fix .name for anonymous default export function declarations
  154. // see test/test262-cases/test/language/module-code/instn-named-bndng-dflt-fun-anon.js cspell:disable-line
  155. initFragments.push(
  156. new InitFragment(
  157. `Object.defineProperty(${name}, "name", { value: "default", configurable: true });\n`,
  158. InitFragment.STAGE_HARMONY_EXPORTS,
  159. 2
  160. )
  161. );
  162. }
  163. } else {
  164. /** @type {string} */
  165. let content;
  166. let name = ConcatenationScope.DEFAULT_EXPORT;
  167. if (runtimeTemplate.supportsConst()) {
  168. content = `/* harmony default export */ const ${name} = `;
  169. if (concatenationScope) {
  170. concatenationScope.registerExport("default", name);
  171. } else {
  172. const used = moduleGraph
  173. .getExportsInfo(module)
  174. .getUsedName("default", runtime);
  175. if (used) {
  176. runtimeRequirements.add(RuntimeGlobals.exports);
  177. /** @type {ExportMap} */
  178. const map = new Map();
  179. map.set(used, name);
  180. initFragments.push(new HarmonyExportInitFragment(exportsName, map));
  181. } else {
  182. content = `/* unused harmony default export */ var ${name} = `;
  183. }
  184. }
  185. } else if (concatenationScope) {
  186. content = `/* harmony default export */ var ${name} = `;
  187. concatenationScope.registerExport("default", name);
  188. } else {
  189. const used = moduleGraph
  190. .getExportsInfo(module)
  191. .getUsedName("default", runtime);
  192. if (used) {
  193. runtimeRequirements.add(RuntimeGlobals.exports);
  194. // This is a little bit incorrect as TDZ is not correct, but we can't use const.
  195. // No local `__WEBPACK_DEFAULT_EXPORT__` binding is created in this path,
  196. // so the anonymous-default `.name` fix-up below must reference the actual
  197. // assignment target instead. See issue #20793.
  198. name = `${exportsName}${propertyAccess(
  199. typeof used === "string" ? [used] : used
  200. )}`;
  201. content = `/* harmony default export */ ${name} = `;
  202. } else {
  203. content = `/* unused harmony default export */ var ${name} = `;
  204. }
  205. }
  206. if (dep.range) {
  207. source.replace(
  208. dep.rangeStatement[0],
  209. dep.range[0] - 1,
  210. `${content}(${dep.prefix}`
  211. );
  212. if (dep.isAnonymousDefault) {
  213. // Fix .name for anonymous default export expressions
  214. // see test/test262-cases/test/language/module-code/eval-export-dflt-cls-anon.js cspell:disable-line
  215. source.replace(
  216. dep.range[1],
  217. dep.rangeStatement[1] - 0.5,
  218. `);\n(Object.getOwnPropertyDescriptor(${name}, "name") || {}).writable || Object.defineProperty(${name}, "name", { value: "default", configurable: true });`
  219. );
  220. } else {
  221. source.replace(dep.range[1], dep.rangeStatement[1] - 0.5, ");");
  222. }
  223. return;
  224. }
  225. source.replace(dep.rangeStatement[0], dep.rangeStatement[1] - 1, content);
  226. }
  227. }
  228. };
  229. module.exports = HarmonyExportExpressionDependency;