From ada334b19940babaa79833820ff19a0ead0ff324 Mon Sep 17 00:00:00 2001 From: xiaoxiaojx <784487301@qq.com> Date: Tue, 7 Oct 2025 01:17:03 +0800 Subject: [PATCH] add watch test --- lib/DotenvPlugin.js | 5 ++- test/watchCases/plugins/dotenv-plugin/0/.env | 2 ++ .../plugins/dotenv-plugin/0/index.js | 5 +++ test/watchCases/plugins/dotenv-plugin/1/.env | 3 ++ .../plugins/dotenv-plugin/1/index.js | 5 +++ test/watchCases/plugins/dotenv-plugin/2/.env | 3 ++ .../plugins/dotenv-plugin/2/.env.development | 2 ++ .../plugins/dotenv-plugin/2/index.js | 8 +++++ .../plugins/dotenv-plugin/webpack.config.js | 35 +++++++++++++++++++ 9 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/watchCases/plugins/dotenv-plugin/0/.env create mode 100644 test/watchCases/plugins/dotenv-plugin/0/index.js create mode 100644 test/watchCases/plugins/dotenv-plugin/1/.env create mode 100644 test/watchCases/plugins/dotenv-plugin/1/index.js create mode 100644 test/watchCases/plugins/dotenv-plugin/2/.env create mode 100644 test/watchCases/plugins/dotenv-plugin/2/.env.development create mode 100644 test/watchCases/plugins/dotenv-plugin/2/index.js create mode 100644 test/watchCases/plugins/dotenv-plugin/webpack.config.js diff --git a/lib/DotenvPlugin.js b/lib/DotenvPlugin.js index b1c8812e6..d215cb1e0 100644 --- a/lib/DotenvPlugin.js +++ b/lib/DotenvPlugin.js @@ -6,7 +6,7 @@ "use strict"; const createSchemaValidation = require("./util/create-schema-validation"); -const { join } = require("./util/fs"); +const { isAbsolute, join } = require("./util/fs"); /** @typedef {import("../declarations/WebpackOptions").DotenvPluginOptions} DotenvPluginOptions */ /** @typedef {import("./Compiler")} Compiler */ @@ -336,6 +336,9 @@ class DotenvPlugin { } const getDir = () => { if (typeof rawDir === "string") { + if (isAbsolute(rawDir)) { + return rawDir; + } return join(fs, context, rawDir); } if (rawDir === true) { diff --git a/test/watchCases/plugins/dotenv-plugin/0/.env b/test/watchCases/plugins/dotenv-plugin/0/.env new file mode 100644 index 000000000..0372d502d --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/0/.env @@ -0,0 +1,2 @@ +WEBPACK_API_URL=https://api0.example.com +WEBPACK_FEATURE_FLAG=false diff --git a/test/watchCases/plugins/dotenv-plugin/0/index.js b/test/watchCases/plugins/dotenv-plugin/0/index.js new file mode 100644 index 000000000..56f7b8554 --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/0/index.js @@ -0,0 +1,5 @@ +it("should load env variables from .env file in step 0", function () { + expect(process.env.WEBPACK_API_URL).toBe("https://api0.example.com"); + expect(process.env.WEBPACK_FEATURE_FLAG).toBe("false"); + expect(process.env.WEBPACK_NEW_VAR).toBeUndefined(); +}); diff --git a/test/watchCases/plugins/dotenv-plugin/1/.env b/test/watchCases/plugins/dotenv-plugin/1/.env new file mode 100644 index 000000000..feea2c8f8 --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/1/.env @@ -0,0 +1,3 @@ +WEBPACK_API_URL=https://api1.example.com +WEBPACK_FEATURE_FLAG=false +WEBPACK_NEW_VAR=added-in-step-1 diff --git a/test/watchCases/plugins/dotenv-plugin/1/index.js b/test/watchCases/plugins/dotenv-plugin/1/index.js new file mode 100644 index 000000000..1d13b3faf --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/1/index.js @@ -0,0 +1,5 @@ +it("should load new variable added to .env file in step 1", function () { + expect(process.env.WEBPACK_API_URL).toBe("https://api1.example.com"); + expect(process.env.WEBPACK_FEATURE_FLAG).toBe("false"); + expect(process.env.WEBPACK_NEW_VAR).toBe("added-in-step-1"); +}); diff --git a/test/watchCases/plugins/dotenv-plugin/2/.env b/test/watchCases/plugins/dotenv-plugin/2/.env new file mode 100644 index 000000000..bd5f89bbc --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/2/.env @@ -0,0 +1,3 @@ +WEBPACK_API_URL=https://api2.example.com +WEBPACK_FEATURE_FLAG=false +WEBPACK_NEW_VAR=added-in-step-1 diff --git a/test/watchCases/plugins/dotenv-plugin/2/.env.development b/test/watchCases/plugins/dotenv-plugin/2/.env.development new file mode 100644 index 000000000..f7aeb515f --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/2/.env.development @@ -0,0 +1,2 @@ +WEBPACK_FEATURE_FLAG=true +WEBPACK_DEV_ONLY=development-value diff --git a/test/watchCases/plugins/dotenv-plugin/2/index.js b/test/watchCases/plugins/dotenv-plugin/2/index.js new file mode 100644 index 000000000..b73b55598 --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/2/index.js @@ -0,0 +1,8 @@ +it("should override .env values with .env.development in step 2", function () { + expect(process.env.WEBPACK_API_URL).toBe("https://api2.example.com"); + // WEBPACK_FEATURE_FLAG should be overridden by .env.development + expect(process.env.WEBPACK_FEATURE_FLAG).toBe("true"); + expect(process.env.WEBPACK_NEW_VAR).toBe("added-in-step-1"); + // New variable from .env.development + expect(process.env.WEBPACK_DEV_ONLY).toBe("development-value"); +}); diff --git a/test/watchCases/plugins/dotenv-plugin/webpack.config.js b/test/watchCases/plugins/dotenv-plugin/webpack.config.js new file mode 100644 index 000000000..66ef5a5fe --- /dev/null +++ b/test/watchCases/plugins/dotenv-plugin/webpack.config.js @@ -0,0 +1,35 @@ +"use strict"; + +const path = require("path"); +const DotenvPlugin = require("../../../../").DotenvPlugin; + +/** @type {(env: Env, options: TestOptions) => import("../../../../").Configuration} */ + +module.exports = (env, { srcPath, testPath }) => { + const dotenvPlugin = new DotenvPlugin({ + prefix: "WEBPACK_", + dir: "" + }); + return { + mode: "development", + dotenv: false, + plugins: [ + (compiler) => { + let i = 0; + // Update dotenvPlugin.config.dir before each compile + // Use beforeCompile with stage -1 to run before DotenvPlugin + compiler.hooks.beforeCompile.tap( + { + name: "UpdateDotenvDir", + stage: -1 + }, + () => { + dotenvPlugin.config.dir = path.join(__dirname, String(i)); + i++; + } + ); + }, + dotenvPlugin + ] + }; +};