Skip to content

Commit 7925cd5

Browse files
committed
feat: auto insert matching env from dotenv file
See #1134
1 parent 52cc6b5 commit 7925cd5

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

packages/scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"css-loader": "^5.2.4",
5151
"del": "^6.0.0",
5252
"dev-ip": "^1.0.1",
53+
"dotenv": "^8.2.0",
5354
"execa": "^5.0.0",
5455
"figures": "^3.2.0",
5556
"file-loader": "^6.0.0",

packages/scripts/src/bin/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import chalk from 'chalk';
33
import program from 'commander';
44
import path from 'path';
55
import updateNotifier from 'update-notifier';
6+
import dotenv from 'dotenv';
67

78
import { clearConsole } from '../dev-utils';
89
import { bootstrap } from './bootstrap';
@@ -11,6 +12,8 @@ import { pack } from './pack';
1112
import { serve } from './serve';
1213
import { bulletSymbol, contextHelp, printIntro } from './utils';
1314

15+
dotenv.config();
16+
1417
export interface ProgramOptions {
1518
context?: string;
1619
projectConfig?: string;

packages/scripts/src/config/WebpackConfigHelper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export class WebpackConfigHelper {
268268
new webpack.DefinePlugin({
269269
'process.env.NODE_ENV': JSON.stringify(this.env),
270270
'process.env.BABEL_ENV': JSON.stringify(this.env),
271+
...this.getEnvVariables(),
271272
// Our own access to project config from the modules
272273
// mainly needed for the publicPath entrypoint
273274
__WPACKIO__: {
@@ -718,6 +719,19 @@ ${bannerConfig.copyrightText}${bannerConfig.credit ? creditNote : ''}`,
718719
return defaults;
719720
}
720721

722+
private getEnvVariables() {
723+
const envVariables: Record<string, any> = {};
724+
const match = 'WPACKIO_';
725+
Object.keys(process.env ?? {}).forEach(key => {
726+
if (key.startsWith(match)) {
727+
envVariables[
728+
`process.env.${key.substring(match.length)}`
729+
] = JSON.stringify(process.env[key]);
730+
}
731+
});
732+
return envVariables;
733+
}
734+
721735
/**
722736
* Get calculated app directory
723737
*/

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5079,6 +5079,11 @@ dot-prop@^6.0.1:
50795079
dependencies:
50805080
is-obj "^2.0.0"
50815081

5082+
dotenv@^8.2.0:
5083+
version "8.2.0"
5084+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
5085+
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
5086+
50825087
dts-dom@latest:
50835088
version "3.6.0"
50845089
resolved "https://registry.yarnpkg.com/dts-dom/-/dts-dom-3.6.0.tgz#0e8beaf6240e699d623bf4d7dcf0e6216222459a"

0 commit comments

Comments
 (0)