HarmonyImportDependency.js 14 KB

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