mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
fix(vite): use externality
to handle vite-node externals (#6153)
This commit is contained in:
parent
f0485d83d9
commit
5e62e0fec0
@ -5,6 +5,8 @@ import fse from 'fs-extra'
|
|||||||
import { resolve } from 'pathe'
|
import { resolve } from 'pathe'
|
||||||
import { addServerMiddleware } from '@nuxt/kit'
|
import { addServerMiddleware } from '@nuxt/kit'
|
||||||
import type { Plugin as VitePlugin, ViteDevServer } from 'vite'
|
import type { Plugin as VitePlugin, ViteDevServer } from 'vite'
|
||||||
|
import { ExternalsOptions, isExternal, ExternalsDefaults } from 'externality'
|
||||||
|
import { resolve as resolveModule } from 'mlly'
|
||||||
import { distDir } from './dirs'
|
import { distDir } from './dirs'
|
||||||
import type { ViteBuildContext } from './vite'
|
import type { ViteBuildContext } from './vite'
|
||||||
import { isCSS } from './utils'
|
import { isCSS } from './utils'
|
||||||
@ -56,7 +58,8 @@ function createViteNodeMiddleware (ctx: ViteBuildContext) {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
app.use('/module', defineLazyEventHandler(() => {
|
app.use('/module', defineLazyEventHandler(() => {
|
||||||
const node: ViteNodeServer = new ViteNodeServer(ctx.ssrServer, {
|
const viteServer = ctx.ssrServer
|
||||||
|
const node: ViteNodeServer = new ViteNodeServer(viteServer, {
|
||||||
deps: {
|
deps: {
|
||||||
inline: [
|
inline: [
|
||||||
/\/(nuxt|nuxt3)\//,
|
/\/(nuxt|nuxt3)\//,
|
||||||
@ -69,6 +72,31 @@ function createViteNodeMiddleware (ctx: ViteBuildContext) {
|
|||||||
web: []
|
web: []
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
const externalOpts: ExternalsOptions = {
|
||||||
|
inline: [
|
||||||
|
/virtual:/,
|
||||||
|
/\.ts$/,
|
||||||
|
...ExternalsDefaults.inline,
|
||||||
|
...viteServer.config.ssr.noExternal as string[]
|
||||||
|
],
|
||||||
|
external: [
|
||||||
|
...viteServer.config.ssr.external,
|
||||||
|
/node_modules/
|
||||||
|
],
|
||||||
|
resolve: {
|
||||||
|
type: 'module',
|
||||||
|
extensions: ['.ts', '.js', '.json', '.vue', '.mjs', '.jsx', '.tsx', '.wasm']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const rootDir = ctx.nuxt.options.rootDir
|
||||||
|
node.shouldExternalize = async (id: string) => {
|
||||||
|
const result = await isExternal(id, rootDir, externalOpts)
|
||||||
|
if (result?.external) {
|
||||||
|
return resolveModule(result.id, { url: rootDir })
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
return async (event) => {
|
return async (event) => {
|
||||||
const moduleId = decodeURI(event.req.url).substring(1)
|
const moduleId = decodeURI(event.req.url).substring(1)
|
||||||
if (moduleId === '/') {
|
if (moduleId === '/') {
|
||||||
|
Loading…
Reference in New Issue
Block a user