Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: specify own Node version as target when bundling config files #17307

Merged
merged 13 commits into from
Jul 30, 2024
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ async function bundleConfigFile(
absWorkingDir: process.cwd(),
entryPoints: [fileName],
write: false,
target: ['node18'],
target: [`node${process.versions.node}`],
platform: 'node',
bundle: true,
format: isESM ? 'esm' : 'cjs',
Expand Down
23 changes: 23 additions & 0 deletions playground/config/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { resolve } from 'node:path'
import { loadConfigFromFile } from 'vite'
import { expect, it } from 'vitest'

const [nvMajor, nvMinor] = process.versions.node.split('.').map(Number)
const isImportAttributesSupported =
(nvMajor === 18 && nvMinor >= 20) ||
// Node v19 doesn't support import attributes
(nvMajor === 20 && nvMinor >= 10) ||
nvMajor >= 21

it('loadConfigFromFile', async () => {
const { config } = await loadConfigFromFile(
{} as any,
Expand All @@ -24,3 +31,19 @@ it('loadConfigFromFile', async () => {
}
`)
})

it.runIf(isImportAttributesSupported)(
'loadConfigFromFile with import attributes',
async () => {
const { config } = await loadConfigFromFile(
{} as any,
resolve(__dirname, '../packages/entry/vite.config.import-attributes.ts'),
resolve(__dirname, '../packages/entry'),
)
expect(config).toMatchInlineSnapshot(`
{
"jsonValue": "vite",
}
`)
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// We have to import external json to prevent Vite / esbuild from bundling it.
import pkg from 'vite/package.json' with { type: 'json' }

export default {
jsonValue: pkg.name,
}
Loading