Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: built in env files support #3759

Open
wants to merge 76 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
abe80f0
feat: handle env files
Apr 27, 2023
f5bcec5
chore: undo package json changes
Apr 27, 2023
011627e
refactor: undo changes to webpack cli
Apr 28, 2023
bb735ec
feat: add dotenv webpack plugin package
Apr 28, 2023
5a1b4db
refactor: remove cache
Apr 30, 2023
95ca34e
fix: default mode should be production
Apr 30, 2023
08d16ff
refactor: provide env files as default value
Apr 30, 2023
e3a23ce
Merge branch 'master' into feature/env-files
Apr 30, 2023
e2cf250
feat: add webpack cli runner code
May 2, 2023
ca5a782
Merge branch 'master' into feature/env-files
May 2, 2023
fd6d2ba
fix: add env plugin in build config
May 3, 2023
d544597
fix: add webpack as dev dep for dotenv webpack plugin
May 3, 2023
b83ccaf
fix: incorrect path being used in dotenv
May 3, 2023
a4388d7
test: add a simple test
May 3, 2023
ab00786
fix: check if webpack is installed before adding dotenv plugin
May 4, 2023
4e039ba
test: add test for value override
May 4, 2023
3790945
test: add test for the case when variable does not start with webpack_
May 4, 2023
c3d97d7
refactor: simplify test code
May 4, 2023
1aef466
test: add missing assertion
May 4, 2023
bd6c4a6
chore: debug failing smoketests
May 4, 2023
74ad4c8
feat: add cli option
May 6, 2023
afcbf8c
Merge branch 'webpack:master' into feature/env-files
burhanuday May 6, 2023
f203e8b
Merge branch 'master' into feature/env-files
May 6, 2023
9286cb5
refactor: redo applying the plugin
May 6, 2023
8689a2e
test: update snapshots
May 6, 2023
ca6fc8d
test: update tests
May 6, 2023
75ebe62
Merge branch 'feature/env-files' of github.com:burhanuday/webpack-cli…
May 6, 2023
4388c69
test: update snapshots
May 6, 2023
7ab13cf
fix: undo package json changes
May 6, 2023
bf8cb58
feat: add .example env file
May 8, 2023
ff7246f
chore: move dotenv webpack plugin to peerdeps
May 8, 2023
9d7e1db
feat: rename cli flag
May 8, 2023
8a45c8b
test: update snapshots
May 8, 2023
7401474
Merge branch 'master' into feature/env-files
May 8, 2023
f4b8835
test: add more test cases
May 8, 2023
9ee072d
test: add test case for mode local files
May 8, 2023
f7f79b8
fix: update types
May 8, 2023
88d3660
test: add test case for multiple configs
May 8, 2023
0158dca
fix: do validation of passed config
May 9, 2023
f8a6142
test: add more test cases
May 9, 2023
a165f38
fix: use correct logger name
May 10, 2023
06399c5
feat: add types and jsdoc
May 10, 2023
a0534f5
Merge branch 'master' into feature/env-files
May 10, 2023
7f8c4c7
Merge branch 'master' into feature/env-files
May 13, 2023
e00469c
chore: bump
May 15, 2023
d89d73c
feat: add schema utils package
May 16, 2023
0c477b0
feat: use schema utils package to validate input
May 16, 2023
8911ba7
test: update tests with new validation errors
May 16, 2023
00fb0b8
refactor: avoid using object assign
May 16, 2023
cdeb09f
perf: use webpack fs for better perf
May 16, 2023
07dbcd5
feat: add env files to build dependencies
May 17, 2023
99a7163
refactor: extract file exists into a function
May 17, 2023
03d285c
fix: use push instead of unshift for perf
May 17, 2023
ac666c4
Merge branch 'master' into feature/env-files
May 17, 2023
7de5892
feat: make env var prefix configurable
May 17, 2023
07ac04e
test: add tests for env var prefix
May 17, 2023
a3e81fa
test: update tests
May 17, 2023
8e8b78c
Merge branch 'master' into feature/env-files
May 20, 2023
c191cb2
feat: update priority order to make it same as next
May 20, 2023
67d8aed
test: add test case to check priority order of variables
May 20, 2023
a29fc1b
feat: reduce fs call
May 20, 2023
ca7a264
test: add test case to validate files
May 20, 2023
8a98a05
refactor: filter variables together
May 20, 2023
03211fe
feat: check if plugin is installed before adding
May 20, 2023
1cb9309
chore: update snapshots
May 20, 2023
26d136f
docs: add documentation for plugin
May 20, 2023
cdd9bde
docs: update cli docs
May 20, 2023
8acb20c
docs: update schema
May 23, 2023
1388d5e
feat: throw error when an empty value is passed
May 23, 2023
81edc4c
feat: update types
May 23, 2023
55b64a1
test: add tests for allowEmptyValues
May 23, 2023
c4ec39b
docs: add docs for allowemptyvalues option
May 23, 2023
f0e32aa
feat: fail the build on errors
May 27, 2023
6681233
feat: use webpack error to throw
May 27, 2023
8d92b67
Merge branch 'master' into feature/env-files
May 27, 2023
ccf341e
chore: remove author field from package file
Jun 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/dotenv-webpack-plugin/README.md
Expand Up @@ -77,12 +77,15 @@ 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
new DotenvWebpackPlugin({
envFiles: ["./.env", "./.env.local"],
prefixes: ["process.env.", "import.meta.env."],
envVarPrefix: "PUBLIC_",
allowEmptyValues: false,
});
```
53 changes: 48 additions & 5 deletions packages/dotenv-webpack-plugin/src/index.js
Expand Up @@ -35,12 +35,14 @@ class DotenvWebpackPlugin {
envFiles = this.defaultFileList,
prefixes = ["process.env.", "import.meta.env."],
envVarPrefix = "PUBLIC_",
alexander-akait marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's allow it to be string | string[], some env variables can be from other environments, for example SENTRY_ (error reporting) requre prefix SENTRY_, and it can be used in client, so you can have ["PUBLIC_", "SENTRY_"]

allowEmptyValues = false,
} = options;

this.options = {
envFiles,
prefixes,
envVarPrefix,
allowEmptyValues,
};
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved
}

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;
Expand Down
8 changes: 6 additions & 2 deletions packages/dotenv-webpack-plugin/src/options.json
Expand Up @@ -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"
}
}
}
7 changes: 4 additions & 3 deletions 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;
};
2 changes: 2 additions & 0 deletions test/build/dotenv-webpack-plugin/allow-empty-values/.env
@@ -0,0 +1,2 @@
PUBLIC_VARIABLE=
PUBLIC_VARIABLE2=
@@ -0,0 +1 @@
console.log("Hello from index.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,
}),
],
};
16 changes: 16 additions & 0 deletions test/build/dotenv-webpack-plugin/dotenv-webpack-plugin.test.js
Expand Up @@ -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);
});
});
2 changes: 2 additions & 0 deletions test/build/dotenv-webpack-plugin/validates-empty-value/.env
@@ -0,0 +1,2 @@
PUBLIC_VARIABLE=
PUBLIC_VARIABLE2=
@@ -0,0 +1 @@
console.log("Hello from index.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()],
};