HarmonyImportDependency.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConditionalInitFragment = require("../ConditionalInitFragment");
  7. const Dependency = require("../Dependency");
  8. const HarmonyLinkingError = require("../HarmonyLinkingError");
  9. const InitFragment = require("../InitFragment");
  10. const Template = require("../Template");
  11. const AwaitDependenciesInitFragment = require("../async-modules/AwaitDependenciesInitFragment");
  12. const { filterRuntime, mergeRuntime } = require("../util/runtime");
  13. const { ImportPhase, ImportPhaseUtils } = require("./ImportPhase");
  14. const ModuleDependency = require("./ModuleDependency");
  15. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  16. /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
  17. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  18. /** @typedef {import("../ExportsInfo")} ExportsInfo */
  19. /** @typedef {import("../Module")} Module */
  20. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  21. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  22. /** @typedef {import("../WebpackError")} WebpackError */
  23. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  24. /** @typedef {import("./ImportPhase").ImportPhaseType} ImportPhaseType */
  25. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  26. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  27. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  28. /** @typedef {0 | 1 | 2 | 3 | false} ExportPresenceMode */
  29. const ExportPresenceModes = {
  30. NONE: /** @type {ExportPresenceMode} */ (0),
  31. WARN: /** @type {ExportPresenceMode} */ (1),
  32. AUTO: /** @type {ExportPresenceMode} */ (2),
  33. ERROR: /** @type {ExportPresenceMode} */ (3),
  34. /**
  35. * @param {string | false} str param
  36. * @returns {ExportPresenceMode} result
  37. */
  38. fromUserOption(str) {
  39. switch (str) {
  40. case "error":
  41. return ExportPresenceModes.ERROR;
  42. case "warn":
  43. return ExportPresenceModes.WARN;
  44. case "auto":
  45. return ExportPresenceModes.AUTO;
  46. case false:
  47. return ExportPresenceModes.NONE;
  48. default:
  49. throw new Error(`Invalid export presence value ${str}`);
  50. }
  51. }
  52. };
  53. /** @typedef {string[]} Ids */
  54. class HarmonyImportDependency extends ModuleDependency {
  55. /**
  56. * @param {string} request request string
  57. * @param {number} sourceOrder source order
  58. * @param {ImportPhaseType=} phase import phase
  59. * @param {ImportAttributes=} attributes import attributes
  60. */
  61. constructor(
  62. request,
  63. sourceOrder,
  64. phase = ImportPhase.Evaluation,
  65. attributes = undefined
  66. ) {
  67. super(request, sourceOrder);
  68. this.phase = phase;
  69. this.attributes = attributes;
  70. }
  71. get category() {
  72. return "esm";
  73. }
  74. /**
  75. * @returns {string | null} an identifier to merge equal requests
  76. */
  77. getResourceIdentifier() {
  78. let str = super.getResourceIdentifier();
  79. if (ImportPhaseUtils.isDefer(this.phase)) {
  80. str += "|defer";
  81. }
  82. if (this.attributes) {
  83. str += `|importAttributes${JSON.stringify(this.attributes)}`;
  84. }
  85. return str;
  86. }
  87. /**
  88. * Returns list of exports referenced by this dependency
  89. * @param {ModuleGraph} moduleGraph module graph
  90. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  91. * @returns {ReferencedExports} referenced exports
  92. */
  93. getReferencedExports(moduleGraph, runtime) {
  94. return Dependency.NO_EXPORTS_REFERENCED;
  95. }
  96. /**
  97. * @param {ModuleGraph} moduleGraph the module graph
  98. * @returns {string} name of the variable for the import
  99. */
  100. getImportVar(moduleGraph) {
  101. const module = /** @type {Module} */ (moduleGraph.getParentModule(this));
  102. const importedModule = /** @type {Module} */ (moduleGraph.getModule(this));
  103. const meta = moduleGraph.getMeta(module);
  104. const isDeferred =
  105. ImportPhaseUtils.isDefer(this.phase) &&
  106. !(/** @type {BuildMeta} */ (importedModule.buildMeta).async);
  107. const metaKey = isDeferred ? "deferredImportVarMap" : "importVarMap";
  108. let importVarMap = meta[metaKey];
  109. if (!importVarMap) {
  110. meta[metaKey] = importVarMap =
  111. /** @type {Map<Module, string>} */
  112. (new Map());
  113. }
  114. let importVar = importVarMap.get(importedModule);
  115. if (importVar) return importVar;
  116. importVar = `${Template.toIdentifier(
  117. `${this.userRequest}`
  118. )}__WEBPACK_${isDeferred ? "DEFERRED_" : ""}IMPORTED_MODULE_${importVarMap.size}__`;
  119. importVarMap.set(importedModule, importVar);
  120. return importVar;
  121. }
  122. /**
  123. * @param {DependencyTemplateContext} context the template context
  124. * @returns {string} the expression
  125. */
  126. getModuleExports({
  127. runtimeTemplate,
  128. moduleGraph,
  129. chunkGraph,
  130. runtimeRequirements
  131. }) {
  132. return runtimeTemplate.moduleExports({
  133. module: moduleGraph.getModule(this),
  134. chunkGraph,
  135. request: this.request,
  136. runtimeRequirements
  137. });
  138. }
  139. /**
  140. * @param {boolean} update create new variables or update existing one
  141. * @param {DependencyTemplateContext} templateContext the template context
  142. * @returns {[string, string]} the import statement and the compat statement
  143. */
  144. getImportStatement(
  145. update,
  146. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  147. ) {
  148. return runtimeTemplate.importStatement({
  149. update,
  150. module: /** @type {Module} */ (moduleGraph.getModule(this)),
  151. moduleGraph,
  152. chunkGraph,
  153. importVar: this.getImportVar(moduleGraph),
  154. request: this.request,
  155. originModule: module,
  156. runtimeRequirements,
  157. dependency: this
  158. });
  159. }
  160. /**
  161. * @param {ModuleGraph} moduleGraph module graph
  162. * @param {Ids} ids imported ids
  163. * @param {string} additionalMessage extra info included in the error message
  164. * @returns {WebpackError[] | undefined} errors
  165. */
  166. getLinkingErrors(moduleGraph, ids, additionalMessage) {
  167. const importedModule = moduleGraph.getModule(this);
  168. // ignore errors for missing or failed modules
  169. if (!importedModule || importedModule.getNumberOfErrors() > 0) {
  170. return;
  171. }
  172. const parentModule =
  173. /** @type {Module} */
  174. (moduleGraph.getParentModule(this));
  175. const exportsType = importedModule.getExportsType(
  176. moduleGraph,
  177. /** @type {BuildMeta} */ (parentModule.buildMeta).strictHarmonyModule
  178. );
  179. if (exportsType === "namespace" || exportsType === "default-with-named") {
  180. if (ids.length === 0) {
  181. return;
  182. }
  183. if (
  184. (exportsType !== "default-with-named" || ids[0] !== "default") &&
  185. moduleGraph.isExportProvided(importedModule, ids) === false
  186. ) {
  187. // We are sure that it's not provided
  188. // Try to provide detailed info in the error message
  189. let pos = 0;
  190. let exportsInfo = moduleGraph.getExportsInfo(importedModule);
  191. while (pos < ids.length && exportsInfo) {
  192. const id = ids[pos++];
  193. const exportInfo = exportsInfo.getReadOnlyExportInfo(id);
  194. if (exportInfo.provided === false) {
  195. // We are sure that it's not provided
  196. const providedExports = exportsInfo.getProvidedExports();
  197. const moreInfo = !Array.isArray(providedExports)
  198. ? " (possible exports unknown)"
  199. : providedExports.length === 0
  200. ? " (module has no exports)"
  201. : ` (possible exports: ${providedExports.join(", ")})`;
  202. return [
  203. new HarmonyLinkingError(
  204. `export ${ids
  205. .slice(0, pos)
  206. .map((id) => `'${id}'`)
  207. .join(".")} ${additionalMessage} was not found in '${
  208. this.userRequest
  209. }'${moreInfo}`
  210. )
  211. ];
  212. }
  213. exportsInfo =
  214. /** @type {ExportsInfo} */
  215. (exportInfo.getNestedExportsInfo());
  216. }
  217. // General error message
  218. return [
  219. new HarmonyLinkingError(
  220. `export ${ids
  221. .map((id) => `'${id}'`)
  222. .join(".")} ${additionalMessage} was not found in '${
  223. this.userRequest
  224. }'`
  225. )
  226. ];
  227. }
  228. }
  229. switch (exportsType) {
  230. case "default-only":
  231. // It's has only a default export
  232. if (ids.length > 0 && ids[0] !== "default") {
  233. // In strict harmony modules we only support the default export
  234. return [
  235. new HarmonyLinkingError(
  236. `Can't import the named export ${ids
  237. .map((id) => `'${id}'`)
  238. .join(
  239. "."
  240. )} ${additionalMessage} from default-exporting module (only default export is available)`
  241. )
  242. ];
  243. }
  244. break;
  245. case "default-with-named":
  246. // It has a default export and named properties redirect
  247. // In some cases we still want to warn here
  248. if (
  249. ids.length > 0 &&
  250. ids[0] !== "default" &&
  251. /** @type {BuildMeta} */
  252. (importedModule.buildMeta).defaultObject === "redirect-warn"
  253. ) {
  254. // For these modules only the default export is supported
  255. return [
  256. new HarmonyLinkingError(
  257. `Should not import the named export ${ids
  258. .map((id) => `'${id}'`)
  259. .join(
  260. "."
  261. )} ${additionalMessage} from default-exporting module (only default export is available soon)`
  262. )
  263. ];
  264. }
  265. break;
  266. }
  267. }
  268. /**
  269. * @param {ObjectSerializerContext} context context
  270. */
  271. serialize(context) {
  272. const { write } = context;
  273. write(this.attributes);
  274. write(this.phase);
  275. super.serialize(context);
  276. }
  277. /**
  278. * @param {ObjectDeserializerContext} context context
  279. */
  280. deserialize(context) {
  281. const { read } = context;
  282. this.attributes = read();
  283. this.phase = read();
  284. super.deserialize(context);
  285. }
  286. }
  287. module.exports = HarmonyImportDependency;
  288. /** @type {WeakMap<Module, WeakMap<Module, RuntimeSpec | boolean>>} */
  289. const importEmittedMap = new WeakMap();
  290. HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate extends (
  291. ModuleDependency.Template
  292. ) {
  293. /**
  294. * @param {Dependency} dependency the dependency for which the template should be applied
  295. * @param {ReplaceSource} source the current replace source which can be modified
  296. * @param {DependencyTemplateContext} templateContext the context object
  297. * @returns {void}
  298. */
  299. apply(dependency, source, templateContext) {
  300. const dep = /** @type {HarmonyImportDependency} */ (dependency);
  301. const { module, chunkGraph, moduleGraph, runtime } = templateContext;
  302. const connection = moduleGraph.getConnection(dep);
  303. if (connection && !connection.isTargetActive(runtime)) return;
  304. const referencedModule = connection && connection.module;
  305. if (
  306. connection &&
  307. connection.weak &&
  308. referencedModule &&
  309. chunkGraph.getModuleId(referencedModule) === null
  310. ) {
  311. // in weak references, module might not be in any chunk
  312. // but that's ok, we don't need that logic in this case
  313. return;
  314. }
  315. const moduleKey = referencedModule
  316. ? referencedModule.identifier()
  317. : dep.request;
  318. const key = `${ImportPhaseUtils.isDefer(dep.phase) ? "deferred " : ""}harmony import ${moduleKey}`;
  319. const runtimeCondition = dep.weak
  320. ? false
  321. : connection
  322. ? filterRuntime(runtime, (r) => connection.isTargetActive(r))
  323. : true;
  324. if (module && referencedModule) {
  325. let emittedModules = importEmittedMap.get(module);
  326. if (emittedModules === undefined) {
  327. emittedModules = new WeakMap();
  328. importEmittedMap.set(module, emittedModules);
  329. }
  330. let mergedRuntimeCondition = runtimeCondition;
  331. const oldRuntimeCondition = emittedModules.get(referencedModule) || false;
  332. if (oldRuntimeCondition !== false && mergedRuntimeCondition !== true) {
  333. if (mergedRuntimeCondition === false || oldRuntimeCondition === true) {
  334. mergedRuntimeCondition = oldRuntimeCondition;
  335. } else {
  336. mergedRuntimeCondition = mergeRuntime(
  337. oldRuntimeCondition,
  338. mergedRuntimeCondition
  339. );
  340. }
  341. }
  342. emittedModules.set(referencedModule, mergedRuntimeCondition);
  343. }
  344. const importStatement = dep.getImportStatement(false, templateContext);
  345. if (
  346. referencedModule &&
  347. templateContext.moduleGraph.isAsync(referencedModule)
  348. ) {
  349. templateContext.initFragments.push(
  350. new ConditionalInitFragment(
  351. importStatement[0],
  352. InitFragment.STAGE_HARMONY_IMPORTS,
  353. /** @type {number} */ (dep.sourceOrder),
  354. key,
  355. runtimeCondition
  356. )
  357. );
  358. const importVar = dep.getImportVar(templateContext.moduleGraph);
  359. templateContext.initFragments.push(
  360. new AwaitDependenciesInitFragment(new Map([[importVar, importVar]]))
  361. );
  362. templateContext.initFragments.push(
  363. new ConditionalInitFragment(
  364. importStatement[1],
  365. InitFragment.STAGE_ASYNC_HARMONY_IMPORTS,
  366. /** @type {number} */ (dep.sourceOrder),
  367. `${key} compat`,
  368. runtimeCondition
  369. )
  370. );
  371. } else {
  372. templateContext.initFragments.push(
  373. new ConditionalInitFragment(
  374. importStatement[0] + importStatement[1],
  375. InitFragment.STAGE_HARMONY_IMPORTS,
  376. /** @type {number} */ (dep.sourceOrder),
  377. key,
  378. runtimeCondition
  379. )
  380. );
  381. }
  382. }
  383. /**
  384. * @param {Module} module the module
  385. * @param {Module} referencedModule the referenced module
  386. * @returns {RuntimeSpec | boolean} runtimeCondition in which this import has been emitted
  387. */
  388. static getImportEmittedRuntime(module, referencedModule) {
  389. const emittedModules = importEmittedMap.get(module);
  390. if (emittedModules === undefined) return false;
  391. return emittedModules.get(referencedModule) || false;
  392. }
  393. };
  394. module.exports.ExportPresenceModes = ExportPresenceModes;