123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- "use strict";
- const { UsageState } = require("../ExportsInfo");
- const makeSerializable = require("../util/makeSerializable");
- const { filterRuntime, runtimeToString } = require("../util/runtime");
- const NullDependency = require("./NullDependency");
- class PureExpressionDependency extends NullDependency {
-
- constructor(range) {
- super();
- this.range = range;
-
- this.usedByExports = false;
- }
-
- _getRuntimeCondition(moduleGraph, runtime) {
- const usedByExports = this.usedByExports;
- if (usedByExports !== false) {
- const selfModule =
-
- (moduleGraph.getParentModule(this));
- const exportsInfo = moduleGraph.getExportsInfo(selfModule);
- const runtimeCondition = filterRuntime(runtime, runtime => {
- for (const exportName of usedByExports) {
- if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
- return true;
- }
- }
- return false;
- });
- return runtimeCondition;
- }
- return false;
- }
-
- updateHash(hash, context) {
- const runtimeCondition = this._getRuntimeCondition(
- context.chunkGraph.moduleGraph,
- context.runtime
- );
- if (runtimeCondition === true) {
- return;
- } else if (runtimeCondition === false) {
- hash.update("null");
- } else {
- hash.update(
- runtimeToString(runtimeCondition) +
- "|" +
- runtimeToString(context.runtime)
- );
- }
- hash.update(this.range + "");
- }
-
- getModuleEvaluationSideEffectsState(moduleGraph) {
- return false;
- }
-
- serialize(context) {
- const { write } = context;
- write(this.range);
- write(this.usedByExports);
- super.serialize(context);
- }
-
- deserialize(context) {
- const { read } = context;
- this.range = read();
- this.usedByExports = read();
- super.deserialize(context);
- }
- }
- makeSerializable(
- PureExpressionDependency,
- "webpack/lib/dependencies/PureExpressionDependency"
- );
- PureExpressionDependency.Template = class PureExpressionDependencyTemplate extends (
- NullDependency.Template
- ) {
-
- apply(
- dependency,
- source,
- { chunkGraph, moduleGraph, runtime, runtimeTemplate, runtimeRequirements }
- ) {
- const dep = (dependency);
- const runtimeCondition = dep._getRuntimeCondition(moduleGraph, runtime);
- if (runtimeCondition === true) {
- return;
- } else if (runtimeCondition === false) {
- source.insert(
- dep.range[0],
- `(/* unused pure expression or super */ null && (`
- );
- source.insert(dep.range[1], "))");
- } else {
- const condition = runtimeTemplate.runtimeConditionExpression({
- chunkGraph,
- runtime,
- runtimeCondition,
- runtimeRequirements
- });
- source.insert(
- dep.range[0],
- `(/* runtime-dependent pure expression or super */ ${condition} ? (`
- );
- source.insert(dep.range[1], ") : null)");
- }
- }
- };
- module.exports = PureExpressionDependency;
|