123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- "use strict";
- const { ConcatSource, PrefixSource } = require("webpack-sources");
- const InitFragment = require("./InitFragment");
- const Template = require("./Template");
- const { mergeRuntime } = require("./util/runtime");
- const wrapInCondition = (condition, source) => {
- if (typeof source === "string") {
- return Template.asString([
- `if (${condition}) {`,
- Template.indent(source),
- "}",
- ""
- ]);
- } else {
- return new ConcatSource(
- `if (${condition}) {\n`,
- new PrefixSource("\t", source),
- "}\n"
- );
- }
- };
- class ConditionalInitFragment extends InitFragment {
-
- constructor(
- content,
- stage,
- position,
- key,
- runtimeCondition = true,
- endContent
- ) {
- super(content, stage, position, key, endContent);
- this.runtimeCondition = runtimeCondition;
- }
-
- getContent(context) {
- if (this.runtimeCondition === false || !this.content) return "";
- if (this.runtimeCondition === true) return this.content;
- const expr = context.runtimeTemplate.runtimeConditionExpression({
- chunkGraph: context.chunkGraph,
- runtimeRequirements: context.runtimeRequirements,
- runtime: context.runtime,
- runtimeCondition: this.runtimeCondition
- });
- if (expr === "true") return this.content;
- return wrapInCondition(expr, this.content);
- }
-
- getEndContent(context) {
- if (this.runtimeCondition === false || !this.endContent) return "";
- if (this.runtimeCondition === true) return this.endContent;
- const expr = context.runtimeTemplate.runtimeConditionExpression({
- chunkGraph: context.chunkGraph,
- runtimeRequirements: context.runtimeRequirements,
- runtime: context.runtime,
- runtimeCondition: this.runtimeCondition
- });
- if (expr === "true") return this.endContent;
- return wrapInCondition(expr, this.endContent);
- }
-
- merge(other) {
- if (this.runtimeCondition === true) return this;
- if (other.runtimeCondition === true) return other;
- if (this.runtimeCondition === false) return other;
- if (other.runtimeCondition === false) return this;
- const runtimeCondition = mergeRuntime(
- this.runtimeCondition,
- other.runtimeCondition
- );
- return new ConditionalInitFragment(
- this.content,
- this.stage,
- this.position,
- this.key,
- runtimeCondition,
- this.endContent
- );
- }
- }
- module.exports = ConditionalInitFragment;
|