Apply module bundling
[platform/framework/web/wrtjs.git] / node_modules / webpack / lib / dependencies / HarmonyEvaluatedImportSpecifierDependency.js
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
16 /**
17  * Dependency for static evaluating import specifier. e.g.
18  * @example
19  * import a from "a";
20  * "x" in a;
21  * a.x !== undefined; // if x value statically analyzable
22  */
23 class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency {
24         constructor(request, sourceOrder, ids, name, range, assertions, operator) {
25                 super(request, sourceOrder, ids, name, range, false, assertions);
26                 this.operator = operator;
27         }
28
29         get type() {
30                 return `evaluated X ${this.operator} harmony import specifier`;
31         }
32
33         serialize(context) {
34                 super.serialize(context);
35                 const { write } = context;
36                 write(this.operator);
37         }
38
39         deserialize(context) {
40                 super.deserialize(context);
41                 const { read } = context;
42                 this.operator = read();
43         }
44 }
45
46 makeSerializable(
47         HarmonyEvaluatedImportSpecifierDependency,
48         "webpack/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency"
49 );
50
51 HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImportSpecifierDependencyTemplate extends (
52         HarmonyImportSpecifierDependency.Template
53 ) {
54         /**
55          * @param {Dependency} dependency the dependency for which the template should be applied
56          * @param {ReplaceSource} source the current replace source which can be modified
57          * @param {DependencyTemplateContext} templateContext the context object
58          * @returns {void}
59          */
60         apply(dependency, source, templateContext) {
61                 const dep = /** @type {HarmonyEvaluatedImportSpecifierDependency} */ (
62                         dependency
63                 );
64                 const { module, moduleGraph, runtime } = templateContext;
65                 const connection = moduleGraph.getConnection(dep);
66                 // Skip rendering depending when dependency is conditional
67                 if (connection && !connection.isTargetActive(runtime)) return;
68
69                 const exportsInfo = moduleGraph.getExportsInfo(connection.module);
70                 const ids = dep.getIds(moduleGraph);
71
72                 let value;
73
74                 const exportsType = connection.module.getExportsType(
75                         moduleGraph,
76                         module.buildMeta.strictHarmonyModule
77                 );
78                 switch (exportsType) {
79                         case "default-with-named": {
80                                 if (ids[0] === "default") {
81                                         value =
82                                                 ids.length === 1 || exportsInfo.isExportProvided(ids.slice(1));
83                                 } else {
84                                         value = exportsInfo.isExportProvided(ids);
85                                 }
86                                 break;
87                         }
88                         case "namespace": {
89                                 if (ids[0] === "__esModule") {
90                                         value = ids.length === 1 || undefined;
91                                 } else {
92                                         value = exportsInfo.isExportProvided(ids);
93                                 }
94                                 break;
95                         }
96                         case "dynamic": {
97                                 if (ids[0] !== "default") {
98                                         value = exportsInfo.isExportProvided(ids);
99                                 }
100                                 break;
101                         }
102                         // default-only could lead to runtime error, when default value is primitive
103                 }
104
105                 if (typeof value === "boolean") {
106                         source.replace(dep.range[0], dep.range[1] - 1, ` ${value}`);
107                 } else {
108                         const usedName = exportsInfo.getUsedName(ids, runtime);
109
110                         const code = this._getCodeForIds(
111                                 dep,
112                                 source,
113                                 templateContext,
114                                 ids.slice(0, -1)
115                         );
116                         source.replace(
117                                 dep.range[0],
118                                 dep.range[1] - 1,
119                                 `${
120                                         usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""'
121                                 } in ${code}`
122                         );
123                 }
124         }
125 };
126
127 module.exports = HarmonyEvaluatedImportSpecifierDependency;