fix(nuxt): resolve imports from virtual files (#24022)

This commit is contained in:
Daniel Roe 2023-11-01 16:55:34 +01:00 committed by GitHub
parent 948b30d9c9
commit e753abd7b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 21 deletions

View File

@ -1,8 +1,9 @@
// We set __webpack_public_path via this import with webpack builder
import { createApp, createSSRApp, nextTick } from 'vue'
// These files must be imported first as they have side effects
// These files must be imported first as they have side effects:
// 1. (we set __webpack_public_path via this import, if using webpack builder)
import '#build/paths.mjs'
// 2. we set globalThis.$fetch via this import
import '#build/fetch.mjs'
import { applyPlugins, createNuxtApp } from './nuxt'

View File

@ -12,7 +12,7 @@ export function resolveDeepImportsPlugin (nuxt: Nuxt): Plugin {
name: 'nuxt:resolve-bare-imports',
enforce: 'post',
async resolveId (id, importer, options) {
if (!importer || isAbsolute(id) || !isAbsolute(importer) || exclude.some(e => id.startsWith(e))) {
if (!importer || isAbsolute(id) || (!isAbsolute(importer) && !importer.startsWith('virtual:')) || exclude.some(e => id.startsWith(e))) {
return
}
id = normalize(id)

View File

@ -6,7 +6,6 @@ import { generateTypes, resolveSchema } from 'untyped'
import escapeRE from 'escape-string-regexp'
import { hash } from 'ohash'
import { camelCase } from 'scule'
import { resolvePath } from 'mlly'
import { filename } from 'pathe/utils'
import type { Nuxt, NuxtApp, NuxtTemplate } from 'nuxt/schema'
import { annotatePlugins } from './app'
@ -283,10 +282,10 @@ declare module '@nuxt/schema' {
export const appConfigTemplate: NuxtTemplate = {
filename: 'app.config.mjs',
write: true,
getContents: async ({ app, nuxt }) => {
getContents ({ app, nuxt }) {
return `
import { updateAppConfig } from '#app/config'
import { defuFn } from '${await _resolveId('defu')}'
import { defuFn } from 'defu'
const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
@ -306,9 +305,9 @@ export default /* #__PURE__ */ defuFn(${app.configs.map((_id: string, index: num
export const publicPathTemplate: NuxtTemplate = {
filename: 'paths.mjs',
async getContents ({ nuxt }) {
getContents ({ nuxt }) {
return [
`import { joinURL } from '${await _resolveId('ufo')}'`,
'import { joinURL } from \'ufo\'',
!nuxt.options.dev && 'import { useRuntimeConfig } from \'#internal/nitro\'',
nuxt.options.dev
@ -338,7 +337,7 @@ export const dollarFetchTemplate: NuxtTemplate = {
filename: 'fetch.mjs',
getContents () {
return [
"import { $fetch } from 'ofetch'",
'import { $fetch } from \'ofetch\'',
"import { baseURL } from '#build/paths.mjs'",
'if (!globalThis.$fetch) {',
' globalThis.$fetch = $fetch.create({',
@ -374,15 +373,3 @@ export const nuxtConfigTemplate = {
].join('\n\n')
}
}
// TODO: Move to kit
function _resolveId (id: string) {
return resolvePath(id, {
url: [
...(typeof global.__NUXT_PREPATHS__ === 'string' ? [global.__NUXT_PREPATHS__] : global.__NUXT_PREPATHS__ || []),
import.meta.url,
process.cwd(),
...(typeof global.__NUXT_PATHS__ === 'string' ? [global.__NUXT_PATHS__] : global.__NUXT_PATHS__ || [])
]
})
}