HarmonyImportDependency.js 15 KB

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