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

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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