1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { UsageState } = require("../ExportsInfo");
|
9 | const makeSerializable = require("../util/makeSerializable");
|
10 | const { filterRuntime } = require("../util/runtime");
|
11 | const NullDependency = require("./NullDependency");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class PureExpressionDependency extends NullDependency {
|
23 | |
24 |
|
25 |
|
26 | constructor(range) {
|
27 | super();
|
28 | this.range = range;
|
29 |
|
30 | this.usedByExports = false;
|
31 | this._hashUpdate = undefined;
|
32 | }
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
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 |
|
49 |
|
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 |
|
70 | makeSerializable(
|
71 | PureExpressionDependency,
|
72 | "webpack/lib/dependencies/PureExpressionDependency"
|
73 | );
|
74 |
|
75 | PureExpressionDependency.Template = class PureExpressionDependencyTemplate extends (
|
76 | NullDependency.Template
|
77 | ) {
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | apply(
|
85 | dependency,
|
86 | source,
|
87 | { chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
|
88 | ) {
|
89 | const dep = (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 |
|
128 | module.exports = PureExpressionDependency;
|