mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-13 12:18:15 +00:00
fix(vite): use resolveId
from vite-node
to resolve deps (#30922)
This commit is contained in:
parent
1cf9bd36d2
commit
72d524b25e
@ -41,7 +41,6 @@
|
||||
"defu": "^6.1.4",
|
||||
"esbuild": "^0.25.0",
|
||||
"escape-string-regexp": "^5.0.0",
|
||||
"externality": "^1.0.2",
|
||||
"get-port-please": "^3.1.2",
|
||||
"h3": "npm:h3-nightly@1.14.0-20250122-114730-3f9e703",
|
||||
"jiti": "^2.4.2",
|
||||
|
@ -39,6 +39,9 @@ function createRunner () {
|
||||
return new ViteNodeRunner({
|
||||
root: viteNodeOptions.root, // Equals to Nuxt `srcDir`
|
||||
base: viteNodeOptions.base,
|
||||
async resolveId (id, importer) {
|
||||
return await viteNodeFetch('/resolve/' + encodeURIComponent(id) + (importer ? '?importer=' + encodeURIComponent(importer) : '')) ?? undefined
|
||||
},
|
||||
async fetchModule (id) {
|
||||
id = id.replace(/\/\//g, '/') // TODO: fix in vite-node
|
||||
return await viteNodeFetch('/module/' + encodeURI(id)).catch((err) => {
|
||||
|
@ -1,36 +0,0 @@
|
||||
import type { ExternalsOptions } from 'externality'
|
||||
import { ExternalsDefaults, isExternal } from 'externality'
|
||||
import type { ViteDevServer } from 'vite'
|
||||
import escapeStringRegexp from 'escape-string-regexp'
|
||||
import { withTrailingSlash } from 'ufo'
|
||||
import type { Nuxt } from 'nuxt/schema'
|
||||
import { resolve } from 'pathe'
|
||||
import { toArray } from '.'
|
||||
|
||||
export function createIsExternal (viteServer: ViteDevServer, nuxt: Nuxt) {
|
||||
const externalOpts: ExternalsOptions = {
|
||||
inline: [
|
||||
/virtual:/,
|
||||
/\.ts$/,
|
||||
...ExternalsDefaults.inline || [],
|
||||
...(
|
||||
viteServer.config.ssr.noExternal && viteServer.config.ssr.noExternal !== true
|
||||
? toArray(viteServer.config.ssr.noExternal)
|
||||
: []
|
||||
),
|
||||
],
|
||||
external: [
|
||||
'#shared',
|
||||
new RegExp('^' + escapeStringRegexp(withTrailingSlash(resolve(nuxt.options.rootDir, nuxt.options.dir.shared)))),
|
||||
...(viteServer.config.ssr.external as string[]) || [],
|
||||
/node_modules/,
|
||||
],
|
||||
resolve: {
|
||||
modules: nuxt.options.modulesDir,
|
||||
type: 'module',
|
||||
extensions: ['.ts', '.js', '.json', '.vue', '.mjs', '.jsx', '.tsx', '.wasm'],
|
||||
},
|
||||
}
|
||||
|
||||
return (id: string) => isExternal(id, nuxt.options.rootDir, externalOpts)
|
||||
}
|
@ -1,18 +1,16 @@
|
||||
import { mkdir, writeFile } from 'node:fs/promises'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import { createApp, createError, defineEventHandler, defineLazyEventHandler, eventHandler, toNodeListener } from 'h3'
|
||||
import { createApp, createError, defineEventHandler, toNodeListener } from 'h3'
|
||||
import { ViteNodeServer } from 'vite-node/server'
|
||||
import { isAbsolute, join, normalize, resolve } from 'pathe'
|
||||
// import { addDevServerHandler } from '@nuxt/kit'
|
||||
import { isFileServingAllowed } from 'vite'
|
||||
import type { ModuleNode, Plugin as VitePlugin } from 'vite'
|
||||
import type { ModuleNode, ViteDevServer, Plugin as VitePlugin } from 'vite'
|
||||
import { getQuery } from 'ufo'
|
||||
import { normalizeViteManifest } from 'vue-bundle-renderer'
|
||||
import { resolve as resolveModule } from 'mlly'
|
||||
import { distDir } from './dirs'
|
||||
import type { ViteBuildContext } from './vite'
|
||||
import { isCSS } from './utils'
|
||||
import { createIsExternal } from './utils/external'
|
||||
|
||||
// TODO: Remove this in favor of registerViteNodeMiddleware
|
||||
// after Nitropack or h3 allows adding middleware after setup
|
||||
@ -101,6 +99,19 @@ function getManifest (ctx: ViteBuildContext) {
|
||||
function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = new Set()) {
|
||||
const app = createApp()
|
||||
|
||||
let _node: ViteNodeServer | undefined
|
||||
function getNode (server: ViteDevServer) {
|
||||
return _node ||= new ViteNodeServer(server, {
|
||||
deps: {
|
||||
inline: [/^#/, /\?/],
|
||||
},
|
||||
transformMode: {
|
||||
ssr: [/.*/],
|
||||
web: [],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
app.use('/manifest', defineEventHandler(() => {
|
||||
const manifest = getManifest(ctx)
|
||||
return manifest
|
||||
@ -112,39 +123,24 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
|
||||
return ids
|
||||
}))
|
||||
|
||||
app.use('/module', defineLazyEventHandler(() => {
|
||||
const viteServer = ctx.ssrServer!
|
||||
const node = new ViteNodeServer(viteServer, {
|
||||
deps: {
|
||||
inline: [
|
||||
// Common
|
||||
/^#/,
|
||||
/\?/,
|
||||
],
|
||||
},
|
||||
transformMode: {
|
||||
ssr: [/.*/],
|
||||
web: [],
|
||||
},
|
||||
})
|
||||
|
||||
const isExternal = createIsExternal(viteServer, ctx.nuxt)
|
||||
node.shouldExternalize = async (id: string) => {
|
||||
const result = await isExternal(id)
|
||||
if (result?.external) {
|
||||
return resolveModule(result.id, { url: ctx.nuxt.options.modulesDir }).catch(() => false)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return eventHandler(async (event) => {
|
||||
const moduleId = decodeURI(event.path).substring(1)
|
||||
if (moduleId === '/') {
|
||||
const RESOLVE_RE = /^\/(?<id>[^?]+)(?:\?importer=(?<importer>.*))?$/
|
||||
app.use('/resolve', defineEventHandler(async (event) => {
|
||||
const { id, importer } = event.path.match(RESOLVE_RE)?.groups || {}
|
||||
if (!id || !ctx.ssrServer) {
|
||||
throw createError({ statusCode: 400 })
|
||||
}
|
||||
if (isAbsolute(moduleId) && !isFileServingAllowed(moduleId, viteServer)) {
|
||||
return await getNode(ctx.ssrServer).resolveId(decodeURIComponent(id), importer ? decodeURIComponent(importer) : undefined).catch(() => null)
|
||||
}))
|
||||
|
||||
app.use('/module', defineEventHandler(async (event) => {
|
||||
const moduleId = decodeURI(event.path).substring(1)
|
||||
if (moduleId === '/' || !ctx.ssrServer) {
|
||||
throw createError({ statusCode: 400 })
|
||||
}
|
||||
if (isAbsolute(moduleId) && !isFileServingAllowed(ctx.ssrServer.config, moduleId)) {
|
||||
throw createError({ statusCode: 403 /* Restricted */ })
|
||||
}
|
||||
const node = getNode(ctx.ssrServer)
|
||||
const module = await node.fetchModule(moduleId).catch(async (err) => {
|
||||
const errorData = {
|
||||
code: 'VITE_ERROR',
|
||||
@ -159,7 +155,6 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
|
||||
throw createError({ data: errorData })
|
||||
})
|
||||
return module
|
||||
})
|
||||
}))
|
||||
|
||||
return app
|
||||
|
@ -891,9 +891,6 @@ importers:
|
||||
escape-string-regexp:
|
||||
specifier: ^5.0.0
|
||||
version: 5.0.0
|
||||
externality:
|
||||
specifier: ^1.0.2
|
||||
version: 1.0.2
|
||||
get-port-please:
|
||||
specifier: ^3.1.2
|
||||
version: 3.1.2
|
||||
@ -4580,9 +4577,6 @@ packages:
|
||||
extend@3.0.2:
|
||||
resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==}
|
||||
|
||||
externality@1.0.2:
|
||||
resolution: {integrity: sha512-LyExtJWKxtgVzmgtEHyQtLFpw1KFhQphF9nTG8TpAIVkiI/xQ3FJh75tRFLYl4hkn7BNIIdLJInuDAavX35pMw==}
|
||||
|
||||
fake-indexeddb@6.0.0:
|
||||
resolution: {integrity: sha512-YEboHE5VfopUclOck7LncgIqskAqnv4q0EWbYCaxKKjAvO93c+TJIaBuGy8CBFdbg9nKdpN3AuPRwVBJ4k7NrQ==}
|
||||
engines: {node: '>=18'}
|
||||
@ -11933,13 +11927,6 @@ snapshots:
|
||||
|
||||
extend@3.0.2: {}
|
||||
|
||||
externality@1.0.2:
|
||||
dependencies:
|
||||
enhanced-resolve: 5.18.0
|
||||
mlly: 1.7.4
|
||||
pathe: 1.1.2
|
||||
ufo: 1.5.4
|
||||
|
||||
fake-indexeddb@6.0.0: {}
|
||||
|
||||
fast-deep-equal@3.1.3: {}
|
||||
|
Loading…
Reference in New Issue
Block a user