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