fix(vite): include import.meta build vars in define as well (#22576)

This commit is contained in:
Daniel Roe 2023-08-10 09:51:58 +01:00 committed by GitHub
parent a88d3f3ccd
commit 758ae76709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 5 deletions

View File

@ -22,7 +22,9 @@ export default defineUntypedSchema({
define: { define: {
$resolve: async (val, get) => ({ $resolve: async (val, get) => ({
'process.dev': await get('dev'), 'process.dev': await get('dev'),
'import.meta.dev': await get('dev'),
'process.test': isTest, 'process.test': isTest,
'import.meta.test': isTest,
...val || {} ...val || {}
}) })
}, },

View File

@ -44,6 +44,11 @@ export async function buildClient (ctx: ViteBuildContext) {
'process.browser': true, 'process.browser': true,
'process.nitro': false, 'process.nitro': false,
'process.prerender': false, 'process.prerender': false,
'import.meta.server': false,
'import.meta.client': true,
'import.meta.browser': true,
'import.meta.nitro': false,
'import.meta.prerender': false,
'module.hot': false 'module.hot': false
}, },
optimizeDeps: { optimizeDeps: {

View File

@ -43,6 +43,9 @@ export async function buildServer (ctx: ViteBuildContext) {
'process.server': true, 'process.server': true,
'process.client': false, 'process.client': false,
'process.browser': false, 'process.browser': false,
'import.meta.server': true,
'import.meta.client': false,
'import.meta.browser': false,
'typeof window': '"undefined"', 'typeof window': '"undefined"',
'typeof document': '"undefined"', 'typeof document': '"undefined"',
'typeof navigator': '"undefined"', 'typeof navigator': '"undefined"',

View File

@ -145,7 +145,7 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
nuxt.hook('vite:extendConfig', (config) => { nuxt.hook('vite:extendConfig', (config) => {
config.plugins!.push(replace({ config.plugins!.push(replace({
preventAssignment: true, preventAssignment: true,
...Object.fromEntries(Object.entries(config.define!).filter(([key]) => key.startsWith('process.')).map(([key, value]) => [key.replace('process.', 'import.meta.'), JSON.stringify(value)])) ...Object.fromEntries(Object.entries(config.define!).filter(([key]) => key.startsWith('import.meta.')))
})) }))
}) })

View File

@ -4,9 +4,5 @@ export default defineVitestConfig({
test: { test: {
dir: './test/nuxt', dir: './test/nuxt',
environment: 'nuxt' environment: 'nuxt'
},
define: {
'import.meta.client': true,
'import.meta.server': false
} }
}) })