source: frontend/node_modules/webpack/lib/EnvironmentPlugin.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: 2.2 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Authors Simen Brekken @simenbrekken, Einar Löve @einarlove
4*/
5
6"use strict";
7
8const DefinePlugin = require("./DefinePlugin");
9const WebpackError = require("./errors/WebpackError");
10
11/** @typedef {import("./Compiler")} Compiler */
12/** @typedef {import("./DefinePlugin").CodeValue} CodeValue */
13
14const PLUGIN_NAME = "EnvironmentPlugin";
15
16class EnvironmentPlugin {
17 /**
18 * Creates an instance of EnvironmentPlugin.
19 * @param {(string | string[] | Record<string, EXPECTED_ANY>)[]} keys keys
20 */
21 constructor(...keys) {
22 if (keys.length === 1 && Array.isArray(keys[0])) {
23 /** @type {string[]} */
24 this.keys = keys[0];
25 this.defaultValues = {};
26 } else if (keys.length === 1 && keys[0] && typeof keys[0] === "object") {
27 this.keys = Object.keys(keys[0]);
28 this.defaultValues =
29 /** @type {Record<string, EXPECTED_ANY>} */
30 (keys[0]);
31 } else {
32 this.keys = /** @type {string[]} */ (keys);
33 this.defaultValues = {};
34 }
35 }
36
37 /**
38 * Applies the plugin by registering its hooks on the compiler.
39 * @param {Compiler} compiler the compiler instance
40 * @returns {void}
41 */
42 apply(compiler) {
43 const definePlugin = new DefinePlugin({});
44
45 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
46 /** @type {Record<string, CodeValue>} */
47 const definitions = {};
48 for (const key of this.keys) {
49 const value =
50 process.env[key] !== undefined
51 ? process.env[key]
52 : this.defaultValues[key];
53
54 if (value === undefined) {
55 const error = new WebpackError(
56 `${PLUGIN_NAME} - ${key} environment variable is undefined.\n\n` +
57 "You can pass an object with default values to suppress this warning.\n" +
58 "See https://webpack.js.org/plugins/environment-plugin for example."
59 );
60
61 error.name = "EnvVariableNotDefinedError";
62 compilation.errors.push(error);
63 }
64 const defValue =
65 value === undefined ? "undefined" : JSON.stringify(value);
66 definitions[`process.env.${key}`] = defValue;
67 definitions[`import.meta.env.${key}`] = defValue;
68 }
69 definePlugin.definitions = definitions;
70 });
71 definePlugin.apply(compiler);
72 }
73}
74
75module.exports = EnvironmentPlugin;
Note: See TracBrowser for help on using the repository browser.