diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index fe5ab43f70..0df73cbb24 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -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: [ diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index ce406d4fca..17f8cca341 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -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)})` } } } },