Skip to content

Commit bbf001f

Browse files
authored
fix: specify own Node version as target when bundling config files (#17307)
1 parent 659b720 commit bbf001f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/vite/src/node/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ async function bundleConfigFile(
10741074
absWorkingDir: process.cwd(),
10751075
entryPoints: [fileName],
10761076
write: false,
1077-
target: ['node18'],
1077+
target: [`node${process.versions.node}`],
10781078
platform: 'node',
10791079
bundle: true,
10801080
format: isESM ? 'esm' : 'cjs',

playground/config/__tests__/config.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { resolve } from 'node:path'
22
import { loadConfigFromFile } from 'vite'
33
import { expect, it } from 'vitest'
44

5+
const [nvMajor, nvMinor] = process.versions.node.split('.').map(Number)
6+
const isImportAttributesSupported =
7+
(nvMajor === 18 && nvMinor >= 20) ||
8+
// Node v19 doesn't support import attributes
9+
(nvMajor === 20 && nvMinor >= 10) ||
10+
nvMajor >= 21
11+
512
it('loadConfigFromFile', async () => {
613
const { config } = await loadConfigFromFile(
714
{} as any,
@@ -24,3 +31,19 @@ it('loadConfigFromFile', async () => {
2431
}
2532
`)
2633
})
34+
35+
it.runIf(isImportAttributesSupported)(
36+
'loadConfigFromFile with import attributes',
37+
async () => {
38+
const { config } = await loadConfigFromFile(
39+
{} as any,
40+
resolve(__dirname, '../packages/entry/vite.config.import-attributes.ts'),
41+
resolve(__dirname, '../packages/entry'),
42+
)
43+
expect(config).toMatchInlineSnapshot(`
44+
{
45+
"jsonValue": "vite",
46+
}
47+
`)
48+
},
49+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// We have to import external json to prevent Vite / esbuild from bundling it.
2+
import pkg from 'vite/package.json' with { type: 'json' }
3+
4+
export default {
5+
jsonValue: pkg.name,
6+
}

0 commit comments

Comments
 (0)