add watch test

This commit is contained in:
xiaoxiaojx 2025-10-07 01:17:03 +08:00
parent 08625a84d7
commit ada334b199
9 changed files with 67 additions and 1 deletions

View File

@ -6,7 +6,7 @@
"use strict"; "use strict";
const createSchemaValidation = require("./util/create-schema-validation"); 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("../declarations/WebpackOptions").DotenvPluginOptions} DotenvPluginOptions */
/** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler")} Compiler */
@ -336,6 +336,9 @@ class DotenvPlugin {
} }
const getDir = () => { const getDir = () => {
if (typeof rawDir === "string") { if (typeof rawDir === "string") {
if (isAbsolute(rawDir)) {
return rawDir;
}
return join(fs, context, rawDir); return join(fs, context, rawDir);
} }
if (rawDir === true) { if (rawDir === true) {

View File

@ -0,0 +1,2 @@
WEBPACK_API_URL=https://api0.example.com
WEBPACK_FEATURE_FLAG=false

View File

@ -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();
});

View File

@ -0,0 +1,3 @@
WEBPACK_API_URL=https://api1.example.com
WEBPACK_FEATURE_FLAG=false
WEBPACK_NEW_VAR=added-in-step-1

View File

@ -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");
});

View File

@ -0,0 +1,3 @@
WEBPACK_API_URL=https://api2.example.com
WEBPACK_FEATURE_FLAG=false
WEBPACK_NEW_VAR=added-in-step-1

View File

@ -0,0 +1,2 @@
WEBPACK_FEATURE_FLAG=true
WEBPACK_DEV_ONLY=development-value

View File

@ -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");
});

View File

@ -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
]
};
};