source: frontend/node_modules/webpack/lib/ConditionalInitFragment.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.0 KB
Line 
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
17/**
18 * Returns wrapped source.
19 * @param {string} condition condition
20 * @param {string | Source} source source
21 * @returns {string | Source} wrapped source
22 */
23const wrapInCondition = (condition, source) => {
24 if (typeof source === "string") {
25 return Template.asString([
26 `if (${condition}) {`,
27 Template.indent(source),
28 "}",
29 ""
30 ]);
31 }
32 return new ConcatSource(
33 `if (${condition}) {\n`,
34 new PrefixSource("\t", source),
35 "}\n"
36 );
37};
38
39/**
40 * Represents ConditionalInitFragment.
41 * @extends {InitFragment<GenerateContext>}
42 */
43class ConditionalInitFragment extends InitFragment {
44 /**
45 * Creates an instance of ConditionalInitFragment.
46 * @param {string | Source | undefined} content the source code that will be included as initialization code
47 * @param {number} stage category of initialization code (contribute to order)
48 * @param {number} position position in the category (contribute to order)
49 * @param {string | undefined} key unique key to avoid emitting the same initialization code twice
50 * @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed
51 * @param {string | Source=} endContent the source code that will be included at the end of the module
52 */
53 constructor(
54 content,
55 stage,
56 position,
57 key,
58 runtimeCondition = true,
59 endContent = undefined
60 ) {
61 super(content, stage, position, key, endContent);
62 this.runtimeCondition = runtimeCondition;
63 }
64
65 /**
66 * Returns the source code that will be included as initialization code.
67 * @param {GenerateContext} context context
68 * @returns {string | Source | undefined} the source code that will be included as initialization code
69 */
70 getContent(context) {
71 if (this.runtimeCondition === false || !this.content) return "";
72 if (this.runtimeCondition === true) return this.content;
73 const expr = context.runtimeTemplate.runtimeConditionExpression({
74 chunkGraph: context.chunkGraph,
75 runtimeRequirements: context.runtimeRequirements,
76 runtime: context.runtime,
77 runtimeCondition: this.runtimeCondition
78 });
79 if (expr === "true") return this.content;
80 return wrapInCondition(expr, this.content);
81 }
82
83 /**
84 * Returns the source code that will be included at the end of the module.
85 * @param {GenerateContext} context context
86 * @returns {string | Source | undefined} the source code that will be included at the end of the module
87 */
88 getEndContent(context) {
89 if (this.runtimeCondition === false || !this.endContent) return "";
90 if (this.runtimeCondition === true) return this.endContent;
91 const expr = context.runtimeTemplate.runtimeConditionExpression({
92 chunkGraph: context.chunkGraph,
93 runtimeRequirements: context.runtimeRequirements,
94 runtime: context.runtime,
95 runtimeCondition: this.runtimeCondition
96 });
97 if (expr === "true") return this.endContent;
98 return wrapInCondition(expr, this.endContent);
99 }
100
101 /**
102 * Returns merged fragment.
103 * @param {ConditionalInitFragment} other fragment to merge with
104 * @returns {ConditionalInitFragment} merged fragment
105 */
106 merge(other) {
107 if (this.runtimeCondition === true) return this;
108 if (other.runtimeCondition === true) return other;
109 if (this.runtimeCondition === false) return other;
110 if (other.runtimeCondition === false) return this;
111 const runtimeCondition = mergeRuntime(
112 this.runtimeCondition,
113 other.runtimeCondition
114 );
115 return new ConditionalInitFragment(
116 this.content,
117 this.stage,
118 this.position,
119 this.key,
120 runtimeCondition,
121 this.endContent
122 );
123 }
124}
125
126module.exports = ConditionalInitFragment;
Note: See TracBrowser for help on using the repository browser.