Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CUSTOM_PACK_PROBE=custom
OTHER_PACK_PROBE=other
VITE_PACK_PROBE=default
TSDOWN_PACK_PROBE=compat
CLI_PACK_PROBE=cli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const values = [process.env.CUSTOM_PACK_PROBE, process.env.OTHER_PACK_PROBE, process.env.VITE_PACK_PROBE, process.env.TSDOWN_PACK_PROBE, process.env.CLI_PACK_PROBE];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"pack-env-prefix","private":true,"type":"module"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[[case]]
name = "pack_env_prefix_config"
vp = ["local", "global"]
steps = [
{ argv = ["vp", "pack"], snapshot = false },
["node", "verify.mjs"],
]

[[case]]
name = "pack_env_prefix_override"
vp = ["local", "global"]
steps = [
{ argv = ["vp", "pack", "--env-prefix", "CLI_"], snapshot = false },
["node", "verify.mjs", "override"],
]

[[case]]
name = "pack_env_prefix_no_config"
vp = ["local", "global"]
steps = [
{ argv = ["vp", "pack", "--no-config", "entry.js", "--env-file", "build.env"], snapshot = false },
["node", "verify.mjs", "no-config"],
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pack_env_prefix_config

## `vp pack`


## `node verify.mjs`

```
Environment prefix values and exclusions match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pack_env_prefix_config

## `vp pack`


## `node verify.mjs`

```
Environment prefix values and exclusions match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pack_env_prefix_no_config

## `vp pack --no-config entry.js --env-file build.env`


## `node verify.mjs no-config`

```
Environment prefix values and exclusions match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pack_env_prefix_no_config

## `vp pack --no-config entry.js --env-file build.env`


## `node verify.mjs no-config`

```
Environment prefix values and exclusions match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pack_env_prefix_override

## `vp pack --env-prefix CLI_`


## `node verify.mjs override`

```
Environment prefix values and exclusions match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pack_env_prefix_override

## `vp pack --env-prefix CLI_`


## `node verify.mjs override`

```
Environment prefix values and exclusions match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'node:assert/strict';

for (const key of ['CUSTOM_PACK_PROBE', 'OTHER_PACK_PROBE', 'VITE_PACK_PROBE', 'TSDOWN_PACK_PROBE', 'CLI_PACK_PROBE']) {
delete process.env[key];
}
const defaults = [undefined, undefined, 'default', 'compat', undefined];
const expected = {
string: ['custom', undefined, undefined, undefined, undefined],
array: ['custom', 'other', undefined, undefined, undefined],
empty: [undefined, undefined, undefined, undefined, undefined],
defaults,
};
if (process.argv[2] === 'no-config') {
assert.deepEqual((await import('./dist/entry.mjs')).values, defaults);
} else {
for (const [name, values] of Object.entries(expected)) {
assert.deepEqual((await import(`./dist/${name}/entry.mjs`)).values,
process.argv[2] === 'override' ? [undefined, undefined, undefined, undefined, 'cli'] : values,
name);
}
}
console.log('Environment prefix values and exclusions match.');
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
pack: [
{ name: 'string', envPrefix: 'CUSTOM_' },
{ name: 'array', envPrefix: ['CUSTOM_', 'OTHER_'] },
{ name: 'empty', envPrefix: [] },
{ name: 'defaults' },
].map(({ name, ...config }) => ({
...config,
entry: ['entry.js'],
envFile: 'build.env',
outDir: `dist/${name}`,
})),
};
4 changes: 1 addition & 3 deletions packages/cli/src/pack-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ cli
if (input.length > 0) {
flags.entry = input;
}
if (flags.envPrefix === undefined) {
flags.envPrefix = DEFAULT_ENV_PREFIXES;
}

async function runBuild(): Promise<TsdownHandle> {
const viteConfig =
Expand All @@ -163,6 +160,7 @@ cli
: [viteConfig?.pack ?? {}];
for (const packConfig of packConfigs) {
const merged = { ...packConfig, ...flags };
merged.envPrefix ??= DEFAULT_ENV_PREFIXES;
// Keep postcss/lightningcss external to the dts bundle (see plugin doc)
if (merged.dts) {
const existingPlugins = Array.isArray(merged.plugins) ? merged.plugins : [];
Expand Down
Loading