Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(vite-node): use externality to handle externals
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 26, 2022
1 parent 25ac652 commit 78af77e
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/vite/src/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fse from 'fs-extra'
import { resolve } from 'pathe'
import { addServerMiddleware } from '@nuxt/kit'
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 type { ViteBuildContext } from './vite'
import { isCSS } from './utils'
Expand Down Expand Up @@ -56,7 +58,8 @@ function createViteNodeMiddleware (ctx: ViteBuildContext) {
}))

app.use('/module', defineLazyEventHandler(() => {
const node: ViteNodeServer = new ViteNodeServer(ctx.ssrServer, {
const viteServer = ctx.ssrServer
const node: ViteNodeServer = new ViteNodeServer(viteServer, {
deps: {
inline: [
/\/(nuxt|nuxt3)\//,
Expand All @@ -69,6 +72,31 @@ function createViteNodeMiddleware (ctx: ViteBuildContext) {
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) => {
const moduleId = decodeURI(event.req.url).substring(1)
if (moduleId === '/') {
Expand Down

0 comments on commit 78af77e

Please sign in to comment.