fix(nuxt): avoid injecting url helpers into globalThis (#9627)

This commit is contained in:
Daniel Roe 2023-01-14 01:27:06 +00:00 committed by GitHub
parent 86ed4ef1f4
commit 488479ab1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -24,6 +24,20 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
srcDir: nuxt.options.serverDir,
dev: nuxt.options.dev,
buildDir: nuxt.options.buildDir,
imports: {
imports: [
{
as: '__buildAssetsURL',
name: 'buildAssetsURL',
from: resolve(distDir, 'core/runtime/nitro/paths')
},
{
as: '__publicAssetsURL',
name: 'publicAssetsURL',
from: resolve(distDir, 'core/runtime/nitro/paths')
}
]
},
esbuild: {
options: {
exclude: [

View File

@ -12,6 +12,7 @@ import { writeManifest } from './manifest'
export async function buildServer (ctx: ViteBuildContext) {
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
const helper = ctx.nuxt.options.nitro.imports !== false ? '' : 'globalThis.'
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
entry: ctx.entry,
base: ctx.nuxt.options.dev
@ -24,11 +25,11 @@ export async function buildServer (ctx: ViteBuildContext) {
return { relative: true }
}
if (type === 'public') {
return { runtime: `globalThis.__publicAssetsURL(${JSON.stringify(filename)})` }
return { runtime: `${helper}__publicAssetsURL(${JSON.stringify(filename)})` }
}
if (type === 'asset') {
const relativeFilename = filename.replace(withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir)), '')
return { runtime: `globalThis.__buildAssetsURL(${JSON.stringify(relativeFilename)})` }
return { runtime: `${helper}__buildAssetsURL(${JSON.stringify(relativeFilename)})` }
}
}
},