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