| 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 |
|
|---|
| 8 | const DefinePlugin = require("./DefinePlugin");
|
|---|
| 9 | const WebpackError = require("./errors/WebpackError");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 12 | /** @typedef {import("./DefinePlugin").CodeValue} CodeValue */
|
|---|
| 13 |
|
|---|
| 14 | const PLUGIN_NAME = "EnvironmentPlugin";
|
|---|
| 15 |
|
|---|
| 16 | class 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 |
|
|---|
| 75 | module.exports = EnvironmentPlugin;
|
|---|