UNPKG

3.54 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ConcatSource, PrefixSource } = require("webpack-sources");
9const InitFragment = require("./InitFragment");
10const Template = require("./Template");
11const { mergeRuntime } = require("./util/runtime");
12
13/** @typedef {import("webpack-sources").Source} Source */
14/** @typedef {import("./Generator").GenerateContext} GenerateContext */
15/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
16
17const wrapInCondition = (condition, source) => {
18 if (typeof source === "string") {
19 return Template.asString([
20 `if (${condition}) {`,
21 Template.indent(source),
22 "}",
23 ""
24 ]);
25 } else {
26 return new ConcatSource(
27 `if (${condition}) {\n`,
28 new PrefixSource("\t", source),
29 "}\n"
30 );
31 }
32};
33
34class ConditionalInitFragment extends InitFragment {
35 /**
36 * @param {string|Source} content the source code that will be included as initialization code
37 * @param {number} stage category of initialization code (contribute to order)
38 * @param {number} position position in the category (contribute to order)
39 * @param {string} key unique key to avoid emitting the same initialization code twice
40 * @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed
41 * @param {string|Source=} endContent the source code that will be included at the end of the module
42 */
43 constructor(
44 content,
45 stage,
46 position,
47 key,
48 runtimeCondition = true,
49 endContent
50 ) {
51 super(content, stage, position, key, endContent);
52 this.runtimeCondition = runtimeCondition;
53 }
54
55 /**
56 * @param {GenerateContext} generateContext context for generate
57 * @returns {string|Source} the source code that will be included as initialization code
58 */
59 getContent(generateContext) {
60 if (this.runtimeCondition === false || !this.content) return "";
61 if (this.runtimeCondition === true) return this.content;
62 const expr = generateContext.runtimeTemplate.runtimeConditionExpression({
63 chunkGraph: generateContext.chunkGraph,
64 runtimeRequirements: generateContext.runtimeRequirements,
65 runtime: generateContext.runtime,
66 runtimeCondition: this.runtimeCondition
67 });
68 if (expr === "true") return this.content;
69 return wrapInCondition(expr, this.content);
70 }
71
72 /**
73 * @param {GenerateContext} generateContext context for generate
74 * @returns {string|Source=} the source code that will be included at the end of the module
75 */
76 getEndContent(generateContext) {
77 if (this.runtimeCondition === false || !this.endContent) return "";
78 if (this.runtimeCondition === true) return this.endContent;
79 const expr = generateContext.runtimeTemplate.runtimeConditionExpression({
80 chunkGraph: generateContext.chunkGraph,
81 runtimeRequirements: generateContext.runtimeRequirements,
82 runtime: generateContext.runtime,
83 runtimeCondition: this.runtimeCondition
84 });
85 if (expr === "true") return this.endContent;
86 return wrapInCondition(expr, this.endContent);
87 }
88
89 merge(other) {
90 if (this.runtimeCondition === true) return this;
91 if (other.runtimeCondition === true) return other;
92 if (this.runtimeCondition === false) return other;
93 if (other.runtimeCondition === false) return this;
94 const runtimeCondition = mergeRuntime(
95 this.runtimeCondition,
96 other.runtimeCondition
97 );
98 return new ConditionalInitFragment(
99 this.content,
100 this.stage,
101 this.position,
102 this.key,
103 runtimeCondition,
104 this.endContent
105 );
106 }
107}
108
109module.exports = ConditionalInitFragment;