Skip to content

Commit

Permalink
fix: resolve env file read environment variables error (#1483)
Browse files Browse the repository at this point in the history
* fix: resolve env file error

* chore: update code

* chore: bump version

* chore: update script

* chore: update test

* chore: merge user env

* chore: merge user env

* chore: update test

* chore: update e2e tests
  • Loading branch information
ErKeLost authored Jul 1, 2024
1 parent 53f5115 commit 49523be
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 90 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-poets-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

bump dotenv version
5 changes: 5 additions & 0 deletions .changeset/real-sheep-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/runtime-plugin-import-meta": patch
---

import meta resolve process NODE_ENV
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ See [Contributing Guide](https://github.com/farm-fe/farm/blob/main/CONTRIBUTING.

- Wechat group

<br><img src="https://github.com/ErKeLost/react-farm/assets/66500121/8ea5240b-b8d7-40ac-b02b-0f281e660a25" width="30%" />
<br><img src="https://github.com/ErKeLost/react-farm/assets/66500121/9780f176-98e6-488d-8118-c3b454737701" width="30%" />

- QQ group

Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Farm 远快于业界常用的 JS 构建工具,在 benchmark 测试中,Farm

- 微信交流群

<br><img src="https://github.com/ErKeLost/react-farm/assets/66500121/8ea5240b-b8d7-40ac-b02b-0f281e660a25" width="30%" />
<br><img src="https://github.com/ErKeLost/react-farm/assets/66500121/9780f176-98e6-488d-8118-c3b454737701" width="30%" />

- QQ 群

Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/env/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FARM_CUSTOM_ENV_VARIABLE=2
CUSTOM_PREFIX_ENV_VARIABLE=2
NORMAL_ENV_VARIABLE=2
FARM_EFFECTIVE_MODE_FILE_NAME=.env.production
FARM_BOOL=true
FARM_EXPAND_A=$EXPAND
FARM_EXPAND_B=$DEPEND_ENV
FARM_ESCAPE_A=escape\$
FARM_ESCAPE_B=escape$
File renamed without changes.
8 changes: 0 additions & 8 deletions examples/env/farm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { defineConfig } from '@farmfe/core';
import { resolve } from 'path';
export default defineConfig({
// compilation: {
// mode: 'staging'
// },
compilation: {
persistentCache: false,
},
envPrefix: ['FARM_', 'CUSTOM_PREFIX_', 'NEW_'],
envDir: resolve('./env'),
server: {
port: 7667
}
});
3 changes: 2 additions & 1 deletion examples/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"@farmfe/core": "workspace:*"
},
"scripts": {
"dev": "farm start",
"start": "farm start",
"build": "farm build",
"preview": "farm preview"
}
}
}
2 changes: 1 addition & 1 deletion examples/react/.env.development
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FARM_CUSTOM_ENV_VARIABLE=1
CUSTOM_PREFIX_ENV_VARIABLE=1
FARM_EFFECTIVE_MODE_FILE_NAME=.env
FARM_EFFECTIVE_MODE_FILE_NAME=.env123132
FARM_BOOL=true
FARM_EXPAND_A=$EXPAND
FARM_EXPAND_B=$DEPEND_ENV
Expand Down
3 changes: 1 addition & 2 deletions examples/react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { BizType } from './enums';

export function Main() {
const store = useStore();
console.log(process.env.NODE_ENV);
console.log(import.meta);
console.log(import.meta.env);
return (
<>
<div>
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
"pnpm": {
"overrides": {
"@farmfe/core": "workspace:*"
},
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
}
}
}
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
"@swc/helpers": "^0.5.0",
"chokidar": "^3.5.3",
"deepmerge": "^4.3.1",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
"dotenv": "^16.4.5",
"dotenv-expand": "^11.0.6",
"execa": "^7.1.1",
"farm-browserslist-generator": "^1.0.0",
"fast-glob": "^3.3.2",
Expand Down
61 changes: 43 additions & 18 deletions packages/core/src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
/**
* The following is modified based on source found in
* https://github.com/vitejs/vite/blob/main/packages/vite/src/node/env.ts
*
* MIT License
* Copyright (c) 2019-present, Yuxi (Evan)
* https://github.com/vitejs/vite/blob/main/LICENSE
*
*/

import fs from 'node:fs';
import path from 'node:path';

import { parse } from 'dotenv';
import { expand } from 'dotenv-expand';
import { arraify, getFileSystemStats } from '../utils/index.js';
import { type DotenvPopulateInput, expand } from 'dotenv-expand';
import { arraify, normalizePath, tryStatSync } from '../utils/index.js';

