UNPKG

3.66 kBJavaScriptView Raw
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 } = require("../util/runtime");
11const NullDependency = require("./NullDependency");
12
13/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
14/** @typedef {import("../ChunkGraph")} ChunkGraph */
15/** @typedef {import("../Dependency")} Dependency */
16/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
17/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../ModuleGraph")} ModuleGraph */
19/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
20/** @typedef {import("../util/Hash")} Hash */
21
22class PureExpressionDependency extends NullDependency {
23 /**
24 * @param {[number, number]} range the source range
25 */
26 constructor(range) {
27 super();
28 this.range = range;
29 /** @type {Set<string> | false} */
30 this.usedByExports = false;
31 this._hashUpdate = undefined;
32 }
33
34 /**
35 * Update the hash
36 * @param {Hash} hash hash to be updated
37 * @param {UpdateHashContext} context context
38 * @returns {void}
39 */
40 updateHash(hash, context) {
41 if (this._hashUpdate === undefined) {
42 this._hashUpdate = this.range + "";
43 }
44 hash.update(this._hashUpdate);
45 }
46
47 /**
48 * @param {ModuleGraph} moduleGraph the module graph
49 * @returns {ConnectionState} how this dependency connects the module to referencing modules
50 */
51 getModuleEvaluationSideEffectsState(moduleGraph) {
52 return false;
53 }
54
55 serialize(context) {
56 const { write } = context;
57 write(this.range);
58 write(this.usedByExports);
59 super.serialize(context);
60 }
61
62 deserialize(context) {
63 const { read } = context;
64 this.range = read();
65 this.usedByExports = read();
66 super.deserialize(context);
67 }
68}
69
70makeSerializable(
71 PureExpressionDependency,
72 "webpack/lib/dependencies/PureExpressionDependency"
73);
74
75PureExpressionDependency.Template = class PureExpressionDependencyTemplate extends (
76 NullDependency.Template
77) {
78 /**
79 * @param {Dependency} dependency the dependency for which the template should be applied
80 * @param {ReplaceSource} source the current replace source which can be modified
81 * @param {DependencyTemplateContext} templateContext the context object
82 * @returns {void}
83 */
84 apply(
85 dependency,
86 source,
87 { chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
88 ) {
89 const dep = /** @type {PureExpressionDependency} */ (dependency);
90
91 const usedByExports = dep.usedByExports;
92 if (usedByExports !== false) {
93 const selfModule = moduleGraph.getParentModule(dep);
94 const exportsInfo = moduleGraph.getExportsInfo(selfModule);
95 const runtimeCondition = filterRuntime(runtime, runtime => {
96 for (const exportName of usedByExports) {
97 if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
98 return true;
99 }
100 }
101 return false;
102 });
103 if (runtimeCondition === true) return;
104 if (runtimeCondition !== false) {
105 const condition = runtimeTemplate.runtimeConditionExpression({
106 chunkGraph,
107 runtime,
108 runtimeCondition,
109 runtimeRequirements
110 });
111 source.insert(
112 dep.range[0],
113 `(/* runtime-dependent pure expression or super */ ${condition} ? (`
114 );
115 source.insert(dep.range[1], ") : null)");
116 return;
117 }
118 }
119
120 source.insert(
121 dep.range[0],
122 `(/* unused pure expression or super */ null && (`
123 );
124 source.insert(dep.range[1], "))");
125 }
126};
127
128module.exports = PureExpressionDependency;