source: frontend/node_modules/webpack/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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