[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Ivan Kopeykin @vankop
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 9 | const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 12 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 15 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
| 16 | /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
---|
| 17 | /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
---|
| 18 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 20 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Dependency for static evaluating import specifier. e.g.
|
---|
| 24 | * @example
|
---|
| 25 | * import a from "a";
|
---|
| 26 | * "x" in a;
|
---|
| 27 | * a.x !== undefined; // if x value statically analyzable
|
---|
| 28 | */
|
---|
| 29 | class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency {
|
---|
| 30 | /**
|
---|
| 31 | * @param {string} request the request string
|
---|
| 32 | * @param {number} sourceOrder source order
|
---|
| 33 | * @param {TODO} ids ids
|
---|
| 34 | * @param {TODO} name name
|
---|
| 35 | * @param {Range} range location in source code
|
---|
| 36 | * @param {ImportAttributes} attributes import assertions
|
---|
| 37 | * @param {string} operator operator
|
---|
| 38 | */
|
---|
| 39 | constructor(request, sourceOrder, ids, name, range, attributes, operator) {
|
---|
| 40 | super(request, sourceOrder, ids, name, range, false, attributes, []);
|
---|
| 41 | this.operator = operator;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | get type() {
|
---|
| 45 | return `evaluated X ${this.operator} harmony import specifier`;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @param {ObjectSerializerContext} context context
|
---|
| 50 | */
|
---|
| 51 | serialize(context) {
|
---|
| 52 | super.serialize(context);
|
---|
| 53 | const { write } = context;
|
---|
| 54 | write(this.operator);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * @param {ObjectDeserializerContext} context context
|
---|
| 59 | */
|
---|
| 60 | deserialize(context) {
|
---|
| 61 | super.deserialize(context);
|
---|
| 62 | const { read } = context;
|
---|
| 63 | this.operator = read();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | makeSerializable(
|
---|
| 68 | HarmonyEvaluatedImportSpecifierDependency,
|
---|
| 69 | "webpack/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency"
|
---|
| 70 | );
|
---|
| 71 |
|
---|
| 72 | HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImportSpecifierDependencyTemplate extends (
|
---|
| 73 | HarmonyImportSpecifierDependency.Template
|
---|
| 74 | ) {
|
---|
| 75 | /**
|
---|
| 76 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 77 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 78 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 79 | * @returns {void}
|
---|
| 80 | */
|
---|
| 81 | apply(dependency, source, templateContext) {
|
---|
| 82 | const dep = /** @type {HarmonyEvaluatedImportSpecifierDependency} */ (
|
---|
| 83 | dependency
|
---|
| 84 | );
|
---|
| 85 | const { module, moduleGraph, runtime } = templateContext;
|
---|
| 86 | const connection = moduleGraph.getConnection(dep);
|
---|
| 87 | // Skip rendering depending when dependency is conditional
|
---|
| 88 | if (connection && !connection.isTargetActive(runtime)) return;
|
---|
| 89 |
|
---|
| 90 | const exportsInfo = moduleGraph.getExportsInfo(
|
---|
| 91 | /** @type {ModuleGraphConnection} */ (connection).module
|
---|
| 92 | );
|
---|
| 93 | const ids = dep.getIds(moduleGraph);
|
---|
| 94 |
|
---|
| 95 | let value;
|
---|
| 96 |
|
---|
| 97 | const exportsType =
|
---|
| 98 | /** @type {ModuleGraphConnection} */
|
---|
| 99 | (connection).module.getExportsType(
|
---|
| 100 | moduleGraph,
|
---|
| 101 | /** @type {BuildMeta} */
|
---|
| 102 | (module.buildMeta).strictHarmonyModule
|
---|
| 103 | );
|
---|
| 104 | switch (exportsType) {
|
---|
| 105 | case "default-with-named": {
|
---|
| 106 | if (ids[0] === "default") {
|
---|
| 107 | value =
|
---|
| 108 | ids.length === 1 || exportsInfo.isExportProvided(ids.slice(1));
|
---|
| 109 | } else {
|
---|
| 110 | value = exportsInfo.isExportProvided(ids);
|
---|
| 111 | }
|
---|
| 112 | break;
|
---|
| 113 | }
|
---|
| 114 | case "namespace": {
|
---|
| 115 | value =
|
---|
| 116 | ids[0] === "__esModule"
|
---|
| 117 | ? ids.length === 1 || undefined
|
---|
| 118 | : exportsInfo.isExportProvided(ids);
|
---|
| 119 | break;
|
---|
| 120 | }
|
---|
| 121 | case "dynamic": {
|
---|
| 122 | if (ids[0] !== "default") {
|
---|
| 123 | value = exportsInfo.isExportProvided(ids);
|
---|
| 124 | }
|
---|
| 125 | break;
|
---|
| 126 | }
|
---|
| 127 | // default-only could lead to runtime error, when default value is primitive
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | if (typeof value === "boolean") {
|
---|
| 131 | source.replace(dep.range[0], dep.range[1] - 1, ` ${value}`);
|
---|
| 132 | } else {
|
---|
| 133 | const usedName = exportsInfo.getUsedName(ids, runtime);
|
---|
| 134 |
|
---|
| 135 | const code = this._getCodeForIds(
|
---|
| 136 | dep,
|
---|
| 137 | source,
|
---|
| 138 | templateContext,
|
---|
| 139 | ids.slice(0, -1)
|
---|
| 140 | );
|
---|
| 141 | source.replace(
|
---|
| 142 | dep.range[0],
|
---|
| 143 | dep.range[1] - 1,
|
---|
| 144 | `${
|
---|
| 145 | usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""'
|
---|
| 146 | } in ${code}`
|
---|
| 147 | );
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | module.exports = HarmonyEvaluatedImportSpecifierDependency;
|
---|