// Because of the limitation of dotenv-expand,
// learn from the operation method of vite to dotenv.
/**
* The following is modified based on source found in
* https://github.com/vitejs/vite/pull/14391/files
*/
export function loadEnv(
mode: string,
envDir: string,
prefixes: string | string[] = ['FARM_', 'VITE_']
): [env: Record<string, string>, existsEnvFiles: string[]] {
if (mode === 'local') {
throw new Error(
`"local" cannot be used as a mode name because it conflicts with ` +
`the .local postfix for .env files.`
);
}
prefixes = arraify(prefixes);
const env: Record<string, string> = {};
const envFiles = getEnvFilesForMode(mode, envDir);
const existsEnvFiles: string[] = [];
const envFiles = [`.env`, `.env.local`, `.env.${mode}`, `.env.${mode}.local`];

const parsed = Object.fromEntries(
envFiles.flatMap((file) => {
const filePath = path.join(envDir, file);
if (!getFileSystemStats(filePath)?.isFile()) return [];
existsEnvFiles.push(filePath);
envFiles.flatMap((filePath) => {
if (!tryStatSync(filePath)?.isFile()) return [];

return Object.entries(parse(fs.readFileSync(filePath)));
})
);
expand({ parsed });
// For security reasons, we won't get inline env variables.
// Do not inject project process.env by default, cause it's unsafe
prefixes = arraify(prefixes);

const processEnv = { ...process.env } as DotenvPopulateInput;
expand({ parsed, processEnv });

// only keys that start with prefix are exposed to client
for (const [key, value] of Object.entries(parsed)) {
if (prefixes.some((prefix) => key.startsWith(prefix))) {
env[key] = value;
}
}

for (const key in process.env) {
if (
prefixes.some((prefix) => key.startsWith(prefix)) &&
key !== 'FARM_LIB_CORE_PATH'
) {
env[key] = process.env[key] as string;
}
}

return [env, existsEnvFiles];
}

Expand All @@ -46,3 +65,9 @@ export type CompilationMode = 'development' | 'production';
export function setProcessEnv(mode: CompilationMode) {
process.env.NODE_ENV = mode;
}

export function getEnvFilesForMode(mode: string, envDir: string): string[] {
return [`.env`, `.env.local`, `.env.${mode}`, `.env.${mode}.local`].map(
(file) => normalizePath(path.join(envDir, file))
);
}
6 changes: 3 additions & 3 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export async function normalizeUserCompilationConfig(
mode: CompilationMode = 'development',
isDefault = false
): Promise<ResolvedCompilation> {
const { compilation, root } = resolvedUserConfig;
const { compilation, root = process.cwd() } = resolvedUserConfig;

// resolve root path
const resolvedRootPath = normalizePath(root);
Expand Down Expand Up @@ -404,7 +404,6 @@ export async function normalizeUserCompilationConfig(
if (resolvedCompilation.mode === undefined) {
resolvedCompilation.mode = mode;
}

setProcessEnv(resolvedCompilation.mode);
// TODO add targetEnv `lib-browser` and `lib-node` support
const is_entry_html =
Expand Down Expand Up @@ -884,7 +883,8 @@ export async function resolveMergedUserConfig(

resolvedUserConfig.env = {
...userEnv,
NODE_ENV: mode
NODE_ENV: mergedUserConfig.compilation.mode ?? mode,
mode: mode
};

return resolvedUserConfig;
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/utils/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const version = JSON.parse(
).version;

export function normalizePath(id: string): string {
return path.posix.normalize(id ?? process.cwd());
return path.posix.normalize(id);
}

export function normalizeBasePath(basePath: string): string {
Expand Down Expand Up @@ -159,3 +159,9 @@ export function mapTargetEnvValue(config: Config['config']) {
config.output.targetEnv = 'browser';
}
}

export function tryStatSync(file: string): fs.Stats | undefined {
try {
return fs.statSync(file, { throwIfNoEntry: false });
} catch {}
}
12 changes: 9 additions & 3 deletions packages/core/tests/cjs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { expect, test } from 'vitest';
import bindingPath from '../binding/resolve-binding.cjs';

import { normalizeDevServerConfig, resolveConfig } from '../src/index.js';
import { Logger } from '../src/utils/logger.js';
Expand All @@ -20,10 +21,13 @@ test('resolveUserConfig', async () => {
// FARM_HMR_PATH: '/__hmr',
// FARM_HMR_PORT: '9000',
FARM_PROCESS_ENV: {
NODE_ENV: 'development'
NODE_ENV: 'development',
mode: 'development'
},
// FARM_HMR_PROTOCOL: 'ws',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV': '"development"'
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV':
'"development"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"development"'
});
expect(config.compilation.input).toEqual({
main: './main.tsx'
Expand All @@ -48,12 +52,14 @@ test('resolveUserConfig', async () => {
'yarn.lock'
],
envs: {
FARM_PROCESS_ENV: '{"NODE_ENV":"development"}',
FARM_PROCESS_ENV: '{"NODE_ENV":"development","mode":"development"}',
NODE_ENV: 'development',
mode: 'development',
'package.json[name]': 'farm-fe',
'package.json[type]': 'unknown',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV':
'"development"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"development"',
'package.json[browser]': 'unknown',
'package.json[exports]': 'unknown',
'package.json[main]': 'unknown',
Expand Down
35 changes: 24 additions & 11 deletions packages/core/tests/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ test('resolveUserConfig', async () => {
// FARM_HMR_PATH: '/__hmr',
// FARM_HMR_PORT: '9000',
FARM_PROCESS_ENV: {
NODE_ENV: 'development'
NODE_ENV: 'development',
mode: 'development'
},
// FARM_HMR_PROTOCOL: 'ws',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV': '"development"'
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV':
'"development"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"development"'
});
expect(config.compilation.input).toEqual({
main: './main.tsx'
Expand All @@ -52,12 +55,14 @@ test('resolveUserConfig', async () => {
'yarn.lock'
],
envs: {
FARM_PROCESS_ENV: '{"NODE_ENV":"development"}',
FARM_PROCESS_ENV: '{"NODE_ENV":"development","mode":"development"}',
NODE_ENV: 'development',
mode: 'development',
'package.json[name]': 'farm-fe',
'package.json[type]': 'unknown',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV':
'"development"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"development"',
'package.json[browser]': 'unknown',
'package.json[exports]': 'unknown',
'package.json[main]': 'unknown',
Expand Down Expand Up @@ -85,9 +90,11 @@ test('resolveUserConfig-prod', async () => {

expect(config.compilation.define).toEqual({
FARM_PROCESS_ENV: {
NODE_ENV: 'production'
NODE_ENV: 'production',
mode: 'production'
},
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV': '"production"'
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV': '"production"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"production"'
});
expect(config.compilation.input).toEqual({
main: './main.tsx'
Expand All @@ -114,16 +121,18 @@ test('resolveUserConfig-prod', async () => {
'yarn.lock'
],
envs: {
FARM_PROCESS_ENV: '{"NODE_ENV":"production"}',
FARM_PROCESS_ENV: '{"NODE_ENV":"production","mode":"production"}',
NODE_ENV: 'production',
mode: 'production',
'package.json[name]': 'farm-fe',
'package.json[type]': 'unknown',
'package.json[browser]': 'unknown',
'package.json[exports]': 'unknown',
'package.json[main]': 'unknown',
'package.json[module]': 'unknown',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV':
'"production"'
'"production"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"production"'
},
moduleCacheKeyStrategy: {}
});
Expand Down Expand Up @@ -153,9 +162,11 @@ test('resolveUserConfig-input-html-prod', async () => {

expect(config.compilation.define).toEqual({
FARM_PROCESS_ENV: {
NODE_ENV: 'production'
NODE_ENV: 'production',
mode: 'production'
},
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV': '"production"'
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV': '"production"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"production"'
});

expect(config.compilation.output).toEqual({
Expand Down Expand Up @@ -212,16 +223,18 @@ test('resolveUserConfig-input-html-prod', async () => {
'yarn.lock'
],
envs: {
FARM_PROCESS_ENV: '{"NODE_ENV":"production"}',
FARM_PROCESS_ENV: '{"NODE_ENV":"production","mode":"production"}',
NODE_ENV: 'production',
mode: 'production',
'package.json[name]': 'farm-fe',
'package.json[type]': 'unknown',
'package.json[browser]': 'unknown',
'package.json[exports]': 'unknown',
'package.json[main]': 'unknown',
'package.json[module]': 'unknown',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.NODE_ENV':
'"production"'
'"production"',
'$__farm_regex:(global(This)?\\.)?process\\.env\\.mode': '"production"'
},
moduleCacheKeyStrategy: {}
});
Expand Down
Loading

0 comments on commit 49523be

Please sign in to comment.