mirror of https://github.com/webpack/webpack.git
add watch test
This commit is contained in:
parent
08625a84d7
commit
ada334b199
|
@ -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) {
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
WEBPACK_API_URL=https://api0.example.com
|
||||
WEBPACK_FEATURE_FLAG=false
|
|
@ -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();
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
WEBPACK_API_URL=https://api1.example.com
|
||||
WEBPACK_FEATURE_FLAG=false
|
||||
WEBPACK_NEW_VAR=added-in-step-1
|
|
@ -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");
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
WEBPACK_API_URL=https://api2.example.com
|
||||
WEBPACK_FEATURE_FLAG=false
|
||||
WEBPACK_NEW_VAR=added-in-step-1
|
|
@ -0,0 +1,2 @@
|
|||
WEBPACK_FEATURE_FLAG=true
|
||||
WEBPACK_DEV_ONLY=development-value
|
|
@ -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");
|
||||
});
|
|
@ -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
|
||||
]
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue