From abe80f05811ef3a505bbae587c2a0665e535ac1e Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 27 Apr 2023 14:29:32 +0530 Subject: [PATCH 01/65] feat: handle env files --- packages/webpack-cli/package.json | 2 + packages/webpack-cli/src/webpack-cli.ts | 62 ++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index cab9eeba764..9022883b967 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -41,6 +41,8 @@ "colorette": "^2.0.14", "commander": "^10.0.1", "cross-spawn": "^7.0.3", + "dotenv": "^16.0.3", + "dotenv-expand": "^10.0.0", "envinfo": "^7.7.3", "fastest-levenshtein": "^1.0.12", "import-local": "^3.0.2", diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index fcb5e2ab6b9..6d59f493ff3 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -45,7 +45,7 @@ import { import webpackMerge from "webpack-merge"; import webpack from "webpack"; -import { Compiler, MultiCompiler, WebpackError, StatsOptions } from "webpack"; +import { Compiler, MultiCompiler, WebpackError, StatsOptions, Configuration } from "webpack"; import { stringifyStream } from "@discoveryjs/json-ext"; import { Help, ParseOptions } from "commander"; @@ -56,6 +56,8 @@ const path = require("path"); const { pathToFileURL } = require("url"); const util = require("util"); const { program, Option } = require("commander"); +const dotenv = require("dotenv"); +const dotenvExpand = require("dotenv-expand"); const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack"; const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server"; @@ -1953,6 +1955,64 @@ class WebpackCLI implements IWebpackCLI { config.path.set(config.options, mergedConfigPaths as unknown as string); } + const runFunctionOnEachConfiguration = ( + options: Configuration | Configuration[], + fn: CallableFunction, + ) => { + if (Array.isArray(options)) { + for (let item of options) { + item = fn(item); + } + } else { + options = fn(options); + } + + return options; + }; + + // plugin env variables using dot env package + const _envDirectory = path.resolve(process.cwd(), "environment"); + + config.options = runFunctionOnEachConfiguration(config.options, (options: Configuration) => { + const mode = options.mode || "development"; + // .local file variables will get precedence + const environmentFiles = [ + `${_envDirectory}/.env`, // loaded in all cases + `${_envDirectory}/.env.local`, // loaded in all cases, ignored by git + `${_envDirectory}/.env.${mode}`, // only loaded in specified mode + `${_envDirectory}/.env.${mode}.local`, // only loaded in specified mode, ignored by git + ]; + + let envVariables: Record = {}; + // parse environment vars from .env files + environmentFiles.forEach((environmentFile) => { + if (fs.existsSync(environmentFile)) { + try { + const environmentFileContents: string = fs.readFileSync(environmentFile); + const parsedEnvVariables: Record = + dotenv.parse(environmentFileContents); + for (const [key, value] of Object.entries(parsedEnvVariables)) { + // only add variables starting with WEBPACK_ + if (key.startsWith("WEBPACK_")) envVariables[`process.env.${key}`] = value; + } + } catch (err) { + this.logger.error(`Could not read ${environmentFile}`); + } + } + }); + + // expand environment vars + envVariables = dotenvExpand({ + parsed: envVariables, + // don't write to process.env + ignoreProcessEnv: true, + }).parsed; + + options.plugins = (options.plugins || []).concat([new webpack.DefinePlugin(envVariables)]); + + return options; + }); + return config; } From f5bcec53336c905bc720b481cfd659e23b063dcb Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 27 Apr 2023 20:07:15 +0530 Subject: [PATCH 02/65] chore: undo package json changes --- packages/webpack-cli/package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 9022883b967..cab9eeba764 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -41,8 +41,6 @@ "colorette": "^2.0.14", "commander": "^10.0.1", "cross-spawn": "^7.0.3", - "dotenv": "^16.0.3", - "dotenv-expand": "^10.0.0", "envinfo": "^7.7.3", "fastest-levenshtein": "^1.0.12", "import-local": "^3.0.2", From 011627e9fcbe0d962c41852a1691c34f78355d7e Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Fri, 28 Apr 2023 20:47:45 +0530 Subject: [PATCH 03/65] refactor: undo changes to webpack cli --- packages/webpack-cli/src/webpack-cli.ts | 61 ------------------------- 1 file changed, 61 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 6d59f493ff3..2a0e6f639f0 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -56,8 +56,6 @@ const path = require("path"); const { pathToFileURL } = require("url"); const util = require("util"); const { program, Option } = require("commander"); -const dotenv = require("dotenv"); -const dotenvExpand = require("dotenv-expand"); const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack"; const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server"; @@ -1954,65 +1952,6 @@ class WebpackCLI implements IWebpackCLI { }, {}); config.path.set(config.options, mergedConfigPaths as unknown as string); } - - const runFunctionOnEachConfiguration = ( - options: Configuration | Configuration[], - fn: CallableFunction, - ) => { - if (Array.isArray(options)) { - for (let item of options) { - item = fn(item); - } - } else { - options = fn(options); - } - - return options; - }; - - // plugin env variables using dot env package - const _envDirectory = path.resolve(process.cwd(), "environment"); - - config.options = runFunctionOnEachConfiguration(config.options, (options: Configuration) => { - const mode = options.mode || "development"; - // .local file variables will get precedence - const environmentFiles = [ - `${_envDirectory}/.env`, // loaded in all cases - `${_envDirectory}/.env.local`, // loaded in all cases, ignored by git - `${_envDirectory}/.env.${mode}`, // only loaded in specified mode - `${_envDirectory}/.env.${mode}.local`, // only loaded in specified mode, ignored by git - ]; - - let envVariables: Record = {}; - // parse environment vars from .env files - environmentFiles.forEach((environmentFile) => { - if (fs.existsSync(environmentFile)) { - try { - const environmentFileContents: string = fs.readFileSync(environmentFile); - const parsedEnvVariables: Record = - dotenv.parse(environmentFileContents); - for (const [key, value] of Object.entries(parsedEnvVariables)) { - // only add variables starting with WEBPACK_ - if (key.startsWith("WEBPACK_")) envVariables[`process.env.${key}`] = value; - } - } catch (err) { - this.logger.error(`Could not read ${environmentFile}`); - } - } - }); - - // expand environment vars - envVariables = dotenvExpand({ - parsed: envVariables, - // don't write to process.env - ignoreProcessEnv: true, - }).parsed; - - options.plugins = (options.plugins || []).concat([new webpack.DefinePlugin(envVariables)]); - - return options; - }); - return config; } From bb735ece3f2d5799f50e7430581a4188932d358a Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Fri, 28 Apr 2023 20:48:05 +0530 Subject: [PATCH 04/65] feat: add dotenv webpack plugin package --- packages/dotenv-webpack-plugin/package.json | 17 ++++++ packages/dotenv-webpack-plugin/src/index.js | 68 +++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 packages/dotenv-webpack-plugin/package.json create mode 100644 packages/dotenv-webpack-plugin/src/index.js diff --git a/packages/dotenv-webpack-plugin/package.json b/packages/dotenv-webpack-plugin/package.json new file mode 100644 index 00000000000..8e221701c4b --- /dev/null +++ b/packages/dotenv-webpack-plugin/package.json @@ -0,0 +1,17 @@ +{ + "name": "dotenv-webpack-plugin", + "version": "1.0.0", + "description": "A webpack plugin to support env files", + "main": "index.js", + "repository": "https://github.com/webpack/webpack-cli", + "author": "Burhanuddin Udaipurwala (burhanuday)", + "license": "MIT", + "private": true, + "dependencies": { + "dotenv": "^16.0.3", + "dotenv-expand": "^10.0.0" + }, + "peerDependencies": { + "webpack": "5.x.x" + } +} diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js new file mode 100644 index 00000000000..83841a1caea --- /dev/null +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -0,0 +1,68 @@ +const fs = require("fs"); +const path = require("path"); +const dotenv = require("dotenv"); +const dotenvExpand = require("dotenv-expand"); +const { DefinePlugin } = require("webpack"); + +class DotenvWebpackPlugin { + constructor(config = {}) { + this.config = Object.assign( + {}, + { + envFiles: [], + prefixes: ["process.env.", "import.meta.env."], + }, + config, + ); + this.cache = new Map(); + } + + apply(compiler) { + const currentDirectory = path.resolve(process.cwd(), "environment"); + + const mode = compiler.options.mode || "development"; + // .local file variables will get precedence + const environmentFiles = + this.config.envFiles.length > 0 + ? this.config.envFiles + : [ + `${currentDirectory}/.env`, // loaded in all cases + `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git + `${currentDirectory}/.env.${mode}`, // only loaded in specified mode + `${currentDirectory}/.env.${mode}.local`, // only loaded in specified mode, ignored by git + ]; + + let envVariables = {}; + // parse environment vars from .env files + environmentFiles.forEach((environmentFile) => { + if (fs.existsSync(environmentFile)) { + try { + const environmentFileContents = fs.readFileSync(environmentFile); + const parsedEnvVariables = dotenv.parse(environmentFileContents); + for (const [key, value] of Object.entries(parsedEnvVariables)) { + // only add variables starting with WEBPACK_ + if (key.startsWith("WEBPACK_")) { + for (let index = 0; index < this.config.prefixes.length; index++) { + const prefix = this.config.prefixes[index]; + envVariables[`${prefix}${key}`] = value; + } + } + } + } catch (err) { + this.logger.error(`Could not read ${environmentFile}`); + } + } + }); + + // expand environment vars + envVariables = dotenvExpand({ + parsed: envVariables, + // don't write to process.env + ignoreProcessEnv: true, + }).parsed; + + new DefinePlugin(envVariables).apply(compiler); + } +} + +module.exports = DotenvWebpackPlugin; From 5a1b4dba8e5bcb201595b1d78ecf04d0e1bcb5ec Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sun, 30 Apr 2023 09:40:46 +0530 Subject: [PATCH 05/65] refactor: remove cache --- packages/dotenv-webpack-plugin/src/index.js | 1 - packages/webpack-cli/src/webpack-cli.ts | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 83841a1caea..f4af7a7e1d9 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -14,7 +14,6 @@ class DotenvWebpackPlugin { }, config, ); - this.cache = new Map(); } apply(compiler) { diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 2a0e6f639f0..fcb5e2ab6b9 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -45,7 +45,7 @@ import { import webpackMerge from "webpack-merge"; import webpack from "webpack"; -import { Compiler, MultiCompiler, WebpackError, StatsOptions, Configuration } from "webpack"; +import { Compiler, MultiCompiler, WebpackError, StatsOptions } from "webpack"; import { stringifyStream } from "@discoveryjs/json-ext"; import { Help, ParseOptions } from "commander"; @@ -1952,6 +1952,7 @@ class WebpackCLI implements IWebpackCLI { }, {}); config.path.set(config.options, mergedConfigPaths as unknown as string); } + return config; } From 95ca34e40f4ada9930c39bd6618863163250b7e4 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sun, 30 Apr 2023 09:41:09 +0530 Subject: [PATCH 06/65] fix: default mode should be production --- packages/dotenv-webpack-plugin/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index f4af7a7e1d9..ebd0adcdf59 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -19,7 +19,7 @@ class DotenvWebpackPlugin { apply(compiler) { const currentDirectory = path.resolve(process.cwd(), "environment"); - const mode = compiler.options.mode || "development"; + const mode = compiler.options.mode || "production"; // .local file variables will get precedence const environmentFiles = this.config.envFiles.length > 0 From 08d16ff5d2bf94f317e3cade1123d638c2752fe7 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sun, 30 Apr 2023 09:46:37 +0530 Subject: [PATCH 07/65] refactor: provide env files as default value --- packages/dotenv-webpack-plugin/src/index.js | 28 ++++++++++++--------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index ebd0adcdf59..4277e40aede 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -6,10 +6,17 @@ const { DefinePlugin } = require("webpack"); class DotenvWebpackPlugin { constructor(config = {}) { + const currentDirectory = path.resolve(process.cwd(), "environment"); + this.config = Object.assign( {}, { - envFiles: [], + envFiles: [ + `${currentDirectory}/.env`, // loaded in all cases + `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git + `${currentDirectory}/.env.[mode]`, // only loaded in specified mode + `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git + ], prefixes: ["process.env.", "import.meta.env."], }, config, @@ -17,19 +24,16 @@ class DotenvWebpackPlugin { } apply(compiler) { - const currentDirectory = path.resolve(process.cwd(), "environment"); - const mode = compiler.options.mode || "production"; + // .local file variables will get precedence - const environmentFiles = - this.config.envFiles.length > 0 - ? this.config.envFiles - : [ - `${currentDirectory}/.env`, // loaded in all cases - `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git - `${currentDirectory}/.env.${mode}`, // only loaded in specified mode - `${currentDirectory}/.env.${mode}.local`, // only loaded in specified mode, ignored by git - ]; + let environmentFiles = Array.isArray(this.config.envFiles) + ? this.config.envFiles + : [this.config.envFiles]; + + environmentFiles = environmentFiles.map((environmentFile) => + environmentFile.replace(/\[mode\]/g, mode), + ); let envVariables = {}; // parse environment vars from .env files From e2cf250c678e20baa72af519760c3a82b1dce87a Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 2 May 2023 21:34:50 +0530 Subject: [PATCH 08/65] feat: add webpack cli runner code --- packages/dotenv-webpack-plugin/package.json | 2 +- packages/webpack-cli/package.json | 1 + packages/webpack-cli/src/webpack-cli.ts | 21 +++++++++++++++++++++ yarn.lock | 10 ++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/dotenv-webpack-plugin/package.json b/packages/dotenv-webpack-plugin/package.json index 8e221701c4b..1cbc4c49044 100644 --- a/packages/dotenv-webpack-plugin/package.json +++ b/packages/dotenv-webpack-plugin/package.json @@ -2,7 +2,7 @@ "name": "dotenv-webpack-plugin", "version": "1.0.0", "description": "A webpack plugin to support env files", - "main": "index.js", + "main": "src/index.js", "repository": "https://github.com/webpack/webpack-cli", "author": "Burhanuddin Udaipurwala (burhanuday)", "license": "MIT", diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index cab9eeba764..7fca8e49fad 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -41,6 +41,7 @@ "colorette": "^2.0.14", "commander": "^10.0.1", "cross-spawn": "^7.0.3", + "dotenv-webpack-plugin": "1.0.0", "envinfo": "^7.7.3", "fastest-levenshtein": "^1.0.12", "import-local": "^3.0.2", diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 90c8f1ff402..f91c3e5c47a 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -56,6 +56,7 @@ const path = require("path"); const { pathToFileURL } = require("url"); const util = require("util"); const { program, Option } = require("commander"); +const DotenvWebpackPlugin = require("dotenv-webpack-plugin"); const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack"; const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server"; @@ -2054,6 +2055,26 @@ class WebpackCLI implements IWebpackCLI { config.path.set(config.options, mergedConfigPaths); } + const runFunctionOnEachConfig = ( + options: WebpackConfiguration | WebpackConfiguration[], + fn: CallableFunction, + ) => { + if (Array.isArray(options)) { + for (let item of options) { + item = fn(item); + } + } else { + options = fn(options); + } + + return options; + }; + + config.options = runFunctionOnEachConfig(config.options, (options: WebpackConfiguration) => { + options.plugins = options.plugins || []; + options.plugins.unshift(new DotenvWebpackPlugin()); + }); + return config; } diff --git a/yarn.lock b/yarn.lock index 02b0df8f7dd..80e1ee9f3cb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4197,6 +4197,16 @@ dot-prop@^5.1.0, dot-prop@^5.2.0: dependencies: is-obj "^2.0.0" +dotenv-expand@^10.0.0: + version "10.0.0" + resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37" + integrity sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A== + +dotenv@^16.0.3: + version "16.0.3" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07" + integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ== + dotenv@~10.0.0: version "10.0.0" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81" From fd6d2bab60230ea1d3112056f1154e2b3945ff73 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 3 May 2023 10:03:55 +0530 Subject: [PATCH 09/65] fix: add env plugin in build config --- packages/webpack-cli/src/webpack-cli.ts | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index f91c3e5c47a..1f36aa21228 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2055,26 +2055,6 @@ class WebpackCLI implements IWebpackCLI { config.path.set(config.options, mergedConfigPaths); } - const runFunctionOnEachConfig = ( - options: WebpackConfiguration | WebpackConfiguration[], - fn: CallableFunction, - ) => { - if (Array.isArray(options)) { - for (let item of options) { - item = fn(item); - } - } else { - options = fn(options); - } - - return options; - }; - - config.options = runFunctionOnEachConfig(config.options, (options: WebpackConfiguration) => { - options.plugins = options.plugins || []; - options.plugins.unshift(new DotenvWebpackPlugin()); - }); - return config; } @@ -2287,6 +2267,7 @@ class WebpackCLI implements IWebpackCLI { progress: options.progress, analyze: options.analyze, }), + new DotenvWebpackPlugin(), ); return options; From d5445972ddc5ccfb7604a745a0f2ea7d50735df8 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 3 May 2023 15:50:39 +0530 Subject: [PATCH 10/65] fix: add webpack as dev dep for dotenv webpack plugin --- packages/dotenv-webpack-plugin/package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/dotenv-webpack-plugin/package.json b/packages/dotenv-webpack-plugin/package.json index 1cbc4c49044..5adec716e12 100644 --- a/packages/dotenv-webpack-plugin/package.json +++ b/packages/dotenv-webpack-plugin/package.json @@ -13,5 +13,8 @@ }, "peerDependencies": { "webpack": "5.x.x" + }, + "devDependencies": { + "webpack": "^5.81.0" } } From b83ccaf9102a2822d5ddf3f1607fa410a9151a9f Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 3 May 2023 16:28:52 +0530 Subject: [PATCH 11/65] fix: incorrect path being used in dotenv --- packages/dotenv-webpack-plugin/src/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 4277e40aede..d4e3a1e28fd 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -1,12 +1,11 @@ const fs = require("fs"); -const path = require("path"); const dotenv = require("dotenv"); const dotenvExpand = require("dotenv-expand"); const { DefinePlugin } = require("webpack"); class DotenvWebpackPlugin { constructor(config = {}) { - const currentDirectory = path.resolve(process.cwd(), "environment"); + const currentDirectory = process.cwd(); this.config = Object.assign( {}, @@ -58,7 +57,7 @@ class DotenvWebpackPlugin { }); // expand environment vars - envVariables = dotenvExpand({ + envVariables = dotenvExpand.expand({ parsed: envVariables, // don't write to process.env ignoreProcessEnv: true, From a4388d777732065e1b438381b5bc39bbdf5b9af0 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 3 May 2023 17:04:34 +0530 Subject: [PATCH 12/65] test: add a simple test --- .../dotenv-webpack-plugin/builtin-config/.env | 2 ++ .../builtin-config/src/index.js | 3 ++ .../dotenv-webpack-plugin.test.js | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/builtin-config/.env create mode 100644 test/build/dotenv-webpack-plugin/builtin-config/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js diff --git a/test/build/dotenv-webpack-plugin/builtin-config/.env b/test/build/dotenv-webpack-plugin/builtin-config/.env new file mode 100644 index 00000000000..b0bcdf5335b --- /dev/null +++ b/test/build/dotenv-webpack-plugin/builtin-config/.env @@ -0,0 +1,2 @@ +WEBPACK_VARIABLE1=value1 +WEBPACK_VARIABLE2=value2 diff --git a/test/build/dotenv-webpack-plugin/builtin-config/src/index.js b/test/build/dotenv-webpack-plugin/builtin-config/src/index.js new file mode 100644 index 00000000000..2b7c82e322b --- /dev/null +++ b/test/build/dotenv-webpack-plugin/builtin-config/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js") +console.log(process.env.WEBPACK_VARIABLE1) +console.log(import.meta.env.WEBPACK_VARIABLE2) diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js new file mode 100644 index 00000000000..9993217996e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -0,0 +1,29 @@ +"use strict"; + +const { run, readFile } = require("../../utils/test-utils"); +const { resolve } = require("path"); +const { existsSync } = require("fs"); + +describe("dotenv-webpack-plugin", () => { + it("reads .env file and defines variables correctly", async () => { + const testDir = __dirname + "/builtin-config"; + const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); + + let data; + + try { + data = await readFile(resolve(testDir, "./dist/main.js"), "utf-8"); + } catch (error) { + expect(error).toBe(null); + } + + expect(data).toContain("Hello from index.js"); + expect(data).toContain("value1"); + expect(data).toContain("value2"); + }); +}); From ab007862fe30233d6b8c9b6977c30135cf063fb2 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 4 May 2023 09:32:58 +0530 Subject: [PATCH 13/65] fix: check if webpack is installed before adding dotenv plugin --- packages/webpack-cli/src/webpack-cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 1f36aa21228..daf8241b58a 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2267,7 +2267,7 @@ class WebpackCLI implements IWebpackCLI { progress: options.progress, analyze: options.analyze, }), - new DotenvWebpackPlugin(), + ...(this.checkPackageExists(WEBPACK_PACKAGE) ? [new DotenvWebpackPlugin()] : []), ); return options; From 4e039ba330cf961a2bc624fbb487396dcf2d53ec Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 4 May 2023 09:39:47 +0530 Subject: [PATCH 14/65] test: add test for value override --- .../dotenv-webpack-plugin.test.js | 23 +++++++++++++++++++ .../overrides-config/.env | 1 + .../overrides-config/.env.production | 1 + .../overrides-config/src/index.js | 3 +++ 4 files changed, 28 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/overrides-config/.env create mode 100644 test/build/dotenv-webpack-plugin/overrides-config/.env.production create mode 100644 test/build/dotenv-webpack-plugin/overrides-config/src/index.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 9993217996e..9949ca0de46 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -26,4 +26,27 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain("value1"); expect(data).toContain("value2"); }); + + // write a test to check if cli overrides variables in .env file when .env.production file is present + it("reads .env.production file and overrides values from .env variables correctly", async () => { + const testDir = __dirname + "/overrides-config"; + const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); + + let data; + + try { + data = await readFile(resolve(testDir, "./dist/main.js"), "utf-8"); + } catch (error) { + expect(error).toBe(null); + } + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",production_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",production_value'); + }); }); diff --git a/test/build/dotenv-webpack-plugin/overrides-config/.env b/test/build/dotenv-webpack-plugin/overrides-config/.env new file mode 100644 index 00000000000..aa585bc8fbf --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-config/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/overrides-config/.env.production b/test/build/dotenv-webpack-plugin/overrides-config/.env.production new file mode 100644 index 00000000000..04305888d86 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-config/.env.production @@ -0,0 +1 @@ +WEBPACK_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/overrides-config/src/index.js b/test/build/dotenv-webpack-plugin/overrides-config/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-config/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); From 379094528af453417deedf0cb9deaa3e45a8b45b Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 4 May 2023 09:45:17 +0530 Subject: [PATCH 15/65] test: add test for the case when variable does not start with webpack_ --- .../dotenv-webpack-plugin.test.js | 23 ++++++++++++++++++- .../non-webpack-variable/.env | 1 + .../non-webpack-variable/src/index.js | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/build/dotenv-webpack-plugin/non-webpack-variable/.env create mode 100644 test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 9949ca0de46..41cea24b7a1 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -27,7 +27,6 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain("value2"); }); - // write a test to check if cli overrides variables in .env file when .env.production file is present it("reads .env.production file and overrides values from .env variables correctly", async () => { const testDir = __dirname + "/overrides-config"; const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); @@ -49,4 +48,26 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain('"process.env.WEBPACK_VARIABLE:",production_value'); expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",production_value'); }); + + it("reads .env file and does not define a variable when it does not start with WEBPACK_", async () => { + const testDir = __dirname + "/non-webpack-variable"; + const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); + + let data; + + try { + data = await readFile(resolve(testDir, "./dist/main.js"), "utf-8"); + } catch (error) { + expect(error).toBe(null); + } + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.NON_WEBPACK_VARIABLE:",process.env.NON_WEBPACK_VARIABLE'); + expect(data).toContain('"import.meta.env.NON_WEBPACK_VARIABLE:",(void 0).NON_WEBPACK_VARIABLE'); + }); }); diff --git a/test/build/dotenv-webpack-plugin/non-webpack-variable/.env b/test/build/dotenv-webpack-plugin/non-webpack-variable/.env new file mode 100644 index 00000000000..9692641d1ef --- /dev/null +++ b/test/build/dotenv-webpack-plugin/non-webpack-variable/.env @@ -0,0 +1 @@ +NON_WEBPACK_VARIABLE=variable_value diff --git a/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js b/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js new file mode 100644 index 00000000000..2d3418bd30c --- /dev/null +++ b/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.NON_WEBPACK_VARIABLE:", process.env.NON_WEBPACK_VARIABLE); +console.log("import.meta.env.NON_WEBPACK_VARIABLE:", import.meta.env.NON_WEBPACK_VARIABLE); From c3d97d77f1a914a9a53bf9499fc9e9dceaf67f2d Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 4 May 2023 09:49:55 +0530 Subject: [PATCH 16/65] refactor: simplify test code --- .../dotenv-webpack-plugin.test.js | 54 ++++++++----------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 41cea24b7a1..456e5ea7a30 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -4,23 +4,29 @@ const { run, readFile } = require("../../utils/test-utils"); const { resolve } = require("path"); const { existsSync } = require("fs"); +const assertNoErrors = (exitCode, stderr, stdout, testDir) => { + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); +}; + +const getBuildOutput = async (testDir) => { + try { + return readFile(resolve(testDir, "./dist/main.js"), "utf-8"); + } catch (error) { + expect(error).toBe(null); + } +}; + describe("dotenv-webpack-plugin", () => { it("reads .env file and defines variables correctly", async () => { const testDir = __dirname + "/builtin-config"; const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); - expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); - - let data; + assertNoErrors(exitCode, stderr, stdout, testDir); - try { - data = await readFile(resolve(testDir, "./dist/main.js"), "utf-8"); - } catch (error) { - expect(error).toBe(null); - } + const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); expect(data).toContain("value1"); @@ -31,18 +37,9 @@ describe("dotenv-webpack-plugin", () => { const testDir = __dirname + "/overrides-config"; const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); - expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); + assertNoErrors(exitCode, stderr, stdout, testDir); - let data; - - try { - data = await readFile(resolve(testDir, "./dist/main.js"), "utf-8"); - } catch (error) { - expect(error).toBe(null); - } + const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); expect(data).toContain('"process.env.WEBPACK_VARIABLE:",production_value'); @@ -53,18 +50,9 @@ describe("dotenv-webpack-plugin", () => { const testDir = __dirname + "/non-webpack-variable"; const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); - expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); - - let data; + assertNoErrors(exitCode, stderr, stdout, testDir); - try { - data = await readFile(resolve(testDir, "./dist/main.js"), "utf-8"); - } catch (error) { - expect(error).toBe(null); - } + const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); expect(data).toContain('"process.env.NON_WEBPACK_VARIABLE:",process.env.NON_WEBPACK_VARIABLE'); From 1aef466419a60958dd87a4ec9599f4b3029ad854 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 4 May 2023 20:54:50 +0530 Subject: [PATCH 17/65] test: add missing assertion --- test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 456e5ea7a30..07b382474fa 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -57,5 +57,6 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain("Hello from index.js"); expect(data).toContain('"process.env.NON_WEBPACK_VARIABLE:",process.env.NON_WEBPACK_VARIABLE'); expect(data).toContain('"import.meta.env.NON_WEBPACK_VARIABLE:",(void 0).NON_WEBPACK_VARIABLE'); + expect(data).not.toContain("variable_value"); }); }); From bd6c4a603833d9202a9cfb9d61957f60a59008d5 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Thu, 4 May 2023 21:09:40 +0530 Subject: [PATCH 18/65] chore: debug failing smoketests --- packages/webpack-cli/src/webpack-cli.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index daf8241b58a..82514462a76 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -56,7 +56,6 @@ const path = require("path"); const { pathToFileURL } = require("url"); const util = require("util"); const { program, Option } = require("commander"); -const DotenvWebpackPlugin = require("dotenv-webpack-plugin"); const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack"; const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server"; @@ -2260,6 +2259,10 @@ class WebpackCLI implements IWebpackCLI { item.plugins = []; } + const DotenvWebpackPlugin = this.checkPackageExists("webpack") + ? require("dotenv-webpack-plugin") + : null; + item.plugins.unshift( new CLIPlugin({ configPath: config.path.get(item), @@ -2267,7 +2270,7 @@ class WebpackCLI implements IWebpackCLI { progress: options.progress, analyze: options.analyze, }), - ...(this.checkPackageExists(WEBPACK_PACKAGE) ? [new DotenvWebpackPlugin()] : []), + ...(DotenvWebpackPlugin ? [new DotenvWebpackPlugin()] : []), ); return options; From 74ad4c87d0cc203a5919accbecd2d27046b5f39b Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 6 May 2023 11:09:07 +0530 Subject: [PATCH 19/65] feat: add cli option --- packages/webpack-cli/src/webpack-cli.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 82514462a76..c39719983f4 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -987,6 +987,17 @@ class WebpackCLI implements IWebpackCLI { description: "Extend webpack configuration", helpLevel: "minimum", }, + { + name: "dot-env", + configs: [ + { + type: "enum", + values: [true], + }, + ], + description: "Read environment variables from .env files", + helpLevel: "minimum", + }, ]; const minimumHelpFlags = [ From 9286cb5801a27863a756a8ae43abc24b2c68240b Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 6 May 2023 11:43:01 +0530 Subject: [PATCH 20/65] refactor: redo applying the plugin --- packages/webpack-cli/src/types.ts | 1 + packages/webpack-cli/src/webpack-cli.ts | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/webpack-cli/src/types.ts b/packages/webpack-cli/src/types.ts index ec31fa9c364..67a2fa8f2d9 100644 --- a/packages/webpack-cli/src/types.ts +++ b/packages/webpack-cli/src/types.ts @@ -179,6 +179,7 @@ type WebpackDevServerOptions = DevServerConfig & configName?: string[]; disableInterpret?: boolean; extends?: string[]; + dotEnv?: boolean; argv: Argv; }; diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index fa6d9dff4de..7b9ad7736d4 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2273,10 +2273,6 @@ class WebpackCLI implements IWebpackCLI { item.plugins = []; } - const DotenvWebpackPlugin = this.checkPackageExists("webpack") - ? require("dotenv-webpack-plugin") - : null; - item.plugins.unshift( new CLIPlugin({ configPath: config.path.get(item), @@ -2284,12 +2280,30 @@ class WebpackCLI implements IWebpackCLI { progress: options.progress, analyze: options.analyze, }), - ...(DotenvWebpackPlugin ? [new DotenvWebpackPlugin()] : []), ); return options; }; + if (!!options.dotEnv && !this.checkPackageExists("webpack")) { + this.logger.error("The 'webpack' package is required to use the '--dot-env' option."); + } + + const shouldAddDotEnvPlugin = !!options.dotEnv && this.checkPackageExists("webpack"); + + if (shouldAddDotEnvPlugin) { + const DotenvWebpackPlugin = await this.tryRequireThenImport void, []>>( + "dotenv-webpack-plugin", + ); + runFunctionOnEachConfig(config.options, (item: WebpackConfiguration) => { + if (!item.plugins) { + item.plugins = []; + } + + item.plugins.unshift(new DotenvWebpackPlugin()); + }); + } + runFunctionOnEachConfig(config.options, internalBuildConfig); return config; From 8689a2e83896149d3f02a5465c553414518e8b2c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 6 May 2023 12:01:53 +0530 Subject: [PATCH 21/65] test: update snapshots --- .../help.test.js.snap.devServer4.webpack5 | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 index ab8413515a6..cb2681b61a5 100644 --- a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 +++ b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 @@ -105,10 +105,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -166,10 +166,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -227,10 +227,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -287,10 +287,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -335,10 +335,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -389,14 +389,13 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -454,14 +453,13 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -513,10 +511,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -561,10 +559,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1543,10 +1541,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1589,10 +1587,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1641,14 +1639,13 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -1704,14 +1701,13 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -1761,10 +1757,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1807,10 +1803,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1854,10 +1850,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1915,10 +1911,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -2166,10 +2162,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -2225,10 +2221,10 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). From ca6fc8d243455a281ff87f9af38bd9ac92411a6c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 6 May 2023 12:04:43 +0530 Subject: [PATCH 22/65] test: update tests --- .../dotenv-webpack-plugin.test.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 07b382474fa..21e57a625a1 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -22,7 +22,11 @@ const getBuildOutput = async (testDir) => { describe("dotenv-webpack-plugin", () => { it("reads .env file and defines variables correctly", async () => { const testDir = __dirname + "/builtin-config"; - const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--dot-env", + ]); assertNoErrors(exitCode, stderr, stdout, testDir); @@ -35,7 +39,11 @@ describe("dotenv-webpack-plugin", () => { it("reads .env.production file and overrides values from .env variables correctly", async () => { const testDir = __dirname + "/overrides-config"; - const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--dot-env", + ]); assertNoErrors(exitCode, stderr, stdout, testDir); @@ -48,7 +56,11 @@ describe("dotenv-webpack-plugin", () => { it("reads .env file and does not define a variable when it does not start with WEBPACK_", async () => { const testDir = __dirname + "/non-webpack-variable"; - const { exitCode, stderr, stdout } = await run(testDir, ["--entry", "./src/index.js"]); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--dot-env", + ]); assertNoErrors(exitCode, stderr, stdout, testDir); From 4388c69b72fbd216704bc57309876e98f9b1a703 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 6 May 2023 12:16:38 +0530 Subject: [PATCH 23/65] test: update snapshots --- package.json | 2 +- .../help.test.js.snap.devServer4.webpack5 | 23 +++++++++++++++++++ yarn.lock | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 578dd8c72cb..4b096dee245 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "test": "jest --reporters=default", "test:smoketests": "nyc node smoketests", "test:coverage": "nyc --no-clean jest", - "test:cli": "jest test --reporters=default", + "test:cli": "jest help.test --reporters=default", "test:packages": "jest packages/ --reporters=default", "test:ci": "yarn test:cli && yarn test:packages", "test:watch": "jest test/ packages/ --watch", diff --git a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 index cb2681b61a5..6409a85a87b 100644 --- a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 +++ b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 @@ -109,6 +109,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -170,6 +171,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -231,6 +233,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -291,6 +294,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -339,6 +343,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -396,6 +401,8 @@ Options: --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only + works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -460,6 +467,8 @@ Options: --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only + works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -515,6 +524,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -563,6 +573,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1545,6 +1556,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1591,6 +1603,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1646,6 +1659,8 @@ Options: --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only + works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -1708,6 +1723,8 @@ Options: --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only + works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading @@ -1761,6 +1778,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1807,6 +1825,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1854,6 +1873,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -1915,6 +1935,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -2166,6 +2187,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). @@ -2225,6 +2247,7 @@ Options: -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). --mode Enable production optimizations or development hints. --name Name of the configuration. Used when loading multiple configurations. -o, --output-path The output directory as **absolute path** (required). diff --git a/yarn.lock b/yarn.lock index a2f9cebb9de..68b2ef4c82b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10161,7 +10161,7 @@ webpack-sources@^3.2.3: resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde" integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w== -webpack@^5.72.0: +webpack@^5.72.0, webpack@^5.81.0: version "5.82.0" resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.82.0.tgz#3c0d074dec79401db026b4ba0fb23d6333f88e7d" integrity sha512-iGNA2fHhnDcV1bONdUu554eZx+XeldsaeQ8T67H6KKHl2nUSwX8Zm7cmzOA46ox/X1ARxf7Bjv8wQ/HsB5fxBg== From 7ab13cf8aca1cb302481e04d6b8afe2e3a9f55b3 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 6 May 2023 12:17:41 +0530 Subject: [PATCH 24/65] fix: undo package json changes --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4b096dee245..578dd8c72cb 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "test": "jest --reporters=default", "test:smoketests": "nyc node smoketests", "test:coverage": "nyc --no-clean jest", - "test:cli": "jest help.test --reporters=default", + "test:cli": "jest test --reporters=default", "test:packages": "jest packages/ --reporters=default", "test:ci": "yarn test:cli && yarn test:packages", "test:watch": "jest test/ packages/ --watch", From bf8cb58e7d498df458cd927da3ddfee6b1421b6e Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 19:56:58 +0530 Subject: [PATCH 25/65] feat: add .example env file --- packages/dotenv-webpack-plugin/src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index d4e3a1e28fd..be89aba0c51 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -11,6 +11,7 @@ class DotenvWebpackPlugin { {}, { envFiles: [ + `${currentDirectory}/.env.example`, // loaded in all cases `${currentDirectory}/.env`, // loaded in all cases `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git `${currentDirectory}/.env.[mode]`, // only loaded in specified mode From ff7246f9adf79e631bda5250f97e52eefad53adc Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 20:00:20 +0530 Subject: [PATCH 26/65] chore: move dotenv webpack plugin to peerdeps --- packages/webpack-cli/package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 7fca8e49fad..a200e39576d 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -41,7 +41,6 @@ "colorette": "^2.0.14", "commander": "^10.0.1", "cross-spawn": "^7.0.3", - "dotenv-webpack-plugin": "1.0.0", "envinfo": "^7.7.3", "fastest-levenshtein": "^1.0.12", "import-local": "^3.0.2", @@ -59,6 +58,9 @@ "@webpack-cli/generators": { "optional": true }, + "dotenv-webpack-plugin": { + "optional": true + }, "webpack-bundle-analyzer": { "optional": true }, From 9d7e1dbbf99917ae0db8f1aea9f9a72bd78fbc69 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 20:05:05 +0530 Subject: [PATCH 27/65] feat: rename cli flag --- packages/webpack-cli/src/webpack-cli.ts | 8 ++++---- .../dotenv-webpack-plugin.test.js | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 7b9ad7736d4..25a433fc62d 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -990,7 +990,7 @@ class WebpackCLI implements IWebpackCLI { helpLevel: "minimum", }, { - name: "dot-env", + name: "read-dot-env", configs: [ { type: "enum", @@ -2285,11 +2285,11 @@ class WebpackCLI implements IWebpackCLI { return options; }; - if (!!options.dotEnv && !this.checkPackageExists("webpack")) { - this.logger.error("The 'webpack' package is required to use the '--dot-env' option."); + if (!!options.readDotEnv && !this.checkPackageExists("webpack")) { + this.logger.error("The 'webpack' package is required to use the '--read-dot-env' option."); } - const shouldAddDotEnvPlugin = !!options.dotEnv && this.checkPackageExists("webpack"); + const shouldAddDotEnvPlugin = !!options.readDotEnv && this.checkPackageExists("webpack"); if (shouldAddDotEnvPlugin) { const DotenvWebpackPlugin = await this.tryRequireThenImport void, []>>( diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 21e57a625a1..6f6ee9ea3a2 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -1,7 +1,7 @@ "use strict"; const { run, readFile } = require("../../utils/test-utils"); -const { resolve } = require("path"); +const { resolve, join } = require("path"); const { existsSync } = require("fs"); const assertNoErrors = (exitCode, stderr, stdout, testDir) => { @@ -21,11 +21,11 @@ const getBuildOutput = async (testDir) => { describe("dotenv-webpack-plugin", () => { it("reads .env file and defines variables correctly", async () => { - const testDir = __dirname + "/builtin-config"; + const testDir = join(__dirname, "/builtin-config"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", - "--dot-env", + "--read-dot-env", ]); assertNoErrors(exitCode, stderr, stdout, testDir); @@ -38,11 +38,11 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env.production file and overrides values from .env variables correctly", async () => { - const testDir = __dirname + "/overrides-config"; + const testDir = join(__dirname, "/overrides-config"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", - "--dot-env", + "--read-dot-env", ]); assertNoErrors(exitCode, stderr, stdout, testDir); @@ -55,11 +55,11 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env file and does not define a variable when it does not start with WEBPACK_", async () => { - const testDir = __dirname + "/non-webpack-variable"; + const testDir = join(__dirname, "/non-webpack-variable"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", - "--dot-env", + "--read-dot-env", ]); assertNoErrors(exitCode, stderr, stdout, testDir); From 8a45c8bd7d3d9145c31168b7ad7a1ef4dbb1f466 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 20:08:15 +0530 Subject: [PATCH 28/65] test: update snapshots --- .../help.test.js.snap.devServer4.webpack5 | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 index 6409a85a87b..8a518fd0ad8 100644 --- a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 +++ b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 @@ -105,7 +105,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -167,7 +167,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -229,7 +229,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -290,7 +290,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -339,7 +339,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -394,7 +394,7 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). @@ -460,7 +460,7 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). @@ -520,7 +520,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -569,7 +569,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -1552,7 +1552,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -1599,7 +1599,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -1652,7 +1652,7 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). @@ -1716,7 +1716,7 @@ Options: on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). @@ -1774,7 +1774,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -1821,7 +1821,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -1869,7 +1869,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -1931,7 +1931,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -2183,7 +2183,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. @@ -2243,7 +2243,7 @@ Options: -j, --json [value] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --dot-env Read environment variables from .env files + --read-dot-env Read environment variables from .env files -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). --no-devtool Negative 'devtool' option. --entry A module that is loaded upon startup. Only the last one is exported. From f4b88356f1e529806459c0634f73445ba096c670 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 20:53:24 +0530 Subject: [PATCH 29/65] test: add more test cases --- .../dotenv-webpack-plugin.test.js | 100 ++++++++++++++++++ .../env-example/.env.example | 1 + .../env-example/src/index.js | 3 + .../mode-development/.env | 1 + .../mode-development/.env.development | 1 + .../mode-development/.env.production | 1 + .../mode-development/src/index.js | 3 + .../dotenv-webpack-plugin/mode-none/.env | 1 + .../mode-none/.env.development | 1 + .../dotenv-webpack-plugin/mode-none/.env.none | 1 + .../mode-none/.env.production | 1 + .../mode-none/src/index.js | 3 + .../mode-production/.env | 1 + .../mode-production/.env.development | 1 + .../mode-production/.env.production | 1 + .../mode-production/src/index.js | 3 + .../overrides-local/.env | 1 + .../overrides-local/.env.local | 1 + .../overrides-local/src/index.js | 3 + 19 files changed, 128 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/env-example/.env.example create mode 100644 test/build/dotenv-webpack-plugin/env-example/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/mode-development/.env create mode 100644 test/build/dotenv-webpack-plugin/mode-development/.env.development create mode 100644 test/build/dotenv-webpack-plugin/mode-development/.env.production create mode 100644 test/build/dotenv-webpack-plugin/mode-development/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/mode-none/.env create mode 100644 test/build/dotenv-webpack-plugin/mode-none/.env.development create mode 100644 test/build/dotenv-webpack-plugin/mode-none/.env.none create mode 100644 test/build/dotenv-webpack-plugin/mode-none/.env.production create mode 100644 test/build/dotenv-webpack-plugin/mode-none/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/mode-production/.env create mode 100644 test/build/dotenv-webpack-plugin/mode-production/.env.development create mode 100644 test/build/dotenv-webpack-plugin/mode-production/.env.production create mode 100644 test/build/dotenv-webpack-plugin/mode-production/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/overrides-local/.env create mode 100644 test/build/dotenv-webpack-plugin/overrides-local/.env.local create mode 100644 test/build/dotenv-webpack-plugin/overrides-local/src/index.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 6f6ee9ea3a2..078bea8ef3f 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -71,4 +71,104 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain('"import.meta.env.NON_WEBPACK_VARIABLE:",(void 0).NON_WEBPACK_VARIABLE'); expect(data).not.toContain("variable_value"); }); + + it("reads .env.production when mode is set to production", async () => { + const testDir = join(__dirname, "/mode-production"); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--read-dot-env", + "--mode", + "production", + ]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",production_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",production_value'); + expect(data).not.toContain("default_value"); + expect(data).not.toContain("development_value"); + }); + + it("reads .env.development when mode is set to development", async () => { + const testDir = join(__dirname, "/mode-development"); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--read-dot-env", + "--mode", + "development", + ]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + // TODO check why webpack adds "\\" to the value only in development mode + expect(data).toContain('"process.env.WEBPACK_VARIABLE:\\", development_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:\\", development_value'); + expect(data).not.toContain("default_value"); + expect(data).not.toContain("production_value"); + }); + + it("reads .env.none when mode is set to none", async () => { + const testDir = join(__dirname, "/mode-none"); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--read-dot-env", + "--mode", + "none", + ]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:", none_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:", none_value'); + expect(data).not.toContain("default_value"); + expect(data).not.toContain("production_value"); + expect(data).not.toContain("development_value"); + }); + + it("reads .env.example when file is present", async () => { + const testDir = join(__dirname, "/env-example"); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--read-dot-env", + ]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",example_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",example_value'); + }); + + it("overrides value from .env when same key in .env.local is present", async () => { + const testDir = join(__dirname, "/overrides-local"); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--read-dot-env", + ]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",local_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",local_value'); + expect(data).not.toContain("default_value"); + }); }); diff --git a/test/build/dotenv-webpack-plugin/env-example/.env.example b/test/build/dotenv-webpack-plugin/env-example/.env.example new file mode 100644 index 00000000000..40bc6de3178 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/env-example/.env.example @@ -0,0 +1 @@ +WEBPACK_VARIABLE=example_value diff --git a/test/build/dotenv-webpack-plugin/env-example/src/index.js b/test/build/dotenv-webpack-plugin/env-example/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/env-example/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/mode-development/.env b/test/build/dotenv-webpack-plugin/mode-development/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-development/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/mode-development/.env.development b/test/build/dotenv-webpack-plugin/mode-development/.env.development new file mode 100644 index 00000000000..aa585bc8fbf --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-development/.env.development @@ -0,0 +1 @@ +WEBPACK_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/mode-development/.env.production b/test/build/dotenv-webpack-plugin/mode-development/.env.production new file mode 100644 index 00000000000..04305888d86 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-development/.env.production @@ -0,0 +1 @@ +WEBPACK_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/mode-development/src/index.js b/test/build/dotenv-webpack-plugin/mode-development/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-development/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env b/test/build/dotenv-webpack-plugin/mode-none/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-none/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env.development b/test/build/dotenv-webpack-plugin/mode-none/.env.development new file mode 100644 index 00000000000..aa585bc8fbf --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-none/.env.development @@ -0,0 +1 @@ +WEBPACK_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env.none b/test/build/dotenv-webpack-plugin/mode-none/.env.none new file mode 100644 index 00000000000..a91edb105dc --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-none/.env.none @@ -0,0 +1 @@ +WEBPACK_VARIABLE=none_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env.production b/test/build/dotenv-webpack-plugin/mode-none/.env.production new file mode 100644 index 00000000000..04305888d86 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-none/.env.production @@ -0,0 +1 @@ +WEBPACK_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/src/index.js b/test/build/dotenv-webpack-plugin/mode-none/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-none/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/mode-production/.env b/test/build/dotenv-webpack-plugin/mode-production/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-production/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/mode-production/.env.development b/test/build/dotenv-webpack-plugin/mode-production/.env.development new file mode 100644 index 00000000000..aa585bc8fbf --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-production/.env.development @@ -0,0 +1 @@ +WEBPACK_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/mode-production/.env.production b/test/build/dotenv-webpack-plugin/mode-production/.env.production new file mode 100644 index 00000000000..04305888d86 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-production/.env.production @@ -0,0 +1 @@ +WEBPACK_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/mode-production/src/index.js b/test/build/dotenv-webpack-plugin/mode-production/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/mode-production/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/overrides-local/.env b/test/build/dotenv-webpack-plugin/overrides-local/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-local/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/overrides-local/.env.local b/test/build/dotenv-webpack-plugin/overrides-local/.env.local new file mode 100644 index 00000000000..8393069e112 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-local/.env.local @@ -0,0 +1 @@ +WEBPACK_VARIABLE=local_value diff --git a/test/build/dotenv-webpack-plugin/overrides-local/src/index.js b/test/build/dotenv-webpack-plugin/overrides-local/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-local/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); From 9ee072df7848dc61442a29728706179bf2aa57e3 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 20:56:55 +0530 Subject: [PATCH 30/65] test: add test case for mode local files --- .../dotenv-webpack-plugin.test.js | 18 ++++++++++++++++++ .../overrides-mode-local/.env.production | 1 + .../overrides-mode-local/.env.production.local | 1 + .../overrides-mode-local/src/index.js | 3 +++ 4 files changed, 23 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production create mode 100644 test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local create mode 100644 test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 078bea8ef3f..d113575c213 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -171,4 +171,22 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",local_value'); expect(data).not.toContain("default_value"); }); + + it("overrides value from .env.[mode] when same key in .env.[mode].local is present", async () => { + const testDir = join(__dirname, "/overrides-local"); + const { exitCode, stderr, stdout } = await run(testDir, [ + "--entry", + "./src/index.js", + "--read-dot-env", + ]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",local_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",local_value'); + expect(data).not.toContain("production_value"); + }); }); diff --git a/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production new file mode 100644 index 00000000000..04305888d86 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production @@ -0,0 +1 @@ +WEBPACK_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local new file mode 100644 index 00000000000..8393069e112 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local @@ -0,0 +1 @@ +WEBPACK_VARIABLE=local_value diff --git a/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js b/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); From f7f79b8bfdb082bd68ac555695f38924541ab79f Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 20:59:06 +0530 Subject: [PATCH 31/65] fix: update types --- packages/webpack-cli/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/src/types.ts b/packages/webpack-cli/src/types.ts index 0da72148c8d..f0044bd4c47 100644 --- a/packages/webpack-cli/src/types.ts +++ b/packages/webpack-cli/src/types.ts @@ -178,7 +178,7 @@ type WebpackDevServerOptions = DevServerConfig & configName?: string[]; disableInterpret?: boolean; extends?: string[]; - dotEnv?: boolean; + readDotEnv?: boolean; argv: Argv; }; From 88d3660ce5e39f4da336c23aca92feb0be8ccf1e Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 8 May 2023 21:30:16 +0530 Subject: [PATCH 32/65] test: add test case for multiple configs --- .gitignore | 2 ++ .../dotenv-webpack-plugin.test.js | 27 ++++++++++++++++--- .../multiple-configs/.env | 1 + .../multiple-configs/src/index.js | 3 +++ .../multiple-configs/src/index2.js | 3 +++ .../multiple-configs/webpack.config.js | 18 +++++++++++++ 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 test/build/dotenv-webpack-plugin/multiple-configs/.env create mode 100644 test/build/dotenv-webpack-plugin/multiple-configs/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js create mode 100644 test/build/dotenv-webpack-plugin/multiple-configs/webpack.config.js diff --git a/.gitignore b/.gitignore index d1c154e5dd0..66ad094ccf6 100644 --- a/.gitignore +++ b/.gitignore @@ -64,5 +64,7 @@ test/**/**/**/bin/ test/**/**/binary/** test/**/dist test/**/**/dist +test/**/**/dist1 +test/**/**/dist2 test/**/**/**/dist test/**/stats.json diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index d113575c213..b08d3a5b48e 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -4,16 +4,16 @@ const { run, readFile } = require("../../utils/test-utils"); const { resolve, join } = require("path"); const { existsSync } = require("fs"); -const assertNoErrors = (exitCode, stderr, stdout, testDir) => { +const assertNoErrors = (exitCode, stderr, stdout, testDir, buildPath = "dist") => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); - expect(existsSync(resolve(testDir, "./dist/main.js"))).toBeTruthy(); + expect(existsSync(resolve(testDir, join(buildPath, "main.js")))).toBeTruthy(); }; -const getBuildOutput = async (testDir) => { +const getBuildOutput = async (testDir, buildPath = "dist") => { try { - return readFile(resolve(testDir, "./dist/main.js"), "utf-8"); + return readFile(resolve(testDir, join(buildPath, "main.js")), "utf-8"); } catch (error) { expect(error).toBe(null); } @@ -189,4 +189,23 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",local_value'); expect(data).not.toContain("production_value"); }); + + it("reads .env file and applies for all configs", async () => { + const testDir = join(__dirname, "/multiple-configs"); + const { exitCode, stderr, stdout } = await run(testDir, ["--read-dot-env"]); + + assertNoErrors(exitCode, stderr, stdout, testDir, "dist1"); + + let data = await getBuildOutput(testDir, "dist1"); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); + + data = await getBuildOutput(testDir, "dist2"); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); + }); }); diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/.env b/test/build/dotenv-webpack-plugin/multiple-configs/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/multiple-configs/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js b/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js b/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/webpack.config.js b/test/build/dotenv-webpack-plugin/multiple-configs/webpack.config.js new file mode 100644 index 00000000000..940a6abe120 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/multiple-configs/webpack.config.js @@ -0,0 +1,18 @@ +const { join } = require("path"); + +module.exports = [ + { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist1"), + }, + }, + { + entry: "./src/index2.js", + mode: "production", + output: { + path: join(__dirname, "dist2"), + }, + }, +]; From 0158dca41dc9c3578af089c55e8cc22194bc6cc6 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 9 May 2023 21:18:06 +0530 Subject: [PATCH 33/65] fix: do validation of passed config --- packages/dotenv-webpack-plugin/src/index.js | 28 ++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index be89aba0c51..086d6a0c8a3 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -7,7 +7,7 @@ class DotenvWebpackPlugin { constructor(config = {}) { const currentDirectory = process.cwd(); - this.config = Object.assign( + this.options = Object.assign( {}, { envFiles: [ @@ -26,12 +26,21 @@ class DotenvWebpackPlugin { apply(compiler) { const mode = compiler.options.mode || "production"; - // .local file variables will get precedence - let environmentFiles = Array.isArray(this.config.envFiles) - ? this.config.envFiles - : [this.config.envFiles]; + // throw error if envFiles is not an array + if (!Array.isArray(this.options.envFiles)) { + const logger = compiler.getInfrastructureLogger("webpack-cli"); + logger.error(`envFiles option must be an array, received ${typeof this.options.envFiles}`); + return; + } - environmentFiles = environmentFiles.map((environmentFile) => + // throw error if prefixes is not an array + if (!Array.isArray(this.options.prefixes)) { + const logger = compiler.getInfrastructureLogger("webpack-cli"); + logger.error(`prefixes option must be an array, received ${typeof this.options.prefixes}`); + return; + } + + const environmentFiles = this.options.envFiles.map((environmentFile) => environmentFile.replace(/\[mode\]/g, mode), ); @@ -45,14 +54,15 @@ class DotenvWebpackPlugin { for (const [key, value] of Object.entries(parsedEnvVariables)) { // only add variables starting with WEBPACK_ if (key.startsWith("WEBPACK_")) { - for (let index = 0; index < this.config.prefixes.length; index++) { - const prefix = this.config.prefixes[index]; + for (let index = 0; index < this.options.prefixes.length; index++) { + const prefix = this.options.prefixes[index]; envVariables[`${prefix}${key}`] = value; } } } } catch (err) { - this.logger.error(`Could not read ${environmentFile}`); + const logger = compiler.getInfrastructureLogger("webpack-cli"); + logger.error(`Could not read ${environmentFile}`); } } }); From f8a6142cb4f22053c4e9cde903894fd3c2b04559 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 9 May 2023 21:18:50 +0530 Subject: [PATCH 34/65] test: add more test cases --- .../custom-config-path/.env.vars | 1 + .../custom-config-path/src/index.js | 3 + .../custom-config-path/webpack.config.js | 15 +++++ .../dotenv-webpack-plugin/custom-prefix/.env | 1 + .../custom-prefix/src/index.js | 2 + .../custom-prefix/webpack.config.js | 15 +++++ .../dotenv-webpack-plugin.test.js | 59 +++++++++++++++---- .../validate-config-paths/.env | 1 + .../validate-config-paths/src/index.js | 2 + .../validate-config-paths/webpack.config.js | 15 +++++ .../validates-prefixes/.env | 1 + .../validates-prefixes/src/index.js | 2 + .../validates-prefixes/webpack.config.js | 15 +++++ 13 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 test/build/dotenv-webpack-plugin/custom-config-path/.env.vars create mode 100644 test/build/dotenv-webpack-plugin/custom-config-path/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/custom-config-path/webpack.config.js create mode 100644 test/build/dotenv-webpack-plugin/custom-prefix/.env create mode 100644 test/build/dotenv-webpack-plugin/custom-prefix/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/custom-prefix/webpack.config.js create mode 100644 test/build/dotenv-webpack-plugin/validate-config-paths/.env create mode 100644 test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/validate-config-paths/webpack.config.js create mode 100644 test/build/dotenv-webpack-plugin/validates-prefixes/.env create mode 100644 test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/validates-prefixes/webpack.config.js diff --git a/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars b/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js b/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js new file mode 100644 index 00000000000..e20057fd299 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js @@ -0,0 +1,3 @@ +console.log("Hello from index.js"); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); +console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/custom-config-path/webpack.config.js b/test/build/dotenv-webpack-plugin/custom-config-path/webpack.config.js new file mode 100644 index 00000000000..a918ac8f2c1 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/custom-config-path/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + envFiles: ["./.env.vars"], + }), + ], +}; diff --git a/test/build/dotenv-webpack-plugin/custom-prefix/.env b/test/build/dotenv-webpack-plugin/custom-prefix/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/custom-prefix/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js b/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js new file mode 100644 index 00000000000..6654609228d --- /dev/null +++ b/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js @@ -0,0 +1,2 @@ +console.log("Hello from index.js"); +console.log("custom_prefix_WEBPACK_VARIABLE:", custom_prefix_WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/custom-prefix/webpack.config.js b/test/build/dotenv-webpack-plugin/custom-prefix/webpack.config.js new file mode 100644 index 00000000000..6bcd31d6e49 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/custom-prefix/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + prefixes: ["custom_prefix_"], + }), + ], +}; diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index b08d3a5b48e..90628a23dbb 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -21,7 +21,7 @@ const getBuildOutput = async (testDir, buildPath = "dist") => { describe("dotenv-webpack-plugin", () => { it("reads .env file and defines variables correctly", async () => { - const testDir = join(__dirname, "/builtin-config"); + const testDir = join(__dirname, "builtin-config"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -38,7 +38,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env.production file and overrides values from .env variables correctly", async () => { - const testDir = join(__dirname, "/overrides-config"); + const testDir = join(__dirname, "overrides-config"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -55,7 +55,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env file and does not define a variable when it does not start with WEBPACK_", async () => { - const testDir = join(__dirname, "/non-webpack-variable"); + const testDir = join(__dirname, "non-webpack-variable"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -73,7 +73,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env.production when mode is set to production", async () => { - const testDir = join(__dirname, "/mode-production"); + const testDir = join(__dirname, "mode-production"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -94,7 +94,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env.development when mode is set to development", async () => { - const testDir = join(__dirname, "/mode-development"); + const testDir = join(__dirname, "mode-development"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -116,7 +116,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env.none when mode is set to none", async () => { - const testDir = join(__dirname, "/mode-none"); + const testDir = join(__dirname, "mode-none"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -138,7 +138,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env.example when file is present", async () => { - const testDir = join(__dirname, "/env-example"); + const testDir = join(__dirname, "env-example"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -155,7 +155,7 @@ describe("dotenv-webpack-plugin", () => { }); it("overrides value from .env when same key in .env.local is present", async () => { - const testDir = join(__dirname, "/overrides-local"); + const testDir = join(__dirname, "overrides-local"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -173,7 +173,7 @@ describe("dotenv-webpack-plugin", () => { }); it("overrides value from .env.[mode] when same key in .env.[mode].local is present", async () => { - const testDir = join(__dirname, "/overrides-local"); + const testDir = join(__dirname, "overrides-local"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", "./src/index.js", @@ -191,7 +191,7 @@ describe("dotenv-webpack-plugin", () => { }); it("reads .env file and applies for all configs", async () => { - const testDir = join(__dirname, "/multiple-configs"); + const testDir = join(__dirname, "multiple-configs"); const { exitCode, stderr, stdout } = await run(testDir, ["--read-dot-env"]); assertNoErrors(exitCode, stderr, stdout, testDir, "dist1"); @@ -208,4 +208,43 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); }); + + it("reads env vars from a custom file path correctly", async () => { + const testDir = join(__dirname, "custom-config-path"); + const { exitCode, stderr, stdout } = await run(testDir); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); + }); + + it("creates variables with custom prefixes when passed", async () => { + const testDir = join(__dirname, "custom-prefix"); + const { exitCode, stderr, stdout } = await run(testDir); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain('"custom_prefix_WEBPACK_VARIABLE:",default_value'); + }); + + it("validates env files paths", async () => { + const testDir = join(__dirname, "validate-config-paths"); + const { stderr } = await run(testDir); + + expect(stderr).toContain("envFiles option must be an array, received string"); + }); + + it("validates custom prefixes", async () => { + const testDir = join(__dirname, "validates-prefixes"); + const { stderr } = await run(testDir); + + expect(stderr).toContain("prefixes option must be an array, received string"); + }); }); diff --git a/test/build/dotenv-webpack-plugin/validate-config-paths/.env b/test/build/dotenv-webpack-plugin/validate-config-paths/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validate-config-paths/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js b/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js new file mode 100644 index 00000000000..6654609228d --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js @@ -0,0 +1,2 @@ +console.log("Hello from index.js"); +console.log("custom_prefix_WEBPACK_VARIABLE:", custom_prefix_WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/validate-config-paths/webpack.config.js b/test/build/dotenv-webpack-plugin/validate-config-paths/webpack.config.js new file mode 100644 index 00000000000..9fbb84427cf --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validate-config-paths/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + envFiles: "./.env.vars", + }), + ], +}; diff --git a/test/build/dotenv-webpack-plugin/validates-prefixes/.env b/test/build/dotenv-webpack-plugin/validates-prefixes/.env new file mode 100644 index 00000000000..994d1cf0c6e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-prefixes/.env @@ -0,0 +1 @@ +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js b/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js new file mode 100644 index 00000000000..6654609228d --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js @@ -0,0 +1,2 @@ +console.log("Hello from index.js"); +console.log("custom_prefix_WEBPACK_VARIABLE:", custom_prefix_WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/validates-prefixes/webpack.config.js b/test/build/dotenv-webpack-plugin/validates-prefixes/webpack.config.js new file mode 100644 index 00000000000..592ef5a9c31 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-prefixes/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + prefixes: "custom_prefix_", + }), + ], +}; From a165f38c15879da2829dbe767cbd3c76cb7b9df6 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 10 May 2023 08:56:17 +0530 Subject: [PATCH 35/65] fix: use correct logger name --- packages/dotenv-webpack-plugin/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 086d6a0c8a3..a68600ae283 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -28,14 +28,14 @@ class DotenvWebpackPlugin { // throw error if envFiles is not an array if (!Array.isArray(this.options.envFiles)) { - const logger = compiler.getInfrastructureLogger("webpack-cli"); + const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); logger.error(`envFiles option must be an array, received ${typeof this.options.envFiles}`); return; } // throw error if prefixes is not an array if (!Array.isArray(this.options.prefixes)) { - const logger = compiler.getInfrastructureLogger("webpack-cli"); + const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); logger.error(`prefixes option must be an array, received ${typeof this.options.prefixes}`); return; } @@ -61,7 +61,7 @@ class DotenvWebpackPlugin { } } } catch (err) { - const logger = compiler.getInfrastructureLogger("webpack-cli"); + const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); logger.error(`Could not read ${environmentFile}`); } } From 06399c5230e8aeb70adb0c6302d50caf9823ee80 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 10 May 2023 09:07:13 +0530 Subject: [PATCH 36/65] feat: add types and jsdoc --- packages/dotenv-webpack-plugin/package.json | 1 + packages/dotenv-webpack-plugin/src/index.js | 11 +++++++++++ packages/dotenv-webpack-plugin/src/types.d.ts | 4 ++++ 3 files changed, 16 insertions(+) create mode 100644 packages/dotenv-webpack-plugin/src/types.d.ts diff --git a/packages/dotenv-webpack-plugin/package.json b/packages/dotenv-webpack-plugin/package.json index 5adec716e12..4e929a4efb9 100644 --- a/packages/dotenv-webpack-plugin/package.json +++ b/packages/dotenv-webpack-plugin/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "A webpack plugin to support env files", "main": "src/index.js", + "types": "src/types.d.ts", "repository": "https://github.com/webpack/webpack-cli", "author": "Burhanuddin Udaipurwala (burhanuday)", "license": "MIT", diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index a68600ae283..491a5752ed4 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -3,7 +3,13 @@ const dotenv = require("dotenv"); const dotenvExpand = require("dotenv-expand"); const { DefinePlugin } = require("webpack"); +/** @typedef {import("./types").Config} Config */ + class DotenvWebpackPlugin { + /** + * Dotenv Webpack Plugin + * @param {Config} config - Configuration options + */ constructor(config = {}) { const currentDirectory = process.cwd(); @@ -23,6 +29,11 @@ class DotenvWebpackPlugin { ); } + /** + * Webpack apply hook + * @param {Object} compiler - Webpack compiler + * @returns {void} + */ apply(compiler) { const mode = compiler.options.mode || "production"; diff --git a/packages/dotenv-webpack-plugin/src/types.d.ts b/packages/dotenv-webpack-plugin/src/types.d.ts new file mode 100644 index 00000000000..ff9b1e33ce0 --- /dev/null +++ b/packages/dotenv-webpack-plugin/src/types.d.ts @@ -0,0 +1,4 @@ +export type Config = { + envFiles: string[]; + prefixes: string[]; +}; From e00469cf9bae348e06800af97e29fd8a9a76a74c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 15 May 2023 19:33:43 +0530 Subject: [PATCH 37/65] chore: bump From d89d73cecd5a83093e3ecc20b3b784a029951312 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 16 May 2023 21:28:44 +0530 Subject: [PATCH 38/65] feat: add schema utils package --- packages/dotenv-webpack-plugin/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/dotenv-webpack-plugin/package.json b/packages/dotenv-webpack-plugin/package.json index 4e929a4efb9..22903fdec80 100644 --- a/packages/dotenv-webpack-plugin/package.json +++ b/packages/dotenv-webpack-plugin/package.json @@ -10,7 +10,8 @@ "private": true, "dependencies": { "dotenv": "^16.0.3", - "dotenv-expand": "^10.0.0" + "dotenv-expand": "^10.0.0", + "schema-utils": "^4.0.1" }, "peerDependencies": { "webpack": "5.x.x" From 0c477b0a3f0a1181036b26d55a47e6fc1c9fb284 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 16 May 2023 21:37:57 +0530 Subject: [PATCH 39/65] feat: use schema utils package to validate input --- packages/dotenv-webpack-plugin/src/index.js | 28 ++++++++----------- .../dotenv-webpack-plugin/src/options.json | 22 +++++++++++++++ 2 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 packages/dotenv-webpack-plugin/src/options.json diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 491a5752ed4..291e82c53b8 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -2,15 +2,23 @@ const fs = require("fs"); const dotenv = require("dotenv"); const dotenvExpand = require("dotenv-expand"); const { DefinePlugin } = require("webpack"); +const { validate } = require("schema-utils"); +const schema = require("./options.json"); /** @typedef {import("./types").Config} Config */ +/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ class DotenvWebpackPlugin { /** * Dotenv Webpack Plugin - * @param {Config} config - Configuration options + * @param {Config} options - Configuration options */ - constructor(config = {}) { + constructor(options = {}) { + validate(/** @type {Schema} */ (schema), options || {}, { + name: "DotenvWebpackPlugin", + baseDataPath: "options", + }); + const currentDirectory = process.cwd(); this.options = Object.assign( @@ -25,7 +33,7 @@ class DotenvWebpackPlugin { ], prefixes: ["process.env.", "import.meta.env."], }, - config, + options, ); } @@ -37,20 +45,6 @@ class DotenvWebpackPlugin { apply(compiler) { const mode = compiler.options.mode || "production"; - // throw error if envFiles is not an array - if (!Array.isArray(this.options.envFiles)) { - const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); - logger.error(`envFiles option must be an array, received ${typeof this.options.envFiles}`); - return; - } - - // throw error if prefixes is not an array - if (!Array.isArray(this.options.prefixes)) { - const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); - logger.error(`prefixes option must be an array, received ${typeof this.options.prefixes}`); - return; - } - const environmentFiles = this.options.envFiles.map((environmentFile) => environmentFile.replace(/\[mode\]/g, mode), ); diff --git a/packages/dotenv-webpack-plugin/src/options.json b/packages/dotenv-webpack-plugin/src/options.json new file mode 100644 index 00000000000..82feaf1f0e7 --- /dev/null +++ b/packages/dotenv-webpack-plugin/src/options.json @@ -0,0 +1,22 @@ +{ + "definitions": {}, + "title": "DotenvWebpackPlugin", + "type": "object", + "additionalProperties": false, + "properties": { + "envFiles": { + "description": "The paths to the .env files to load", + "type": "array", + "items": { + "type": "string" + } + }, + "prefixes": { + "description": "The prefixes to use for the environment variables", + "type": "array", + "items": { + "type": "string" + } + } + } +} From 8911ba731075c9abb0dcb13a0d458c290c6ee202 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 16 May 2023 21:38:14 +0530 Subject: [PATCH 40/65] test: update tests with new validation errors --- .../build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 90628a23dbb..ca841dcfa7d 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -238,13 +238,13 @@ describe("dotenv-webpack-plugin", () => { const testDir = join(__dirname, "validate-config-paths"); const { stderr } = await run(testDir); - expect(stderr).toContain("envFiles option must be an array, received string"); + expect(stderr).toContain("options.envFiles should be an array"); }); it("validates custom prefixes", async () => { const testDir = join(__dirname, "validates-prefixes"); const { stderr } = await run(testDir); - expect(stderr).toContain("prefixes option must be an array, received string"); + expect(stderr).toContain("options.prefixes should be an array"); }); }); From 00fb0b8a766c0ee1ac4c8b59f123f104abc09d2c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 16 May 2023 21:40:13 +0530 Subject: [PATCH 41/65] refactor: avoid using object assign --- packages/dotenv-webpack-plugin/src/index.js | 29 +++++++++++---------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 291e82c53b8..f307f13b729 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -21,20 +21,21 @@ class DotenvWebpackPlugin { const currentDirectory = process.cwd(); - this.options = Object.assign( - {}, - { - envFiles: [ - `${currentDirectory}/.env.example`, // loaded in all cases - `${currentDirectory}/.env`, // loaded in all cases - `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git - `${currentDirectory}/.env.[mode]`, // only loaded in specified mode - `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git - ], - prefixes: ["process.env.", "import.meta.env."], - }, - options, - ); + const { + envFiles = [ + `${currentDirectory}/.env.example`, // loaded in all cases + `${currentDirectory}/.env`, // loaded in all cases + `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git + `${currentDirectory}/.env.[mode]`, // only loaded in specified mode + `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git + ], + prefixes = ["process.env.", "import.meta.env."], + } = options; + + this.options = { + envFiles, + prefixes, + }; } /** From cdeb09f7145e7d28f29b15bf262ac28ae10db845 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 16 May 2023 22:41:10 +0530 Subject: [PATCH 42/65] perf: use webpack fs for better perf --- packages/dotenv-webpack-plugin/src/index.js | 90 ++++++++++++++------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index f307f13b729..a6cf7c75686 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -1,4 +1,3 @@ -const fs = require("fs"); const dotenv = require("dotenv"); const dotenvExpand = require("dotenv-expand"); const { DefinePlugin } = require("webpack"); @@ -7,6 +6,7 @@ const schema = require("./options.json"); /** @typedef {import("./types").Config} Config */ /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ +/** @typedef {import("webpack").Compiler} Compiler */ class DotenvWebpackPlugin { /** @@ -39,23 +39,29 @@ class DotenvWebpackPlugin { } /** - * Webpack apply hook - * @param {Object} compiler - Webpack compiler - * @returns {void} + * Read file from path and parse it + * @param {Compiler} compiler - Webpack compiler + * @param {string} environmentFile - Path to environment file */ - apply(compiler) { - const mode = compiler.options.mode || "production"; + readFile(compiler, environmentFile) { + return new Promise((resolve, reject) => { + const envVariables = {}; - const environmentFiles = this.options.envFiles.map((environmentFile) => - environmentFile.replace(/\[mode\]/g, mode), - ); + const fs = compiler.inputFileSystem; + + fs.stat(environmentFile, (err) => { + // File does not exist + if (err) { + return resolve(); + } + + fs.readFile(environmentFile, (err, environmentFileContents) => { + if (err) { + const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); + logger.error(`Could not read ${environmentFile}`); + return reject(err); + } - let envVariables = {}; - // parse environment vars from .env files - environmentFiles.forEach((environmentFile) => { - if (fs.existsSync(environmentFile)) { - try { - const environmentFileContents = fs.readFileSync(environmentFile); const parsedEnvVariables = dotenv.parse(environmentFileContents); for (const [key, value] of Object.entries(parsedEnvVariables)) { // only add variables starting with WEBPACK_ @@ -66,21 +72,51 @@ class DotenvWebpackPlugin { } } } - } catch (err) { - const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); - logger.error(`Could not read ${environmentFile}`); - } - } + + resolve(envVariables); + }); + }); }); + } - // expand environment vars - envVariables = dotenvExpand.expand({ - parsed: envVariables, - // don't write to process.env - ignoreProcessEnv: true, - }).parsed; + /** + * Webpack apply hook + * @param {Compiler} compiler - Webpack compiler + * @returns {void} + */ + apply(compiler) { + const mode = compiler.options.mode || "production"; + + const environmentFiles = this.options.envFiles.map((environmentFile) => + environmentFile.replace(/\[mode\]/g, mode), + ); - new DefinePlugin(envVariables).apply(compiler); + compiler.hooks.beforeRun.tapPromise("DotenvWebpackPlugin", () => { + return Promise.all( + environmentFiles.map((environmentFile) => this.readFile(compiler, environmentFile)), + ) + .then((valuesList) => { + let envVariables = {}; + + valuesList.forEach((values) => { + if (values) { + Object.entries(values).forEach(([key, value]) => { + envVariables[key] = value; + }); + } + }); + + // expand environment vars + envVariables = dotenvExpand.expand({ + parsed: envVariables, + // don't write to process.env + ignoreProcessEnv: true, + }).parsed; + + new DefinePlugin(envVariables).apply(compiler); + }) + .catch(() => {}); + }); } } From 07dbcd5ef6af802e03b28fbd1daf5df2c5c4c49c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 17 May 2023 08:44:43 +0530 Subject: [PATCH 43/65] feat: add env files to build dependencies --- packages/dotenv-webpack-plugin/src/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index a6cf7c75686..850c31ef34d 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -91,7 +91,11 @@ class DotenvWebpackPlugin { environmentFile.replace(/\[mode\]/g, mode), ); - compiler.hooks.beforeRun.tapPromise("DotenvWebpackPlugin", () => { + compiler.hooks.beforeRun.tapPromise("DotenvWebpackPlugin", (compiler) => { + compiler.hooks.compilation.tap("DotenvWebpackPlugin", (compilation) => { + compilation.buildDependencies.addAll(environmentFiles); + }); + return Promise.all( environmentFiles.map((environmentFile) => this.readFile(compiler, environmentFile)), ) From 99a716350da119898dba6e94e7f24a7a1f7cb6a4 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 17 May 2023 08:54:46 +0530 Subject: [PATCH 44/65] refactor: extract file exists into a function --- packages/dotenv-webpack-plugin/src/index.js | 25 ++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 850c31ef34d..3db0bc5876d 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -38,6 +38,26 @@ class DotenvWebpackPlugin { }; } + /** + * Check if file exists + * @param {Compiler} compiler - Webpack compiler + * @param {string} environmentFile - Path to environment file + */ + fileExists(compiler, environmentFile) { + return new Promise((resolve) => { + const fs = compiler.inputFileSystem; + + fs.stat(environmentFile, (err) => { + // File does not exist + if (err) { + return resolve(false); + } + + return resolve(environmentFile); + }); + }); + } + /** * Read file from path and parse it * @param {Compiler} compiler - Webpack compiler @@ -49,9 +69,8 @@ class DotenvWebpackPlugin { const fs = compiler.inputFileSystem; - fs.stat(environmentFile, (err) => { - // File does not exist - if (err) { + this.fileExists(compiler, environmentFile).then((exists) => { + if (!exists) { return resolve(); } From 03d285c5e22bc99e65965bd0afd1cc1a54eabe46 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 17 May 2023 08:56:04 +0530 Subject: [PATCH 45/65] fix: use push instead of unshift for perf --- packages/webpack-cli/src/webpack-cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 5aa3de75ecd..dd8fa6d4766 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2294,7 +2294,7 @@ class WebpackCLI implements IWebpackCLI { item.plugins = []; } - item.plugins.unshift(new DotenvWebpackPlugin()); + item.plugins.push(new DotenvWebpackPlugin()); }; if (Array.isArray(config.options)) { From 7de589269f2959142f1c038bdc40a5222c89980e Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 17 May 2023 10:17:00 +0530 Subject: [PATCH 46/65] feat: make env var prefix configurable --- packages/dotenv-webpack-plugin/src/index.js | 6 ++++-- packages/dotenv-webpack-plugin/src/options.json | 4 ++++ packages/dotenv-webpack-plugin/src/types.d.ts | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 3db0bc5876d..53ebe7a2b29 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -30,11 +30,13 @@ class DotenvWebpackPlugin { `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git ], prefixes = ["process.env.", "import.meta.env."], + envVarPrefix = "WEBPACK_", } = options; this.options = { envFiles, prefixes, + envVarPrefix, }; } @@ -83,8 +85,8 @@ class DotenvWebpackPlugin { const parsedEnvVariables = dotenv.parse(environmentFileContents); for (const [key, value] of Object.entries(parsedEnvVariables)) { - // only add variables starting with WEBPACK_ - if (key.startsWith("WEBPACK_")) { + // only add variables starting with the provided prefix + if (key.startsWith(this.options.envVarPrefix)) { for (let index = 0; index < this.options.prefixes.length; index++) { const prefix = this.options.prefixes[index]; envVariables[`${prefix}${key}`] = value; diff --git a/packages/dotenv-webpack-plugin/src/options.json b/packages/dotenv-webpack-plugin/src/options.json index 82feaf1f0e7..51ae05d5bb3 100644 --- a/packages/dotenv-webpack-plugin/src/options.json +++ b/packages/dotenv-webpack-plugin/src/options.json @@ -17,6 +17,10 @@ "items": { "type": "string" } + }, + "envVarPrefix": { + "description": "The prefix to use for the environment variables", + "type": "string" } } } diff --git a/packages/dotenv-webpack-plugin/src/types.d.ts b/packages/dotenv-webpack-plugin/src/types.d.ts index ff9b1e33ce0..8e34285d428 100644 --- a/packages/dotenv-webpack-plugin/src/types.d.ts +++ b/packages/dotenv-webpack-plugin/src/types.d.ts @@ -1,4 +1,5 @@ export type Config = { envFiles: string[]; prefixes: string[]; + envVarPrefix: string; }; From 07ac04e463a72305be0eabc067672ca7ecd00bb9 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 17 May 2023 10:23:36 +0530 Subject: [PATCH 47/65] test: add tests for env var prefix --- .../dotenv-webpack-plugin.test.js | 12 ++++++++++++ .../dotenv-webpack-plugin/env-var-prefix/.env | 1 + .../env-var-prefix/src/index.js | 2 ++ .../env-var-prefix/webpack.config.js | 15 +++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/env-var-prefix/.env create mode 100644 test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index ca841dcfa7d..444b41f0713 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -247,4 +247,16 @@ describe("dotenv-webpack-plugin", () => { expect(stderr).toContain("options.prefixes should be an array"); }); + + it("uses the passed env var prefix correctly", async () => { + const testDir = join(__dirname, "env-var-prefix"); + const { exitCode, stderr, stdout } = await run(testDir, ["--read-dot-env"]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain("default_value"); + }); }); diff --git a/test/build/dotenv-webpack-plugin/env-var-prefix/.env b/test/build/dotenv-webpack-plugin/env-var-prefix/.env new file mode 100644 index 00000000000..2eaacc7cf87 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/env-var-prefix/.env @@ -0,0 +1 @@ +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js b/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js new file mode 100644 index 00000000000..db1bb2d502e --- /dev/null +++ b/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js @@ -0,0 +1,2 @@ +console.log("Hello from index.js"); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js b/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js new file mode 100644 index 00000000000..be66c5e22a7 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + envVarPrefix: "PUBLIC_", + }), + ], +}; From a3e81fa560a82590e4053ca0e09d55ca8aeae888 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Wed, 17 May 2023 10:28:21 +0530 Subject: [PATCH 48/65] test: update tests --- packages/dotenv-webpack-plugin/src/index.js | 2 +- .../dotenv-webpack-plugin/builtin-config/.env | 4 +- .../builtin-config/src/index.js | 4 +- .../custom-config-path/.env.vars | 2 +- .../custom-config-path/src/index.js | 4 +- .../dotenv-webpack-plugin/custom-prefix/.env | 2 +- .../custom-prefix/src/index.js | 2 +- .../dotenv-webpack-plugin.test.js | 48 +++++++++---------- .../env-example/.env.example | 2 +- .../env-example/src/index.js | 4 +- .../dotenv-webpack-plugin/env-var-prefix/.env | 2 +- .../env-var-prefix/src/index.js | 2 +- .../env-var-prefix/webpack.config.js | 2 +- .../mode-development/.env | 2 +- .../mode-development/.env.development | 2 +- .../mode-development/.env.production | 2 +- .../mode-development/src/index.js | 4 +- .../dotenv-webpack-plugin/mode-none/.env | 2 +- .../mode-none/.env.development | 2 +- .../dotenv-webpack-plugin/mode-none/.env.none | 2 +- .../mode-none/.env.production | 2 +- .../mode-none/src/index.js | 4 +- .../mode-production/.env | 2 +- .../mode-production/.env.development | 2 +- .../mode-production/.env.production | 2 +- .../mode-production/src/index.js | 4 +- .../multiple-configs/.env | 2 +- .../multiple-configs/src/index.js | 4 +- .../multiple-configs/src/index2.js | 4 +- .../non-webpack-variable/.env | 2 +- .../non-webpack-variable/src/index.js | 4 +- .../overrides-config/.env | 2 +- .../overrides-config/.env.production | 2 +- .../overrides-config/src/index.js | 4 +- .../overrides-local/.env | 2 +- .../overrides-local/.env.local | 2 +- .../overrides-local/src/index.js | 4 +- .../overrides-mode-local/.env.production | 2 +- .../.env.production.local | 2 +- .../overrides-mode-local/src/index.js | 4 +- .../validate-config-paths/.env | 2 +- .../validate-config-paths/src/index.js | 2 +- .../validates-prefixes/.env | 2 +- .../validates-prefixes/src/index.js | 2 +- 44 files changed, 80 insertions(+), 80 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 53ebe7a2b29..a8b58aeb40c 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -30,7 +30,7 @@ class DotenvWebpackPlugin { `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git ], prefixes = ["process.env.", "import.meta.env."], - envVarPrefix = "WEBPACK_", + envVarPrefix = "PUBLIC_", } = options; this.options = { diff --git a/test/build/dotenv-webpack-plugin/builtin-config/.env b/test/build/dotenv-webpack-plugin/builtin-config/.env index b0bcdf5335b..c5dda70fc93 100644 --- a/test/build/dotenv-webpack-plugin/builtin-config/.env +++ b/test/build/dotenv-webpack-plugin/builtin-config/.env @@ -1,2 +1,2 @@ -WEBPACK_VARIABLE1=value1 -WEBPACK_VARIABLE2=value2 +PUBLIC_VARIABLE1=value1 +PUBLIC_VARIABLE2=value2 diff --git a/test/build/dotenv-webpack-plugin/builtin-config/src/index.js b/test/build/dotenv-webpack-plugin/builtin-config/src/index.js index 2b7c82e322b..600e863e06b 100644 --- a/test/build/dotenv-webpack-plugin/builtin-config/src/index.js +++ b/test/build/dotenv-webpack-plugin/builtin-config/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js") -console.log(process.env.WEBPACK_VARIABLE1) -console.log(import.meta.env.WEBPACK_VARIABLE2) +console.log(process.env.PUBLIC_VARIABLE1) +console.log(import.meta.env.PUBLIC_VARIABLE2) diff --git a/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars b/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars +++ b/test/build/dotenv-webpack-plugin/custom-config-path/.env.vars @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js b/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js +++ b/test/build/dotenv-webpack-plugin/custom-config-path/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/custom-prefix/.env b/test/build/dotenv-webpack-plugin/custom-prefix/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/custom-prefix/.env +++ b/test/build/dotenv-webpack-plugin/custom-prefix/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js b/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js index 6654609228d..4a13d4605af 100644 --- a/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js +++ b/test/build/dotenv-webpack-plugin/custom-prefix/src/index.js @@ -1,2 +1,2 @@ console.log("Hello from index.js"); -console.log("custom_prefix_WEBPACK_VARIABLE:", custom_prefix_WEBPACK_VARIABLE); +console.log("custom_prefix_PUBLIC_VARIABLE:", custom_prefix_PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 444b41f0713..4ea18a8bf79 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -50,11 +50,11 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",production_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",production_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",production_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",production_value'); }); - it("reads .env file and does not define a variable when it does not start with WEBPACK_", async () => { + it("reads .env file and does not define a variable when it does not start with PUBLIC_", async () => { const testDir = join(__dirname, "non-webpack-variable"); const { exitCode, stderr, stdout } = await run(testDir, [ "--entry", @@ -67,8 +67,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.NON_WEBPACK_VARIABLE:",process.env.NON_WEBPACK_VARIABLE'); - expect(data).toContain('"import.meta.env.NON_WEBPACK_VARIABLE:",(void 0).NON_WEBPACK_VARIABLE'); + expect(data).toContain('"process.env.NON_PUBLIC_VARIABLE:",process.env.NON_PUBLIC_VARIABLE'); + expect(data).toContain('"import.meta.env.NON_PUBLIC_VARIABLE:",(void 0).NON_PUBLIC_VARIABLE'); expect(data).not.toContain("variable_value"); }); @@ -87,8 +87,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",production_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",production_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",production_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",production_value'); expect(data).not.toContain("default_value"); expect(data).not.toContain("development_value"); }); @@ -109,8 +109,8 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain("Hello from index.js"); // TODO check why webpack adds "\\" to the value only in development mode - expect(data).toContain('"process.env.WEBPACK_VARIABLE:\\", development_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:\\", development_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:\\", development_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:\\", development_value'); expect(data).not.toContain("default_value"); expect(data).not.toContain("production_value"); }); @@ -130,8 +130,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:", none_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:", none_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:", none_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:", none_value'); expect(data).not.toContain("default_value"); expect(data).not.toContain("production_value"); expect(data).not.toContain("development_value"); @@ -150,8 +150,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",example_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",example_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",example_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",example_value'); }); it("overrides value from .env when same key in .env.local is present", async () => { @@ -167,8 +167,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",local_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",local_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",local_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",local_value'); expect(data).not.toContain("default_value"); }); @@ -185,8 +185,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",local_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",local_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",local_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",local_value'); expect(data).not.toContain("production_value"); }); @@ -199,14 +199,14 @@ describe("dotenv-webpack-plugin", () => { let data = await getBuildOutput(testDir, "dist1"); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",default_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",default_value'); data = await getBuildOutput(testDir, "dist2"); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",default_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",default_value'); }); it("reads env vars from a custom file path correctly", async () => { @@ -218,8 +218,8 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"process.env.WEBPACK_VARIABLE:",default_value'); - expect(data).toContain('"import.meta.env.WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"process.env.PUBLIC_VARIABLE:",default_value'); + expect(data).toContain('"import.meta.env.PUBLIC_VARIABLE:",default_value'); }); it("creates variables with custom prefixes when passed", async () => { @@ -231,7 +231,7 @@ describe("dotenv-webpack-plugin", () => { const data = await getBuildOutput(testDir); expect(data).toContain("Hello from index.js"); - expect(data).toContain('"custom_prefix_WEBPACK_VARIABLE:",default_value'); + expect(data).toContain('"custom_prefix_PUBLIC_VARIABLE:",default_value'); }); it("validates env files paths", async () => { diff --git a/test/build/dotenv-webpack-plugin/env-example/.env.example b/test/build/dotenv-webpack-plugin/env-example/.env.example index 40bc6de3178..5bc1af18652 100644 --- a/test/build/dotenv-webpack-plugin/env-example/.env.example +++ b/test/build/dotenv-webpack-plugin/env-example/.env.example @@ -1 +1 @@ -WEBPACK_VARIABLE=example_value +PUBLIC_VARIABLE=example_value diff --git a/test/build/dotenv-webpack-plugin/env-example/src/index.js b/test/build/dotenv-webpack-plugin/env-example/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/env-example/src/index.js +++ b/test/build/dotenv-webpack-plugin/env-example/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/env-var-prefix/.env b/test/build/dotenv-webpack-plugin/env-var-prefix/.env index 2eaacc7cf87..994d1cf0c6e 100644 --- a/test/build/dotenv-webpack-plugin/env-var-prefix/.env +++ b/test/build/dotenv-webpack-plugin/env-var-prefix/.env @@ -1 +1 @@ -PUBLIC_VARIABLE=default_value +WEBPACK_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js b/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js index db1bb2d502e..23aac4380ff 100644 --- a/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js +++ b/test/build/dotenv-webpack-plugin/env-var-prefix/src/index.js @@ -1,2 +1,2 @@ console.log("Hello from index.js"); -console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js b/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js index be66c5e22a7..673849dcf0a 100644 --- a/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js +++ b/test/build/dotenv-webpack-plugin/env-var-prefix/webpack.config.js @@ -9,7 +9,7 @@ module.exports = { }, plugins: [ new DotenvWebpackPlugin({ - envVarPrefix: "PUBLIC_", + envVarPrefix: "WEBPACK_", }), ], }; diff --git a/test/build/dotenv-webpack-plugin/mode-development/.env b/test/build/dotenv-webpack-plugin/mode-development/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/mode-development/.env +++ b/test/build/dotenv-webpack-plugin/mode-development/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/mode-development/.env.development b/test/build/dotenv-webpack-plugin/mode-development/.env.development index aa585bc8fbf..a58008f1597 100644 --- a/test/build/dotenv-webpack-plugin/mode-development/.env.development +++ b/test/build/dotenv-webpack-plugin/mode-development/.env.development @@ -1 +1 @@ -WEBPACK_VARIABLE=development_value +PUBLIC_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/mode-development/.env.production b/test/build/dotenv-webpack-plugin/mode-development/.env.production index 04305888d86..abd00c1fc28 100644 --- a/test/build/dotenv-webpack-plugin/mode-development/.env.production +++ b/test/build/dotenv-webpack-plugin/mode-development/.env.production @@ -1 +1 @@ -WEBPACK_VARIABLE=production_value +PUBLIC_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/mode-development/src/index.js b/test/build/dotenv-webpack-plugin/mode-development/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/mode-development/src/index.js +++ b/test/build/dotenv-webpack-plugin/mode-development/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env b/test/build/dotenv-webpack-plugin/mode-none/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/mode-none/.env +++ b/test/build/dotenv-webpack-plugin/mode-none/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env.development b/test/build/dotenv-webpack-plugin/mode-none/.env.development index aa585bc8fbf..a58008f1597 100644 --- a/test/build/dotenv-webpack-plugin/mode-none/.env.development +++ b/test/build/dotenv-webpack-plugin/mode-none/.env.development @@ -1 +1 @@ -WEBPACK_VARIABLE=development_value +PUBLIC_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env.none b/test/build/dotenv-webpack-plugin/mode-none/.env.none index a91edb105dc..cf185ab413b 100644 --- a/test/build/dotenv-webpack-plugin/mode-none/.env.none +++ b/test/build/dotenv-webpack-plugin/mode-none/.env.none @@ -1 +1 @@ -WEBPACK_VARIABLE=none_value +PUBLIC_VARIABLE=none_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/.env.production b/test/build/dotenv-webpack-plugin/mode-none/.env.production index 04305888d86..abd00c1fc28 100644 --- a/test/build/dotenv-webpack-plugin/mode-none/.env.production +++ b/test/build/dotenv-webpack-plugin/mode-none/.env.production @@ -1 +1 @@ -WEBPACK_VARIABLE=production_value +PUBLIC_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/mode-none/src/index.js b/test/build/dotenv-webpack-plugin/mode-none/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/mode-none/src/index.js +++ b/test/build/dotenv-webpack-plugin/mode-none/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/mode-production/.env b/test/build/dotenv-webpack-plugin/mode-production/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/mode-production/.env +++ b/test/build/dotenv-webpack-plugin/mode-production/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/mode-production/.env.development b/test/build/dotenv-webpack-plugin/mode-production/.env.development index aa585bc8fbf..a58008f1597 100644 --- a/test/build/dotenv-webpack-plugin/mode-production/.env.development +++ b/test/build/dotenv-webpack-plugin/mode-production/.env.development @@ -1 +1 @@ -WEBPACK_VARIABLE=development_value +PUBLIC_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/mode-production/.env.production b/test/build/dotenv-webpack-plugin/mode-production/.env.production index 04305888d86..abd00c1fc28 100644 --- a/test/build/dotenv-webpack-plugin/mode-production/.env.production +++ b/test/build/dotenv-webpack-plugin/mode-production/.env.production @@ -1 +1 @@ -WEBPACK_VARIABLE=production_value +PUBLIC_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/mode-production/src/index.js b/test/build/dotenv-webpack-plugin/mode-production/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/mode-production/src/index.js +++ b/test/build/dotenv-webpack-plugin/mode-production/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/.env b/test/build/dotenv-webpack-plugin/multiple-configs/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/multiple-configs/.env +++ b/test/build/dotenv-webpack-plugin/multiple-configs/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js b/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js +++ b/test/build/dotenv-webpack-plugin/multiple-configs/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js b/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js +++ b/test/build/dotenv-webpack-plugin/multiple-configs/src/index2.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/non-webpack-variable/.env b/test/build/dotenv-webpack-plugin/non-webpack-variable/.env index 9692641d1ef..a8532c76489 100644 --- a/test/build/dotenv-webpack-plugin/non-webpack-variable/.env +++ b/test/build/dotenv-webpack-plugin/non-webpack-variable/.env @@ -1 +1 @@ -NON_WEBPACK_VARIABLE=variable_value +NON_PUBLIC_VARIABLE=variable_value diff --git a/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js b/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js index 2d3418bd30c..830999536e6 100644 --- a/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js +++ b/test/build/dotenv-webpack-plugin/non-webpack-variable/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.NON_WEBPACK_VARIABLE:", process.env.NON_WEBPACK_VARIABLE); -console.log("import.meta.env.NON_WEBPACK_VARIABLE:", import.meta.env.NON_WEBPACK_VARIABLE); +console.log("process.env.NON_PUBLIC_VARIABLE:", process.env.NON_PUBLIC_VARIABLE); +console.log("import.meta.env.NON_PUBLIC_VARIABLE:", import.meta.env.NON_PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/overrides-config/.env b/test/build/dotenv-webpack-plugin/overrides-config/.env index aa585bc8fbf..a58008f1597 100644 --- a/test/build/dotenv-webpack-plugin/overrides-config/.env +++ b/test/build/dotenv-webpack-plugin/overrides-config/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=development_value +PUBLIC_VARIABLE=development_value diff --git a/test/build/dotenv-webpack-plugin/overrides-config/.env.production b/test/build/dotenv-webpack-plugin/overrides-config/.env.production index 04305888d86..abd00c1fc28 100644 --- a/test/build/dotenv-webpack-plugin/overrides-config/.env.production +++ b/test/build/dotenv-webpack-plugin/overrides-config/.env.production @@ -1 +1 @@ -WEBPACK_VARIABLE=production_value +PUBLIC_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/overrides-config/src/index.js b/test/build/dotenv-webpack-plugin/overrides-config/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/overrides-config/src/index.js +++ b/test/build/dotenv-webpack-plugin/overrides-config/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/overrides-local/.env b/test/build/dotenv-webpack-plugin/overrides-local/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/overrides-local/.env +++ b/test/build/dotenv-webpack-plugin/overrides-local/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/overrides-local/.env.local b/test/build/dotenv-webpack-plugin/overrides-local/.env.local index 8393069e112..de73c9136ca 100644 --- a/test/build/dotenv-webpack-plugin/overrides-local/.env.local +++ b/test/build/dotenv-webpack-plugin/overrides-local/.env.local @@ -1 +1 @@ -WEBPACK_VARIABLE=local_value +PUBLIC_VARIABLE=local_value diff --git a/test/build/dotenv-webpack-plugin/overrides-local/src/index.js b/test/build/dotenv-webpack-plugin/overrides-local/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/overrides-local/src/index.js +++ b/test/build/dotenv-webpack-plugin/overrides-local/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production index 04305888d86..abd00c1fc28 100644 --- a/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production +++ b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production @@ -1 +1 @@ -WEBPACK_VARIABLE=production_value +PUBLIC_VARIABLE=production_value diff --git a/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local index 8393069e112..de73c9136ca 100644 --- a/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local +++ b/test/build/dotenv-webpack-plugin/overrides-mode-local/.env.production.local @@ -1 +1 @@ -WEBPACK_VARIABLE=local_value +PUBLIC_VARIABLE=local_value diff --git a/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js b/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js index e20057fd299..c8ae6a18f7b 100644 --- a/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js +++ b/test/build/dotenv-webpack-plugin/overrides-mode-local/src/index.js @@ -1,3 +1,3 @@ console.log("Hello from index.js"); -console.log("process.env.WEBPACK_VARIABLE:", process.env.WEBPACK_VARIABLE); -console.log("import.meta.env.WEBPACK_VARIABLE:", import.meta.env.WEBPACK_VARIABLE); +console.log("process.env.PUBLIC_VARIABLE:", process.env.PUBLIC_VARIABLE); +console.log("import.meta.env.PUBLIC_VARIABLE:", import.meta.env.PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/validate-config-paths/.env b/test/build/dotenv-webpack-plugin/validate-config-paths/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/validate-config-paths/.env +++ b/test/build/dotenv-webpack-plugin/validate-config-paths/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js b/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js index 6654609228d..4a13d4605af 100644 --- a/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js +++ b/test/build/dotenv-webpack-plugin/validate-config-paths/src/index.js @@ -1,2 +1,2 @@ console.log("Hello from index.js"); -console.log("custom_prefix_WEBPACK_VARIABLE:", custom_prefix_WEBPACK_VARIABLE); +console.log("custom_prefix_PUBLIC_VARIABLE:", custom_prefix_PUBLIC_VARIABLE); diff --git a/test/build/dotenv-webpack-plugin/validates-prefixes/.env b/test/build/dotenv-webpack-plugin/validates-prefixes/.env index 994d1cf0c6e..2eaacc7cf87 100644 --- a/test/build/dotenv-webpack-plugin/validates-prefixes/.env +++ b/test/build/dotenv-webpack-plugin/validates-prefixes/.env @@ -1 +1 @@ -WEBPACK_VARIABLE=default_value +PUBLIC_VARIABLE=default_value diff --git a/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js b/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js index 6654609228d..4a13d4605af 100644 --- a/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js +++ b/test/build/dotenv-webpack-plugin/validates-prefixes/src/index.js @@ -1,2 +1,2 @@ console.log("Hello from index.js"); -console.log("custom_prefix_WEBPACK_VARIABLE:", custom_prefix_WEBPACK_VARIABLE); +console.log("custom_prefix_PUBLIC_VARIABLE:", custom_prefix_PUBLIC_VARIABLE); From c191cb28061507d7641b362708eee57536e06d04 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 10:36:28 +0530 Subject: [PATCH 49/65] feat: update priority order to make it same as next --- packages/dotenv-webpack-plugin/src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index a8b58aeb40c..4bb9acaae0c 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -22,11 +22,13 @@ class DotenvWebpackPlugin { const currentDirectory = process.cwd(); const { + // priority is in ascending order + // files at the end of the array have higher priority envFiles = [ `${currentDirectory}/.env.example`, // loaded in all cases `${currentDirectory}/.env`, // loaded in all cases - `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git `${currentDirectory}/.env.[mode]`, // only loaded in specified mode + `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git ], prefixes = ["process.env.", "import.meta.env."], From 67d8aedb680852dc20610c08c3fb40b275ed6e7c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 10:48:33 +0530 Subject: [PATCH 50/65] test: add test case to check priority order of variables --- .../dotenv-webpack-plugin.test.js | 19 +++++++++++++++++++ .../dotenv-webpack-plugin/priority-order/.env | 2 ++ .../priority-order/.env.example | 1 + .../priority-order/.env.local | 2 ++ .../priority-order/.env.production | 2 ++ .../priority-order/.env.production.local | 1 + .../priority-order/src/index.js | 13 +++++++++++++ 7 files changed, 40 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/priority-order/.env create mode 100644 test/build/dotenv-webpack-plugin/priority-order/.env.example create mode 100644 test/build/dotenv-webpack-plugin/priority-order/.env.local create mode 100644 test/build/dotenv-webpack-plugin/priority-order/.env.production create mode 100644 test/build/dotenv-webpack-plugin/priority-order/.env.production.local create mode 100644 test/build/dotenv-webpack-plugin/priority-order/src/index.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index 4ea18a8bf79..cf2adc743c0 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -259,4 +259,23 @@ describe("dotenv-webpack-plugin", () => { expect(data).toContain("Hello from index.js"); expect(data).toContain("default_value"); }); + + it("overrides the variables according to priority order correctly", async () => { + const testDir = join(__dirname, "priority-order"); + const { exitCode, stderr, stdout } = await run(testDir, ["--read-dot-env"]); + + assertNoErrors(exitCode, stderr, stdout, testDir); + + const data = await getBuildOutput(testDir); + + expect(data).toContain("Hello from index.js"); + expect(data).toContain(`"process.env.PUBLIC_EXAMPLE_VARIABLE:",public_example_value_override`); + expect(data).toContain(`"process.env.PUBLIC_ENV_VARIABLE:",public_env_value_override`); + expect(data).toContain( + `"process.env.PUBLIC_ENV_MODE_VARIABLE:",public_env_mode_value_override`, + ); + expect(data).toContain( + `"process.env.PUBLIC_ENV_LOCAL_VARIABLE:",public_env_local_value_override`, + ); + }); }); diff --git a/test/build/dotenv-webpack-plugin/priority-order/.env b/test/build/dotenv-webpack-plugin/priority-order/.env new file mode 100644 index 00000000000..16ea9537be2 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/priority-order/.env @@ -0,0 +1,2 @@ +PUBLIC_EXAMPLE_VARIABLE=public_example_value_override +PUBLIC_ENV_VARIABLE=public_env_value \ No newline at end of file diff --git a/test/build/dotenv-webpack-plugin/priority-order/.env.example b/test/build/dotenv-webpack-plugin/priority-order/.env.example new file mode 100644 index 00000000000..f69be280e6d --- /dev/null +++ b/test/build/dotenv-webpack-plugin/priority-order/.env.example @@ -0,0 +1 @@ +PUBLIC_EXAMPLE_VARIABLE=public_example_value diff --git a/test/build/dotenv-webpack-plugin/priority-order/.env.local b/test/build/dotenv-webpack-plugin/priority-order/.env.local new file mode 100644 index 00000000000..33db2187942 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/priority-order/.env.local @@ -0,0 +1,2 @@ +PUBLIC_ENV_MODE_VARIABLE=public_env_mode_value_override +PUBLIC_ENV_LOCAL_VARIABLE=public_env_local_value \ No newline at end of file diff --git a/test/build/dotenv-webpack-plugin/priority-order/.env.production b/test/build/dotenv-webpack-plugin/priority-order/.env.production new file mode 100644 index 00000000000..6d8491573dc --- /dev/null +++ b/test/build/dotenv-webpack-plugin/priority-order/.env.production @@ -0,0 +1,2 @@ +PUBLIC_ENV_VARIABLE=public_env_value_override +PUBLIC_ENV_MODE_VARIABLE=public_env_mode_value \ No newline at end of file diff --git a/test/build/dotenv-webpack-plugin/priority-order/.env.production.local b/test/build/dotenv-webpack-plugin/priority-order/.env.production.local new file mode 100644 index 00000000000..6dda4786329 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/priority-order/.env.production.local @@ -0,0 +1 @@ +PUBLIC_ENV_LOCAL_VARIABLE=public_env_local_value_override diff --git a/test/build/dotenv-webpack-plugin/priority-order/src/index.js b/test/build/dotenv-webpack-plugin/priority-order/src/index.js new file mode 100644 index 00000000000..e93cdf516a1 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/priority-order/src/index.js @@ -0,0 +1,13 @@ +console.log("Hello from index.js"); + +// value from .env.example to be overridden by .env +console.log("process.env.PUBLIC_EXAMPLE_VARIABLE:", process.env.PUBLIC_EXAMPLE_VARIABLE); + +// value from .env to be overridden by .env.[mode] +console.log("process.env.PUBLIC_ENV_VARIABLE:", process.env.PUBLIC_ENV_VARIABLE); + +// value from .env.[mode] to be overridden by .env.local +console.log("process.env.PUBLIC_ENV_MODE_VARIABLE:", process.env.PUBLIC_ENV_MODE_VARIABLE); + +// value from .env.local to be overridden by .env.[mode].local +console.log("process.env.PUBLIC_ENV_LOCAL_VARIABLE:", process.env.PUBLIC_ENV_LOCAL_VARIABLE); \ No newline at end of file From a29fc1b3431303773ce80f203ca833aec22ef83e Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 11:05:27 +0530 Subject: [PATCH 51/65] feat: reduce fs call --- packages/dotenv-webpack-plugin/src/index.js | 83 +++++++++------------ 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 4bb9acaae0c..9263df89e0c 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -21,16 +21,18 @@ class DotenvWebpackPlugin { const currentDirectory = process.cwd(); + this.defaultFileList = [ + `${currentDirectory}/.env.example`, // loaded in all cases + `${currentDirectory}/.env`, // loaded in all cases + `${currentDirectory}/.env.[mode]`, // only loaded in specified mode + `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git + `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git + ]; + const { // priority is in ascending order // files at the end of the array have higher priority - envFiles = [ - `${currentDirectory}/.env.example`, // loaded in all cases - `${currentDirectory}/.env`, // loaded in all cases - `${currentDirectory}/.env.[mode]`, // only loaded in specified mode - `${currentDirectory}/.env.local`, // loaded in all cases, ignored by git - `${currentDirectory}/.env.[mode].local`, // only loaded in specified mode, ignored by git - ], + envFiles = this.defaultFileList, prefixes = ["process.env.", "import.meta.env."], envVarPrefix = "PUBLIC_", } = options; @@ -43,23 +45,17 @@ class DotenvWebpackPlugin { } /** - * Check if file exists - * @param {Compiler} compiler - Webpack compiler - * @param {string} environmentFile - Path to environment file + * Default file list and the options file list are updated with the + * value of the mode if [mode] placeholder is used + * @param {String} mode - Webpack mode */ - fileExists(compiler, environmentFile) { - return new Promise((resolve) => { - const fs = compiler.inputFileSystem; - - fs.stat(environmentFile, (err) => { - // File does not exist - if (err) { - return resolve(false); - } - - return resolve(environmentFile); - }); - }); + updateFileListWithMode(mode) { + this.options.envFiles = this.options.envFiles.map((environmentFile) => + environmentFile.replace(/\[mode\]/g, mode), + ); + this.defaultFileList = this.defaultFileList.map((environmentFile) => + environmentFile.replace(/\[mode\]/g, mode), + ); } /** @@ -73,31 +69,29 @@ class DotenvWebpackPlugin { const fs = compiler.inputFileSystem; - this.fileExists(compiler, environmentFile).then((exists) => { - if (!exists) { - return resolve(); - } - - fs.readFile(environmentFile, (err, environmentFileContents) => { - if (err) { + fs.readFile(environmentFile, (err, environmentFileContents) => { + if (err) { + if (!this.defaultFileList.includes(environmentFile)) { const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); logger.error(`Could not read ${environmentFile}`); return reject(err); + } else { + return resolve(); } + } - const parsedEnvVariables = dotenv.parse(environmentFileContents); - for (const [key, value] of Object.entries(parsedEnvVariables)) { - // only add variables starting with the provided prefix - if (key.startsWith(this.options.envVarPrefix)) { - for (let index = 0; index < this.options.prefixes.length; index++) { - const prefix = this.options.prefixes[index]; - envVariables[`${prefix}${key}`] = value; - } + const parsedEnvVariables = dotenv.parse(environmentFileContents); + for (const [key, value] of Object.entries(parsedEnvVariables)) { + // only add variables starting with the provided prefix + if (key.startsWith(this.options.envVarPrefix)) { + for (let index = 0; index < this.options.prefixes.length; index++) { + const prefix = this.options.prefixes[index]; + envVariables[`${prefix}${key}`] = value; } } + } - resolve(envVariables); - }); + resolve(envVariables); }); }); } @@ -109,18 +103,15 @@ class DotenvWebpackPlugin { */ apply(compiler) { const mode = compiler.options.mode || "production"; - - const environmentFiles = this.options.envFiles.map((environmentFile) => - environmentFile.replace(/\[mode\]/g, mode), - ); + this.updateFileListWithMode(mode); compiler.hooks.beforeRun.tapPromise("DotenvWebpackPlugin", (compiler) => { compiler.hooks.compilation.tap("DotenvWebpackPlugin", (compilation) => { - compilation.buildDependencies.addAll(environmentFiles); + compilation.buildDependencies.addAll(this.options.envFiles); }); return Promise.all( - environmentFiles.map((environmentFile) => this.readFile(compiler, environmentFile)), + this.options.envFiles.map((environmentFile) => this.readFile(compiler, environmentFile)), ) .then((valuesList) => { let envVariables = {}; From ca7a264d473f4ceef266f2b51c8ca508e162d6ce Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 11:12:45 +0530 Subject: [PATCH 52/65] test: add test case to validate files --- .../dotenv-webpack-plugin.test.js | 7 +++++++ .../validates-file-exists/src/index.js | 1 + .../validates-file-exists/webpack.config.js | 15 +++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/validates-file-exists/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/validates-file-exists/webpack.config.js diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index cf2adc743c0..eb495e3ccbb 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -278,4 +278,11 @@ describe("dotenv-webpack-plugin", () => { `"process.env.PUBLIC_ENV_LOCAL_VARIABLE:",public_env_local_value_override`, ); }); + + it("throws an error if custom env file path is passed and file could not be read", async () => { + const testDir = join(__dirname, "validates-file-exists"); + const { stderr } = await run(testDir); + + expect(stderr).toContain("Could not read ./env.custom"); + }); }); diff --git a/test/build/dotenv-webpack-plugin/validates-file-exists/src/index.js b/test/build/dotenv-webpack-plugin/validates-file-exists/src/index.js new file mode 100644 index 00000000000..2b4c95d39d1 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-file-exists/src/index.js @@ -0,0 +1 @@ +console.log("Hello from index.js"); diff --git a/test/build/dotenv-webpack-plugin/validates-file-exists/webpack.config.js b/test/build/dotenv-webpack-plugin/validates-file-exists/webpack.config.js new file mode 100644 index 00000000000..5770b61c059 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-file-exists/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + envFiles: ["./env.custom"], + }), + ], +}; From 8a98a05dd183ebe0ee581c2021f7fe31d35ff39a Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 11:16:32 +0530 Subject: [PATCH 53/65] refactor: filter variables together --- packages/dotenv-webpack-plugin/src/index.js | 39 ++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 9263df89e0c..5ef9ea560f0 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -65,8 +65,6 @@ class DotenvWebpackPlugin { */ readFile(compiler, environmentFile) { return new Promise((resolve, reject) => { - const envVariables = {}; - const fs = compiler.inputFileSystem; fs.readFile(environmentFile, (err, environmentFileContents) => { @@ -81,21 +79,28 @@ class DotenvWebpackPlugin { } const parsedEnvVariables = dotenv.parse(environmentFileContents); - for (const [key, value] of Object.entries(parsedEnvVariables)) { - // only add variables starting with the provided prefix - if (key.startsWith(this.options.envVarPrefix)) { - for (let index = 0; index < this.options.prefixes.length; index++) { - const prefix = this.options.prefixes[index]; - envVariables[`${prefix}${key}`] = value; - } - } - } - resolve(envVariables); + resolve(parsedEnvVariables); }); }); } + filterVariables(envVariables) { + const filteredEnvVariables = {}; + + for (const [key, value] of Object.entries(envVariables)) { + // only add variables starting with the provided prefix + if (key.startsWith(this.options.envVarPrefix)) { + for (let index = 0; index < this.options.prefixes.length; index++) { + const prefix = this.options.prefixes[index]; + filteredEnvVariables[`${prefix}${key}`] = value; + } + } + } + + return filteredEnvVariables; + } + /** * Webpack apply hook * @param {Compiler} compiler - Webpack compiler @@ -114,7 +119,7 @@ class DotenvWebpackPlugin { this.options.envFiles.map((environmentFile) => this.readFile(compiler, environmentFile)), ) .then((valuesList) => { - let envVariables = {}; + const envVariables = {}; valuesList.forEach((values) => { if (values) { @@ -124,14 +129,16 @@ class DotenvWebpackPlugin { } }); + const filteredEnvVariables = this.filterVariables(envVariables); + // expand environment vars - envVariables = dotenvExpand.expand({ - parsed: envVariables, + const expandedEnvVariables = dotenvExpand.expand({ + parsed: filteredEnvVariables, // don't write to process.env ignoreProcessEnv: true, }).parsed; - new DefinePlugin(envVariables).apply(compiler); + new DefinePlugin(expandedEnvVariables).apply(compiler); }) .catch(() => {}); }); From 03211fe99eef4adb1dc49afc52e93b8ab994d9b8 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 11:29:06 +0530 Subject: [PATCH 54/65] feat: check if plugin is installed before adding --- packages/webpack-cli/src/webpack-cli.ts | 51 ++++++++++++++++--------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 5338dcbd067..e4143750de6 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2290,31 +2290,44 @@ class WebpackCLI implements IWebpackCLI { ); }; - if (!!options.readDotEnv && !this.checkPackageExists("webpack")) { - this.logger.error("The 'webpack' package is required to use the '--read-dot-env' option."); - } + if (options.readDotEnv) { + const isWebpackInstalled = this.checkPackageExists("webpack"); - const shouldAddDotEnvPlugin = !!options.readDotEnv && this.checkPackageExists("webpack"); + if (!isWebpackInstalled) { + this.logger.error("The 'webpack' package is required to use the '--read-dot-env' option."); + } - if (shouldAddDotEnvPlugin) { - const DotenvWebpackPlugin = await this.tryRequireThenImport void, []>>( - "dotenv-webpack-plugin", - ); + const isDotEnvPluginInstalled = this.checkPackageExists("dotenv-webpack-plugin"); - const addDotEnvPlugin = (item: WebpackConfiguration) => { - if (!item.plugins) { - item.plugins = []; - } + if (!isDotEnvPluginInstalled) { + this.logger.error( + "The 'dotenv-webpack-plugin' package is required to use the '--read-dot-env' option.", + ); + } - item.plugins.push(new DotenvWebpackPlugin()); - }; + const shouldAddDotEnvPlugin = + !!options.readDotEnv && isWebpackInstalled && isDotEnvPluginInstalled; - if (Array.isArray(config.options)) { - for (const item of config.options) { - addDotEnvPlugin(item); + if (shouldAddDotEnvPlugin) { + const DotenvWebpackPlugin = await this.tryRequireThenImport< + Instantiable void, []> + >("dotenv-webpack-plugin"); + + const addDotEnvPlugin = (item: WebpackConfiguration) => { + if (!item.plugins) { + item.plugins = []; + } + + item.plugins.push(new DotenvWebpackPlugin()); + }; + + if (Array.isArray(config.options)) { + for (const item of config.options) { + addDotEnvPlugin(item); + } + } else { + addDotEnvPlugin(config.options); } - } else { - addDotEnvPlugin(config.options); } } From 1cb9309dde5083b829cffed8c93ce2c822f21800 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 12:28:53 +0530 Subject: [PATCH 55/65] chore: update snapshots --- .../help.test.js.snap.devServer4.webpack5 | 964 +++++++++--------- 1 file changed, 501 insertions(+), 463 deletions(-) diff --git a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 index 8a518fd0ad8..8e400e66efa 100644 --- a/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 +++ b/test/help/__snapshots__/help.test.js.snap.devServer4.webpack5 @@ -94,15 +94,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -136,7 +136,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. @@ -156,15 +156,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -198,7 +198,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. @@ -218,15 +218,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -260,7 +260,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. @@ -279,39 +279,39 @@ exports[`help should show help information for 'b' command using command syntax: Run webpack (default command, can be omitted). Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - -w, --watch Enter watch mode, which rebuilds on file change. - --no-watch Negative 'watch' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + -w, --watch Enter watch mode, which rebuilds on file change. + --no-watch Negative 'watch' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -328,39 +328,39 @@ exports[`help should show help information for 'b' command using the "--help" op Run webpack (default command, can be omitted). Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - -w, --watch Enter watch mode, which rebuilds on file change. - --no-watch Negative 'watch' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + -w, --watch Enter watch mode, which rebuilds on file change. + --no-watch Negative 'watch' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -377,56 +377,66 @@ exports[`help should show help information for 'build' and respect the "--color" Run webpack (default command, can be omitted). Options: - -c, --config Provide path to a webpack configuration file e.g. - ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using - 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is - a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get - bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code - on warnings from webpack. - -e, --extends Path to the configuration to be extended (only - works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | - eval | - [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the - last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). - --mode Enable production optimizations or development - hints. - --name Name of the configuration. Used when loading - multiple configurations. - -o, --output-path The output directory as **absolute path** - (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build - for. An array of environments to build for all of - them when possible. - --no-target Negative 'target' option. - -w, --watch Enter watch mode, which rebuilds on file change. - --no-watch Negative 'watch' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack + configuration files to process, e.g. + "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to + use if configuration file exports an + array of multiple configurations. + -m, --merge Merge two or more configurations using + 'webpack-merge'. + --disable-interpret Disable interpret for loading the config + file. + --env Environment variables passed to the + configuration when it is a function, e.g. + "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the + specified value. + --analyze It invokes webpack-bundle-analyzer plugin + to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a + file. + --fail-on-warnings Stop webpack-cli process with non-zero + exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended + (only works when using webpack-cli). + --read-dot-env Read environment variables from .env + files + -d, --devtool A developer tool to enhance debugging + (false | eval | + [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. + Only the last one is exported. + --extends Path to the configuration to be extended + (only works when using webpack-cli). + --mode Enable production optimizations or + development hints. + --name Name of the configuration. Used when + loading multiple configurations. + -o, --output-path The output directory as **absolute path** + (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to + build for. An array of environments to + build for all of them when possible. + --no-target Negative 'target' option. + -w, --watch Enter watch mode, which rebuilds on file + change. + --no-watch Negative 'watch' option. + --watch-options-stdin Stop watching when stdin stream has + ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', - 'webpack-cli' and 'webpack-dev-server' and - commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', + 'webpack-cli' and 'webpack-dev-server' + and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -443,56 +453,66 @@ exports[`help should show help information for 'build' and respect the "--no-col Run webpack (default command, can be omitted). Options: - -c, --config Provide path to a webpack configuration file e.g. - ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using - 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is - a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get - bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code - on warnings from webpack. - -e, --extends Path to the configuration to be extended (only - works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | - eval | - [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the - last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). - --mode Enable production optimizations or development - hints. - --name Name of the configuration. Used when loading - multiple configurations. - -o, --output-path The output directory as **absolute path** - (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build - for. An array of environments to build for all of - them when possible. - --no-target Negative 'target' option. - -w, --watch Enter watch mode, which rebuilds on file change. - --no-watch Negative 'watch' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack + configuration files to process, e.g. + "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to + use if configuration file exports an + array of multiple configurations. + -m, --merge Merge two or more configurations using + 'webpack-merge'. + --disable-interpret Disable interpret for loading the config + file. + --env Environment variables passed to the + configuration when it is a function, e.g. + "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the + specified value. + --analyze It invokes webpack-bundle-analyzer plugin + to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a + file. + --fail-on-warnings Stop webpack-cli process with non-zero + exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended + (only works when using webpack-cli). + --read-dot-env Read environment variables from .env + files + -d, --devtool A developer tool to enhance debugging + (false | eval | + [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. + Only the last one is exported. + --extends Path to the configuration to be extended + (only works when using webpack-cli). + --mode Enable production optimizations or + development hints. + --name Name of the configuration. Used when + loading multiple configurations. + -o, --output-path The output directory as **absolute path** + (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to + build for. An array of environments to + build for all of them when possible. + --no-target Negative 'target' option. + -w, --watch Enter watch mode, which rebuilds on file + change. + --no-watch Negative 'watch' option. + --watch-options-stdin Stop watching when stdin stream has + ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', - 'webpack-cli' and 'webpack-dev-server' and - commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', + 'webpack-cli' and 'webpack-dev-server' + and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -509,39 +529,39 @@ exports[`help should show help information for 'build' command using command syn Run webpack (default command, can be omitted). Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - -w, --watch Enter watch mode, which rebuilds on file change. - --no-watch Negative 'watch' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + -w, --watch Enter watch mode, which rebuilds on file change. + --no-watch Negative 'watch' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -558,39 +578,39 @@ exports[`help should show help information for 'build' command using the "--help Run webpack (default command, can be omitted). Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - -w, --watch Enter watch mode, which rebuilds on file change. - --no-watch Negative 'watch' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + -w, --watch Enter watch mode, which rebuilds on file change. + --no-watch Negative 'watch' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1541,37 +1561,37 @@ exports[`help should show help information for 'w' command using command syntax: Run webpack and watch for files changes. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1588,37 +1608,37 @@ exports[`help should show help information for 'w' command using the "--help" op Run webpack and watch for files changes. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1635,54 +1655,63 @@ exports[`help should show help information for 'watch' and respect the "--color" Run webpack and watch for files changes. Options: - -c, --config Provide path to a webpack configuration file e.g. - ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using - 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is - a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get - bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code - on warnings from webpack. - -e, --extends Path to the configuration to be extended (only - works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | - eval | - [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the - last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). - --mode Enable production optimizations or development - hints. - --name Name of the configuration. Used when loading - multiple configurations. - -o, --output-path The output directory as **absolute path** - (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build - for. An array of environments to build for all of - them when possible. - --no-target Negative 'target' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack + configuration files to process, e.g. + "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to + use if configuration file exports an + array of multiple configurations. + -m, --merge Merge two or more configurations using + 'webpack-merge'. + --disable-interpret Disable interpret for loading the config + file. + --env Environment variables passed to the + configuration when it is a function, e.g. + "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the + specified value. + --analyze It invokes webpack-bundle-analyzer plugin + to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a + file. + --fail-on-warnings Stop webpack-cli process with non-zero + exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended + (only works when using webpack-cli). + --read-dot-env Read environment variables from .env + files + -d, --devtool A developer tool to enhance debugging + (false | eval | + [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. + Only the last one is exported. + --extends Path to the configuration to be extended + (only works when using webpack-cli). + --mode Enable production optimizations or + development hints. + --name Name of the configuration. Used when + loading multiple configurations. + -o, --output-path The output directory as **absolute path** + (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to + build for. An array of environments to + build for all of them when possible. + --no-target Negative 'target' option. + --watch-options-stdin Stop watching when stdin stream has + ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', - 'webpack-cli' and 'webpack-dev-server' and - commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', + 'webpack-cli' and 'webpack-dev-server' + and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1699,54 +1728,63 @@ exports[`help should show help information for 'watch' and respect the "--no-col Run webpack and watch for files changes. Options: - -c, --config Provide path to a webpack configuration file e.g. - ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using - 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is - a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get - bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code - on warnings from webpack. - -e, --extends Path to the configuration to be extended (only - works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | - eval | - [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the - last one is exported. - --extends Path to the configuration to be extended (only - works when using webpack-cli). - --mode Enable production optimizations or development - hints. - --name Name of the configuration. Used when loading - multiple configurations. - -o, --output-path The output directory as **absolute path** - (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build - for. An array of environments to build for all of - them when possible. - --no-target Negative 'target' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack + configuration files to process, e.g. + "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to + use if configuration file exports an + array of multiple configurations. + -m, --merge Merge two or more configurations using + 'webpack-merge'. + --disable-interpret Disable interpret for loading the config + file. + --env Environment variables passed to the + configuration when it is a function, e.g. + "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the + specified value. + --analyze It invokes webpack-bundle-analyzer plugin + to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a + file. + --fail-on-warnings Stop webpack-cli process with non-zero + exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended + (only works when using webpack-cli). + --read-dot-env Read environment variables from .env + files + -d, --devtool A developer tool to enhance debugging + (false | eval | + [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. + Only the last one is exported. + --extends Path to the configuration to be extended + (only works when using webpack-cli). + --mode Enable production optimizations or + development hints. + --name Name of the configuration. Used when + loading multiple configurations. + -o, --output-path The output directory as **absolute path** + (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to + build for. An array of environments to + build for all of them when possible. + --no-target Negative 'target' option. + --watch-options-stdin Stop watching when stdin stream has + ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', - 'webpack-cli' and 'webpack-dev-server' and - commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', + 'webpack-cli' and 'webpack-dev-server' + and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1763,37 +1801,37 @@ exports[`help should show help information for 'watch' command using command syn Run webpack and watch for files changes. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1810,37 +1848,37 @@ exports[`help should show help information for 'watch' command using the "--help Run webpack and watch for files changes. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. - -m, --merge Merge two or more configurations using 'webpack-merge'. - --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. - --node-env Sets process.env.NODE_ENV to the specified value. - --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. - --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. - --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. - -e, --extends Path to the configuration to be extended (only works when using webpack-cli). - --read-dot-env Read environment variables from .env files - -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). - --no-devtool Negative 'devtool' option. - --entry A module that is loaded upon startup. Only the last one is exported. - --extends Path to the configuration to be extended (only works when using webpack-cli). - --mode Enable production optimizations or development hints. - --name Name of the configuration. Used when loading multiple configurations. - -o, --output-path The output directory as **absolute path** (required). - --stats [value] Stats options object or preset name. - --no-stats Negative 'stats' option. - -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. - --no-target Negative 'target' option. - --watch-options-stdin Stop watching when stdin stream has ended. - --no-watch-options-stdin Negative 'watch-options-stdin' option. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. + -m, --merge Merge two or more configurations using 'webpack-merge'. + --disable-interpret Disable interpret for loading the config file. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". + --node-env Sets process.env.NODE_ENV to the specified value. + --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. + --progress [value] Print compilation progress during build. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. + -e, --extends Path to the configuration to be extended (only works when using webpack-cli). + --read-dot-env Read environment variables from .env files + -d, --devtool A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map). + --no-devtool Negative 'devtool' option. + --entry A module that is loaded upon startup. Only the last one is exported. + --extends Path to the configuration to be extended (only works when using webpack-cli). + --mode Enable production optimizations or development hints. + --name Name of the configuration. Used when loading multiple configurations. + -o, --output-path The output directory as **absolute path** (required). + --stats [value] Stats options object or preset name. + --no-stats Negative 'stats' option. + -t, --target Environment to build for. Environment to build for. An array of environments to build for all of them when possible. + --no-target Negative 'target' option. + --watch-options-stdin Stop watching when stdin stream has ended. + --no-watch-options-stdin Negative 'watch-options-stdin' option. Global options: - --color Enable colors on console. - --no-color Disable colors on console. - -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. - -h, --help [verbose] Display help for commands and options. + --color Enable colors on console. + --no-color Disable colors on console. + -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. + -h, --help [verbose] Display help for commands and options. To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1858,15 +1896,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -1900,7 +1938,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. @@ -1920,15 +1958,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -1962,7 +2000,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. @@ -2172,15 +2210,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -2214,7 +2252,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. @@ -2232,15 +2270,15 @@ Alternative usage to run commands: webpack [command] [options] The build tool for modern web applications. Options: - -c, --config Provide path to a webpack configuration file e.g. ./webpack.config.js. - --config-name Name of the configuration to use. + -c, --config Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js". + --config-name Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations. -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. - --env Environment passed to the configuration when it is a function. + --env Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval". --node-env Sets process.env.NODE_ENV to the specified value. --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. --progress [value] Print compilation progress during build. - -j, --json [value] Prints result as JSON or store it in a file. + -j, --json [pathToJsonFile] Prints result as JSON or store it in a file. --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack. -e, --extends Path to the configuration to be extended (only works when using webpack-cli). --read-dot-env Read environment variables from .env files @@ -2274,7 +2312,7 @@ Commands: init|create|new|c|n [generation-path] [options] Initialize a new webpack project. loader|l [output-path] [options] Scaffold a loader. plugin|p [output-path] [options] Scaffold a plugin. - serve|server|s [entries...] [options] Run the webpack dev server. + serve|server|s [entries...] [options] Run the webpack dev server and watch for source file changes while serving. version|v [options] Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands. watch|w [entries...] [options] Run webpack and watch for files changes. From 26d136f2df517c5b9d9abd51c40523d800512a8c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 19:23:08 +0530 Subject: [PATCH 56/65] docs: add documentation for plugin --- packages/README.md | 1 + packages/dotenv-webpack-plugin/README.md | 88 ++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 packages/dotenv-webpack-plugin/README.md diff --git a/packages/README.md b/packages/README.md index 185874c38f4..a3ac51f4406 100644 --- a/packages/README.md +++ b/packages/README.md @@ -18,6 +18,7 @@ This folder is the collection of those packages. 3. [info](https://github.com/webpack/webpack-cli/tree/master/packages/info) 4. [serve](https://github.com/webpack/webpack-cli/tree/master/packages/serve) 5. [webpack-cli](https://github.com/webpack/webpack-cli/tree/master/packages/webpack-cli) +6. [dotenv-webpack-plugin](https://github.com/webpack/webpack-cli/tree/master/packages/dotenv-webpack-plugin) ## Generic Installation diff --git a/packages/dotenv-webpack-plugin/README.md b/packages/dotenv-webpack-plugin/README.md new file mode 100644 index 00000000000..48692ceaa6d --- /dev/null +++ b/packages/dotenv-webpack-plugin/README.md @@ -0,0 +1,88 @@ +# Dotenv Webpack Plugin + +`dotenv-webpack-plugin` is a webpack plugin that enables consumers to load environment variables from dotenv files. This plugin simplifies the process of managing environment-specific configurations in your webpack projects. + +## Features + +- Loads environment variables from dotenv files +- Provides a convenient way to manage environment-specific configurations +- Fully configurable via an options API + +## Installation + +Install `dotenv-webpack-plugin` using npm: + +```bash +npm install dotenv-webpack-plugin --save-dev +``` + +or using yarn: + +```bash +yarn add dotenv-webpack-plugin --dev +``` + +or using pnpm: + +```bash +pnpm add dotenv-webpack-plugin --save-dev +``` + +## Usage + +To use `dotenv-webpack-plugin`, follow these steps: + +1. Create a `.env` file in the root directory of your project. Add each environment variable on a new lines in the form of `PUBLIC_NAME=VALUE`. By default only variables that are prefixed with `PUBLIC_` will be exposed to webpack. The prefix can be changed by passing the `envVarPrefix` option to the plugin. + +1. Import `dotenv-webpack-plugin` in your webpack configuration file: + + ```javascript + const DotenvWebpackPlugin = require("dotenv-webpack-plugin"); + ``` + +1. Add an instance of DotenvWebpackPlugin to your webpack plugins: + + ```javascript + module.exports = { + // Your webpack configuration options... + plugins: [new DotenvWebpackPlugin()], + }; + ``` + +## Configuration Options + +DotenvWebpackPlugin accepts the following configuration options: + +1. `envFiles`: An array of dotenv files to load. By default, DotenvWebpackPlugin will look for the following files in the root directory of your project: + + - `.env.[mode].local` + - `.env.local` + - `.env.[mode]` + - `.env` + - `.env.example` + + The `[mode]` placeholder will be replaced with the current webpack mode. For example, if the current webpack mode is `development`, DotenvWebpackPlugin will look for the following files: + + - `.env.development.local` + - `.env.local` + - `.env.development` + - `.env` + - `.env.example` + + If the same variable is defined in multiple files, the value from the file with the highest precedence will be used. The precedence order is same as the order of files listed above. + + While passing an array of dotenv files, the path towards the right of the array will have the highest precedence. For example, if you pass `["./.env", "./.env.local"]`, the value from `.env.local` will be used if the same variable is defined in both files. + +1. `envVarPrefix`: The prefix to use when loading environment variables. By default, DotenvWebpackPlugin will look for variables prefixed with `PUBLIC_`. + +1. `prefixes`: An array of prefixes to prepend to the names of environment variables. By default, DotenvWebpackPlugin will prepend `process.env.` and `import.meta.env.` to the names of environment variables. + +You can pass these options when creating an instance of DotenvWebpackPlugin: + +```javascript +new DotenvWebpackPlugin({ + envFiles: ["./.env", "./.env.local"], + prefixes: ["process.env.", "import.meta.env."], + envVarPrefix: "PUBLIC_", +}); +``` From cdd9bde9246ea2a6359473a3dee26420459d61ed Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 20 May 2023 19:27:34 +0530 Subject: [PATCH 57/65] docs: update cli docs --- OPTIONS.md | 1 + packages/webpack-cli/README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/OPTIONS.md b/OPTIONS.md index a638429def9..d179f550f10 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -706,6 +706,7 @@ Options: --performance-max-entrypoint-size Total size of an entry point (in bytes). --profile Capture timing information for each module. --no-profile Negative 'profile' option. + --read-dot-env Read environment variables from dotenv files --records-input-path Store compiler state to a json file. --no-records-input-path Negative 'records-input-path' option. --records-output-path Load compiler state from a json file. diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 430d7f08b46..d443dfa029d 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -84,6 +84,7 @@ Options: -m, --merge Merge two or more configurations using 'webpack-merge'. --disable-interpret Disable interpret for loading the config file. --env Environment passed to the configuration when it is a function. + --read-dot-env Read environment variables from dotenv files --node-env Sets process.env.NODE_ENV to the specified value. --define-process-env-node-env Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`) --analyze It invokes webpack-bundle-analyzer plugin to get bundle information. From 8acb20c836f965cf9ecd9a0a5f5c9ad90869c222 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 23 May 2023 16:48:46 +0530 Subject: [PATCH 58/65] docs: update schema --- packages/dotenv-webpack-plugin/src/options.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/options.json b/packages/dotenv-webpack-plugin/src/options.json index 51ae05d5bb3..da69467fe14 100644 --- a/packages/dotenv-webpack-plugin/src/options.json +++ b/packages/dotenv-webpack-plugin/src/options.json @@ -12,15 +12,19 @@ } }, "prefixes": { - "description": "The prefixes to use for the environment variables", + "description": "The prefixes to prepend to the environment variables", "type": "array", "items": { "type": "string" } }, "envVarPrefix": { - "description": "The prefix to use for the environment variables", + "description": "The prefix to filter environment variables by", "type": "string" + }, + "allowEmptyValues": { + "description": "Whether to allow empty values for environment variables", + "type": "boolean" } } } From 1388d5eac93bb08b322931de697e341e4985cc0b Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 23 May 2023 16:58:46 +0530 Subject: [PATCH 59/65] feat: throw error when an empty value is passed --- packages/dotenv-webpack-plugin/src/index.js | 53 +++++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index 5ef9ea560f0..babec89e5d2 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -35,12 +35,14 @@ class DotenvWebpackPlugin { envFiles = this.defaultFileList, prefixes = ["process.env.", "import.meta.env."], envVarPrefix = "PUBLIC_", + allowEmptyValues = false, } = options; this.options = { envFiles, prefixes, envVarPrefix, + allowEmptyValues, }; } @@ -91,16 +93,43 @@ class DotenvWebpackPlugin { for (const [key, value] of Object.entries(envVariables)) { // only add variables starting with the provided prefix if (key.startsWith(this.options.envVarPrefix)) { - for (let index = 0; index < this.options.prefixes.length; index++) { - const prefix = this.options.prefixes[index]; - filteredEnvVariables[`${prefix}${key}`] = value; - } + filteredEnvVariables[key] = value; } } return filteredEnvVariables; } + assignPrefixes(envVariables) { + const prefixedEnvVariables = {}; + + for (const [key, value] of Object.entries(envVariables)) { + for (let index = 0; index < this.options.prefixes.length; index++) { + const prefix = this.options.prefixes[index]; + prefixedEnvVariables[`${prefix}${key}`] = value; + } + } + + return prefixedEnvVariables; + } + + /** + * Get list of empty values + * @param {Object} envVariables - Environment variables + * @returns {Array} - List of empty values + */ + getEmptyValues(envVariables) { + const emptyValues = []; + + for (const [key, value] of Object.entries(envVariables)) { + if (value === "") { + emptyValues.push(key); + } + } + + return emptyValues; + } + /** * Webpack apply hook * @param {Compiler} compiler - Webpack compiler @@ -131,9 +160,23 @@ class DotenvWebpackPlugin { const filteredEnvVariables = this.filterVariables(envVariables); + const emptyValues = this.getEmptyValues(filteredEnvVariables); + + if (!this.options.allowEmptyValues && emptyValues.length > 0) { + const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); + logger.error( + `Environment variables cannot have an empty value. The following variables are empty: ${emptyValues.join( + ", ", + )}`, + ); + return; + } + + const prefixedEnvVariables = this.assignPrefixes(filteredEnvVariables); + // expand environment vars const expandedEnvVariables = dotenvExpand.expand({ - parsed: filteredEnvVariables, + parsed: prefixedEnvVariables, // don't write to process.env ignoreProcessEnv: true, }).parsed; From 81edc4c8bb1a0c1c67b737734a9d2459cbe8ebb5 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 23 May 2023 16:59:08 +0530 Subject: [PATCH 60/65] feat: update types --- packages/dotenv-webpack-plugin/src/types.d.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/types.d.ts b/packages/dotenv-webpack-plugin/src/types.d.ts index 8e34285d428..d863dae736b 100644 --- a/packages/dotenv-webpack-plugin/src/types.d.ts +++ b/packages/dotenv-webpack-plugin/src/types.d.ts @@ -1,5 +1,6 @@ export type Config = { - envFiles: string[]; - prefixes: string[]; - envVarPrefix: string; + envFiles?: string[]; + prefixes?: string[]; + envVarPrefix?: string; + allowEmptyValues?: boolean; }; From 55b64a1fcfcf9d7831765fe20c88064e21faabe2 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 23 May 2023 17:02:04 +0530 Subject: [PATCH 61/65] test: add tests for allowEmptyValues --- .../allow-empty-values/.env | 2 ++ .../allow-empty-values/src/index.js | 1 + .../allow-empty-values/webpack.config.js | 15 +++++++++++++++ .../dotenv-webpack-plugin.test.js | 16 ++++++++++++++++ .../validates-empty-value/.env | 2 ++ .../validates-empty-value/src/index.js | 1 + .../validates-empty-value/webpack.config.js | 11 +++++++++++ 7 files changed, 48 insertions(+) create mode 100644 test/build/dotenv-webpack-plugin/allow-empty-values/.env create mode 100644 test/build/dotenv-webpack-plugin/allow-empty-values/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/allow-empty-values/webpack.config.js create mode 100644 test/build/dotenv-webpack-plugin/validates-empty-value/.env create mode 100644 test/build/dotenv-webpack-plugin/validates-empty-value/src/index.js create mode 100644 test/build/dotenv-webpack-plugin/validates-empty-value/webpack.config.js diff --git a/test/build/dotenv-webpack-plugin/allow-empty-values/.env b/test/build/dotenv-webpack-plugin/allow-empty-values/.env new file mode 100644 index 00000000000..048ec8a8dd4 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/allow-empty-values/.env @@ -0,0 +1,2 @@ +PUBLIC_VARIABLE= +PUBLIC_VARIABLE2= \ No newline at end of file diff --git a/test/build/dotenv-webpack-plugin/allow-empty-values/src/index.js b/test/build/dotenv-webpack-plugin/allow-empty-values/src/index.js new file mode 100644 index 00000000000..2b4c95d39d1 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/allow-empty-values/src/index.js @@ -0,0 +1 @@ +console.log("Hello from index.js"); diff --git a/test/build/dotenv-webpack-plugin/allow-empty-values/webpack.config.js b/test/build/dotenv-webpack-plugin/allow-empty-values/webpack.config.js new file mode 100644 index 00000000000..782eee0c94f --- /dev/null +++ b/test/build/dotenv-webpack-plugin/allow-empty-values/webpack.config.js @@ -0,0 +1,15 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [ + new DotenvWebpackPlugin({ + allowEmptyValues: true, + }), + ], +}; diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index eb495e3ccbb..cf2b5448cdd 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -285,4 +285,20 @@ describe("dotenv-webpack-plugin", () => { expect(stderr).toContain("Could not read ./env.custom"); }); + + it("throws an error if empty value is passed for an environment variable", async () => { + const testDir = join(__dirname, "validates-empty-value"); + const { stderr } = await run(testDir); + + expect(stderr).toContain( + "Environment variables cannot have an empty value. The following variables are empty: PUBLIC_VARIABLE, PUBLIC_VARIABLE2", + ); + }); + + it("does not throw an error if empty value is passed for an environment variable and allowEmptyValues is set to true", async () => { + const testDir = join(__dirname, "allow-empty-values"); + const { exitCode, stderr, stdout } = await run(testDir); + + assertNoErrors(exitCode, stderr, stdout, testDir); + }); }); diff --git a/test/build/dotenv-webpack-plugin/validates-empty-value/.env b/test/build/dotenv-webpack-plugin/validates-empty-value/.env new file mode 100644 index 00000000000..048ec8a8dd4 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-empty-value/.env @@ -0,0 +1,2 @@ +PUBLIC_VARIABLE= +PUBLIC_VARIABLE2= \ No newline at end of file diff --git a/test/build/dotenv-webpack-plugin/validates-empty-value/src/index.js b/test/build/dotenv-webpack-plugin/validates-empty-value/src/index.js new file mode 100644 index 00000000000..2b4c95d39d1 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-empty-value/src/index.js @@ -0,0 +1 @@ +console.log("Hello from index.js"); diff --git a/test/build/dotenv-webpack-plugin/validates-empty-value/webpack.config.js b/test/build/dotenv-webpack-plugin/validates-empty-value/webpack.config.js new file mode 100644 index 00000000000..d602814b4f3 --- /dev/null +++ b/test/build/dotenv-webpack-plugin/validates-empty-value/webpack.config.js @@ -0,0 +1,11 @@ +const { join } = require("path"); +const DotenvWebpackPlugin = require("../../../../packages/dotenv-webpack-plugin/src"); + +module.exports = { + entry: "./src/index.js", + mode: "production", + output: { + path: join(__dirname, "dist"), + }, + plugins: [new DotenvWebpackPlugin()], +}; From c4ec39bef583f172b130701b62b8bdf146e1722c Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Tue, 23 May 2023 17:03:48 +0530 Subject: [PATCH 62/65] docs: add docs for allowemptyvalues option --- packages/dotenv-webpack-plugin/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/dotenv-webpack-plugin/README.md b/packages/dotenv-webpack-plugin/README.md index 48692ceaa6d..34c201d2754 100644 --- a/packages/dotenv-webpack-plugin/README.md +++ b/packages/dotenv-webpack-plugin/README.md @@ -77,6 +77,8 @@ DotenvWebpackPlugin accepts the following configuration options: 1. `prefixes`: An array of prefixes to prepend to the names of environment variables. By default, DotenvWebpackPlugin will prepend `process.env.` and `import.meta.env.` to the names of environment variables. +1. `allowEmptyValues`: A boolean value indicating whether to allow empty values. By default this value is set to `false`, DotenvWebpackPlugin will throw an error if an environment variable is defined without a value. + You can pass these options when creating an instance of DotenvWebpackPlugin: ```javascript @@ -84,5 +86,6 @@ new DotenvWebpackPlugin({ envFiles: ["./.env", "./.env.local"], prefixes: ["process.env.", "import.meta.env."], envVarPrefix: "PUBLIC_", + allowEmptyValues: false, }); ``` From f0e32aae133614cdf1b6edb51cd3ba8fd2b9ae87 Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 27 May 2023 09:17:32 +0530 Subject: [PATCH 63/65] feat: fail the build on errors --- packages/dotenv-webpack-plugin/src/index.js | 24 +++++++++++++++---- .../dotenv-webpack-plugin.test.js | 9 +++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index babec89e5d2..c4e6c1adbb8 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -7,6 +7,7 @@ const schema = require("./options.json"); /** @typedef {import("./types").Config} Config */ /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ +/** @typedef {import("webpack").Compilation} Compilation */ class DotenvWebpackPlugin { /** @@ -44,6 +45,8 @@ class DotenvWebpackPlugin { envVarPrefix, allowEmptyValues, }; + + this.errors = []; } /** @@ -72,8 +75,7 @@ class DotenvWebpackPlugin { fs.readFile(environmentFile, (err, environmentFileContents) => { if (err) { if (!this.defaultFileList.includes(environmentFile)) { - const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); - logger.error(`Could not read ${environmentFile}`); + this.collectError(`Could not read ${environmentFile}`); return reject(err); } else { return resolve(); @@ -113,6 +115,18 @@ class DotenvWebpackPlugin { return prefixedEnvVariables; } + /** + * Throw collected errors to fail the build + * @param {Compilation} compilation - Webpack compilation + */ + throwErrors(compilation) { + compilation.errors.push(...this.errors); + } + + collectError(errorMessage) { + this.errors.push(errorMessage); + } + /** * Get list of empty values * @param {Object} envVariables - Environment variables @@ -142,6 +156,9 @@ class DotenvWebpackPlugin { compiler.hooks.beforeRun.tapPromise("DotenvWebpackPlugin", (compiler) => { compiler.hooks.compilation.tap("DotenvWebpackPlugin", (compilation) => { compilation.buildDependencies.addAll(this.options.envFiles); + if (this.errors.length > 0) { + this.throwErrors(compilation); + } }); return Promise.all( @@ -163,8 +180,7 @@ class DotenvWebpackPlugin { const emptyValues = this.getEmptyValues(filteredEnvVariables); if (!this.options.allowEmptyValues && emptyValues.length > 0) { - const logger = compiler.getInfrastructureLogger("DotenvWebpackPlugin"); - logger.error( + this.collectError( `Environment variables cannot have an empty value. The following variables are empty: ${emptyValues.join( ", ", )}`, diff --git a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js index cf2b5448cdd..23fa05ee7b5 100644 --- a/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js +++ b/test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js @@ -8,6 +8,7 @@ const assertNoErrors = (exitCode, stderr, stdout, testDir, buildPath = "dist") = expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); + expect(stdout).not.toContain("ERROR"); expect(existsSync(resolve(testDir, join(buildPath, "main.js")))).toBeTruthy(); }; @@ -281,16 +282,16 @@ describe("dotenv-webpack-plugin", () => { it("throws an error if custom env file path is passed and file could not be read", async () => { const testDir = join(__dirname, "validates-file-exists"); - const { stderr } = await run(testDir); + const { stdout } = await run(testDir); - expect(stderr).toContain("Could not read ./env.custom"); + expect(stdout).toContain("Could not read ./env.custom"); }); it("throws an error if empty value is passed for an environment variable", async () => { const testDir = join(__dirname, "validates-empty-value"); - const { stderr } = await run(testDir); + const { stdout } = await run(testDir); - expect(stderr).toContain( + expect(stdout).toContain( "Environment variables cannot have an empty value. The following variables are empty: PUBLIC_VARIABLE, PUBLIC_VARIABLE2", ); }); From 6681233862dc31dd4ddbbf586d0a8de78c628b2b Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Sat, 27 May 2023 09:24:20 +0530 Subject: [PATCH 64/65] feat: use webpack error to throw --- packages/dotenv-webpack-plugin/src/index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/dotenv-webpack-plugin/src/index.js b/packages/dotenv-webpack-plugin/src/index.js index c4e6c1adbb8..88b4258ecee 100644 --- a/packages/dotenv-webpack-plugin/src/index.js +++ b/packages/dotenv-webpack-plugin/src/index.js @@ -1,6 +1,6 @@ const dotenv = require("dotenv"); const dotenvExpand = require("dotenv-expand"); -const { DefinePlugin } = require("webpack"); +const { DefinePlugin, WebpackError } = require("webpack"); const { validate } = require("schema-utils"); const schema = require("./options.json"); @@ -120,7 +120,12 @@ class DotenvWebpackPlugin { * @param {Compilation} compilation - Webpack compilation */ throwErrors(compilation) { - compilation.errors.push(...this.errors); + const errors = this.errors.map((error) => { + const webpackError = new WebpackError(error); + webpackError.name = "DotenvWebpackPluginError"; + return webpackError; + }); + compilation.errors.push(...errors); } collectError(errorMessage) { From ccf341e30c98ef85aa909c63fdb2549cd772ca0f Mon Sep 17 00:00:00 2001 From: Burhanuddin Udaipurwala Date: Mon, 5 Jun 2023 20:02:36 +0530 Subject: [PATCH 65/65] chore: remove author field from package file --- packages/dotenv-webpack-plugin/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/dotenv-webpack-plugin/package.json b/packages/dotenv-webpack-plugin/package.json index 22903fdec80..38223cd1973 100644 --- a/packages/dotenv-webpack-plugin/package.json +++ b/packages/dotenv-webpack-plugin/package.json @@ -5,7 +5,6 @@ "main": "src/index.js", "types": "src/types.d.ts", "repository": "https://github.com/webpack/webpack-cli", - "author": "Burhanuddin Udaipurwala (burhanuday)", "license": "MIT", "private": true, "dependencies": {