Skip to content

Commit

Permalink
fix: specify own Node version as target when bundling config files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai authored Jul 30, 2024
1 parent 659b720 commit bbf001f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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,
}

0 comments on commit bbf001f

Please sign in to comment.