From e753abd7b8fcc03b1b41563b0375f96c6026a7f8 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 1 Nov 2023 16:55:34 +0100 Subject: [PATCH] fix(nuxt): resolve imports from virtual files (#24022) --- packages/nuxt/src/app/entry.ts | 5 ++-- .../src/core/plugins/resolve-deep-imports.ts | 2 +- packages/nuxt/src/core/templates.ts | 23 ++++--------------- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/nuxt/src/app/entry.ts b/packages/nuxt/src/app/entry.ts index dbb236140a..58ad36a2de 100644 --- a/packages/nuxt/src/app/entry.ts +++ b/packages/nuxt/src/app/entry.ts @@ -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' diff --git a/packages/nuxt/src/core/plugins/resolve-deep-imports.ts b/packages/nuxt/src/core/plugins/resolve-deep-imports.ts index b785ec3b7d..3bf20e1983 100644 --- a/packages/nuxt/src/core/plugins/resolve-deep-imports.ts +++ b/packages/nuxt/src/core/plugins/resolve-deep-imports.ts @@ -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) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index bd37fe53a7..bbb28584de 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -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__ || []) - ] - }) -}