source: frontend/node_modules/webpack/lib/dependencies/PureExpressionDependency.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.0 KB
RevLine 
[9af201e]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { UsageState } = require("../ExportsInfo");
9const makeSerializable = require("../util/makeSerializable");
10const { filterRuntime, runtimeToString } = require("../util/runtime");
11const NullDependency = require("./NullDependency");
12
13/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
14/** @typedef {import("../Dependency")} Dependency */
15/** @typedef {import("../Dependency").RuntimeSpec} RuntimeSpec */
16/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
17/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../Module")} Module */
19/** @typedef {import("../ModuleGraph")} ModuleGraph */
20/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
21/** @typedef {import("../javascript/JavascriptParser").Range} Range */
22/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
23/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
24/** @typedef {import("../util/Hash")} Hash */
25
26class PureExpressionDependency extends NullDependency {
27 /**
28 * Creates an instance of PureExpressionDependency.
29 * @param {Range} range the source range
30 */
31 constructor(range) {
32 super();
33 this.range = range;
34 /** @type {Set<string> | false} */
35 this.usedByExports = false;
36 }
37
38 /**
39 * Get runtime condition.
40 * @param {ModuleGraph} moduleGraph module graph
41 * @param {RuntimeSpec} runtime current runtimes
42 * @returns {boolean | RuntimeSpec} runtime condition
43 */
44 _getRuntimeCondition(moduleGraph, runtime) {
45 const usedByExports = this.usedByExports;
46 if (usedByExports !== false) {
47 const selfModule =
48 /** @type {Module} */
49 (moduleGraph.getParentModule(this));
50 const exportsInfo = moduleGraph.getExportsInfo(selfModule);
51 const runtimeCondition = filterRuntime(runtime, (runtime) => {
52 for (const exportName of usedByExports) {
53 if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
54 return true;
55 }
56 }
57 return false;
58 });
59 return runtimeCondition;
60 }
61 return false;
62 }
63
64 /**
65 * Updates the hash with the data contributed by this instance.
66 * @param {Hash} hash hash to be updated
67 * @param {UpdateHashContext} context context
68 * @returns {void}
69 */
70 updateHash(hash, context) {
71 const runtimeCondition = this._getRuntimeCondition(
72 context.chunkGraph.moduleGraph,
73 context.runtime
74 );
75 if (runtimeCondition === true) {
76 return;
77 } else if (runtimeCondition === false) {
78 hash.update("null");
79 } else {
80 hash.update(
81 `${runtimeToString(runtimeCondition)}|${runtimeToString(
82 context.runtime
83 )}`
84 );
85 }
86 hash.update(String(this.range));
87 }
88
89 /**
90 * Gets module evaluation side effects state.
91 * @param {ModuleGraph} moduleGraph the module graph
92 * @returns {ConnectionState} how this dependency connects the module to referencing modules
93 */
94 getModuleEvaluationSideEffectsState(moduleGraph) {
95 return false;
96 }
97
98 /**
99 * Serializes this instance into the provided serializer context.
100 * @param {ObjectSerializerContext} context context
101 */
102 serialize(context) {
103 const { write } = context;
104 write(this.range);
105 write(this.usedByExports);
106 super.serialize(context);
107 }
108
109 /**
110 * Restores this instance from the provided deserializer context.
111 * @param {ObjectDeserializerContext} context context
112 */
113 deserialize(context) {
114 const { read } = context;
115 this.range = read();
116 this.usedByExports = read();
117 super.deserialize(context);
118 }
119}
120
121makeSerializable(
122 PureExpressionDependency,
123 "webpack/lib/dependencies/PureExpressionDependency"
124);
125
126PureExpressionDependency.Template = class PureExpressionDependencyTemplate extends (
127 NullDependency.Template
128) {
129 /**
130 * Applies the plugin by registering its hooks on the compiler.
131 * @param {Dependency} dependency the dependency for which the template should be applied
132 * @param {ReplaceSource} source the current replace source which can be modified
133 * @param {DependencyTemplateContext} templateContext the context object
134 * @returns {void}
135 */
136 apply(
137 dependency,
138 source,
139 { chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
140 ) {
141 const dep = /** @type {PureExpressionDependency} */ (dependency);
142 const runtimeCondition = dep._getRuntimeCondition(moduleGraph, runtime);
143 if (runtimeCondition === true) {
144 // Do nothing
145 } else if (runtimeCondition === false) {
146 source.insert(
147 dep.range[0],
148 "(/* unused pure expression or super */ null && ("
149 );
150 source.insert(dep.range[1], "))");
151 } else {
152 const condition = runtimeTemplate.runtimeConditionExpression({
153 chunkGraph,
154 runtime,
155 runtimeCondition,
156 runtimeRequirements
157 });
158 source.insert(
159 dep.range[0],
160 `(/* runtime-dependent pure expression or super */ ${condition} ? (`
161 );
162 source.insert(dep.range[1], ") : null)");
163 }
164 }
165};
166
167module.exports = PureExpressionDependency;
Note: See TracBrowser for help on using the repository browser.