From d5e1b104796e089ba5f4f65bd486e189ed24e8a6 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 24 May 2022 18:04:51 +0800 Subject: [PATCH 1/3] fix(cjs): build cjs for `loadEnv` --- packages/vite/src/node/config.ts | 84 +-------------------------- packages/vite/src/node/env.ts | 82 ++++++++++++++++++++++++++ packages/vite/src/node/publicUtils.ts | 1 + 3 files changed, 85 insertions(+), 82 deletions(-) create mode 100644 packages/vite/src/node/env.ts diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 1601dcbc149ca7..f64ff7c00c6502 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -4,15 +4,13 @@ import { parse as parseUrl, pathToFileURL } from 'url' import { performance } from 'perf_hooks' import { createRequire } from 'module' import colors from 'picocolors' -import dotenv from 'dotenv' -import dotenvExpand from 'dotenv-expand' import type { Alias, AliasOptions } from 'types/alias' import { createFilter } from '@rollup/pluginutils' import aliasPlugin from '@rollup/plugin-alias' import { build } from 'esbuild' import type { RollupOptions } from 'rollup' import type { Plugin } from './plugin' -import type { BuildOptions } from './build' +import type { BuildOptions, ResolvedBuildOptions } from './build' import { resolveBuildOptions } from './build' import type { ResolvedServerOptions, ServerOptions } from './server' import { resolveServerOptions } from './server' @@ -20,7 +18,6 @@ import type { PreviewOptions, ResolvedPreviewOptions } from './preview' import { resolvePreviewOptions } from './preview' import type { CSSOptions } from './plugins/css' import { - arraify, createDebugger, dynamicImport, isExternalUrl, @@ -43,7 +40,7 @@ import type { JsonOptions } from './plugins/json' import type { PluginContainer } from './server/pluginContainer' import { createPluginContainer } from './server/pluginContainer' import type { PackageCache } from './packages' -import type { ResolvedBuildOptions } from '.' +import { loadEnv, resolveEnvPrefix } from './env' const debug = createDebugger('vite:config') @@ -858,80 +855,3 @@ async function loadConfigFromBundledFile( _require.extensions[extension] = defaultLoader return config } - -export function loadEnv( - mode: string, - envDir: string, - prefixes: string | string[] = 'VITE_' -): Record { - 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 = {} - const envFiles = [ - /** mode local file */ `.env.${mode}.local`, - /** mode file */ `.env.${mode}`, - /** local file */ `.env.local`, - /** default file */ `.env` - ] - - // check if there are actual env variables starting with VITE_* - // these are typically provided inline and should be prioritized - for (const key in process.env) { - if ( - prefixes.some((prefix) => key.startsWith(prefix)) && - env[key] === undefined - ) { - env[key] = process.env[key] as string - } - } - - for (const file of envFiles) { - const path = lookupFile(envDir, [file], { pathOnly: true, rootDir: envDir }) - if (path) { - const parsed = dotenv.parse(fs.readFileSync(path), { - debug: process.env.DEBUG?.includes('vite:dotenv') || undefined - }) - - // let environment variables use each other - dotenvExpand({ - parsed, - // prevent process.env mutation - ignoreProcessEnv: true - } as any) - - // 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] === undefined - ) { - env[key] = value - } else if ( - key === 'NODE_ENV' && - process.env.VITE_USER_NODE_ENV === undefined - ) { - // NODE_ENV override in .env file - process.env.VITE_USER_NODE_ENV = value - } - } - } - } - return env -} - -export function resolveEnvPrefix({ - envPrefix = 'VITE_' -}: UserConfig): string[] { - envPrefix = arraify(envPrefix) - if (envPrefix.some((prefix) => prefix === '')) { - throw new Error( - `envPrefix option contains value '', which could lead unexpected exposure of sensitive information.` - ) - } - return envPrefix -} diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts new file mode 100644 index 00000000000000..5f204488c67efd --- /dev/null +++ b/packages/vite/src/node/env.ts @@ -0,0 +1,82 @@ +import fs from 'fs' +import dotenv from 'dotenv' +import dotenvExpand from 'dotenv-expand' +import { arraify, lookupFile } from './utils' +import type { UserConfig } from './config' + +export function loadEnv( + mode: string, + envDir: string, + prefixes: string | string[] = 'VITE_' +): Record { + 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 = {} + const envFiles = [ + /** mode local file */ `.env.${mode}.local`, + /** mode file */ `.env.${mode}`, + /** local file */ `.env.local`, + /** default file */ `.env` + ] + + // check if there are actual env variables starting with VITE_* + // these are typically provided inline and should be prioritized + for (const key in process.env) { + if ( + prefixes.some((prefix) => key.startsWith(prefix)) && + env[key] === undefined + ) { + env[key] = process.env[key] as string + } + } + + for (const file of envFiles) { + const path = lookupFile(envDir, [file], { pathOnly: true, rootDir: envDir }) + if (path) { + const parsed = dotenv.parse(fs.readFileSync(path), { + debug: process.env.DEBUG?.includes('vite:dotenv') || undefined + }) + + // let environment variables use each other + dotenvExpand({ + parsed, + // prevent process.env mutation + ignoreProcessEnv: true + } as any) + + // 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] === undefined + ) { + env[key] = value + } else if ( + key === 'NODE_ENV' && + process.env.VITE_USER_NODE_ENV === undefined + ) { + // NODE_ENV override in .env file + process.env.VITE_USER_NODE_ENV = value + } + } + } + } + return env +} + +export function resolveEnvPrefix({ + envPrefix = 'VITE_' +}: UserConfig): string[] { + envPrefix = arraify(envPrefix) + if (envPrefix.some((prefix) => prefix === '')) { + throw new Error( + `envPrefix option contains value '', which could lead unexpected exposure of sensitive information.` + ) + } + return envPrefix +} diff --git a/packages/vite/src/node/publicUtils.ts b/packages/vite/src/node/publicUtils.ts index 43234eef2bcf0c..e24db763814dec 100644 --- a/packages/vite/src/node/publicUtils.ts +++ b/packages/vite/src/node/publicUtils.ts @@ -11,3 +11,4 @@ export { normalizePath, mergeConfig, mergeAlias } from './utils' export { send } from './server/send' export { createLogger } from './logger' export { searchForWorkspaceRoot } from './server/searchRoot' +export { loadEnv, resolveEnvPrefix } from './env' From 54e3cc3f66995024072c1e77662d5351aef97314 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 24 May 2022 18:07:52 +0800 Subject: [PATCH 2/3] chore: increase limit --- packages/vite/rollup.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index a8730833917aad..9ad04d55a3658a 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -232,7 +232,7 @@ function createCjsConfig(isProduction: boolean) { ...Object.keys(pkg.dependencies), ...(isProduction ? [] : Object.keys(pkg.devDependencies)) ], - plugins: [...createNodePlugins(false, false), bundleSizeLimit(50)] + plugins: [...createNodePlugins(false, false), bundleSizeLimit(55)] }) } From 9476e347362426e02d1ecb9245adb0680a58eabe Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 25 May 2022 01:36:36 +0800 Subject: [PATCH 3/3] chore: fix tests --- packages/vite/src/node/__tests__/config.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/__tests__/config.spec.ts b/packages/vite/src/node/__tests__/config.spec.ts index 77314a896920e7..b32960497e3af4 100644 --- a/packages/vite/src/node/__tests__/config.spec.ts +++ b/packages/vite/src/node/__tests__/config.spec.ts @@ -1,7 +1,8 @@ import { describe, expect, test } from 'vitest' import type { InlineConfig } from '..' import type { UserConfig, UserConfigExport } from '../config' -import { resolveConfig, resolveEnvPrefix } from '../config' +import { resolveConfig } from '../config' +import { resolveEnvPrefix } from '../env' import { mergeConfig } from '../publicUtils' describe('mergeConfig', () => {