mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 23:32:38 +00:00
feat(vite): use externality
to resolve externals for ssr dev bundler (#1172)
This commit is contained in:
parent
550a9f2e12
commit
d166b78e06
@ -67,7 +67,6 @@
|
|||||||
"ufo": "^0.7.9",
|
"ufo": "^0.7.9",
|
||||||
"unenv": "^0.3.10",
|
"unenv": "^0.3.10",
|
||||||
"unstorage": "^0.2.9",
|
"unstorage": "^0.2.9",
|
||||||
"vue": "3.2.20",
|
|
||||||
"vue-bundle-renderer": "^0.3.2",
|
"vue-bundle-renderer": "^0.3.2",
|
||||||
"vue-server-renderer": "^2.6.14"
|
"vue-server-renderer": "^2.6.14"
|
||||||
},
|
},
|
||||||
@ -76,7 +75,16 @@
|
|||||||
"@types/http-proxy": "^1.17.7",
|
"@types/http-proxy": "^1.17.7",
|
||||||
"@types/node-fetch": "^3.0.2",
|
"@types/node-fetch": "^3.0.2",
|
||||||
"@types/serve-static": "^1.13.10",
|
"@types/serve-static": "^1.13.10",
|
||||||
"unbuild": "latest"
|
"unbuild": "latest",
|
||||||
|
"vue": "3.2.20"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"vue": "3.2.20"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"vue": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^14.16.0 || ^16.11.0"
|
"node": "^14.16.0 || ^16.11.0"
|
||||||
|
@ -22,6 +22,7 @@ export interface NitroHooks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface NitroContext {
|
export interface NitroContext {
|
||||||
|
alias: Record<string, string>
|
||||||
timing: boolean
|
timing: boolean
|
||||||
inlineDynamicImports: boolean
|
inlineDynamicImports: boolean
|
||||||
minify: boolean
|
minify: boolean
|
||||||
@ -87,6 +88,7 @@ export type NitroPreset = NitroInput | ((input: NitroInput) => NitroInput)
|
|||||||
|
|
||||||
export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): NitroContext {
|
export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): NitroContext {
|
||||||
const defaults: NitroContext = {
|
const defaults: NitroContext = {
|
||||||
|
alias: {},
|
||||||
timing: undefined,
|
timing: undefined,
|
||||||
inlineDynamicImports: undefined,
|
inlineDynamicImports: undefined,
|
||||||
minify: undefined,
|
minify: undefined,
|
||||||
|
@ -55,7 +55,8 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
|
|||||||
'@babel/parser': 'unenv/runtime/mock/proxy',
|
'@babel/parser': 'unenv/runtime/mock/proxy',
|
||||||
'@vue/compiler-core': 'unenv/runtime/mock/proxy',
|
'@vue/compiler-core': 'unenv/runtime/mock/proxy',
|
||||||
'@vue/compiler-dom': 'unenv/runtime/mock/proxy',
|
'@vue/compiler-dom': 'unenv/runtime/mock/proxy',
|
||||||
'@vue/compiler-ssr': 'unenv/runtime/mock/proxy'
|
'@vue/compiler-ssr': 'unenv/runtime/mock/proxy',
|
||||||
|
...nitroContext.alias
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,8 @@
|
|||||||
"prepack": "unbuild"
|
"prepack": "unbuild"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"unbuild": "latest"
|
"unbuild": "latest",
|
||||||
|
"vue": "3.2.20"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/kit": "3.0.0",
|
"@nuxt/kit": "3.0.0",
|
||||||
@ -23,6 +24,7 @@
|
|||||||
"chokidar": "^3.5.2",
|
"chokidar": "^3.5.2",
|
||||||
"consola": "^2.15.3",
|
"consola": "^2.15.3",
|
||||||
"defu": "^5.0.0",
|
"defu": "^5.0.0",
|
||||||
|
"externality": "0.1.0",
|
||||||
"fs-extra": "^10.0.0",
|
"fs-extra": "^10.0.0",
|
||||||
"magic-string": "^0.25.7",
|
"magic-string": "^0.25.7",
|
||||||
"p-debounce": "^4.0.0",
|
"p-debounce": "^4.0.0",
|
||||||
@ -31,7 +33,9 @@
|
|||||||
"postcss-import-resolver": "^2.0.0",
|
"postcss-import-resolver": "^2.0.0",
|
||||||
"postcss-url": "^10.1.3",
|
"postcss-url": "^10.1.3",
|
||||||
"ufo": "^0.7.9",
|
"ufo": "^0.7.9",
|
||||||
"vite": "^2.6.10",
|
"vite": "^2.6.10"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
"vue": "3.2.20"
|
"vue": "3.2.20"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { builtinModules } from 'module'
|
|
||||||
import { pathToFileURL } from 'url'
|
import { pathToFileURL } from 'url'
|
||||||
import { join } from 'pathe'
|
|
||||||
import * as vite from 'vite'
|
import * as vite from 'vite'
|
||||||
|
import { ExternalsOptions, isExternal as _isExternal, ExternalsDefaults } from 'externality'
|
||||||
import { hashId, uniq } from './utils'
|
import { hashId, uniq } from './utils'
|
||||||
|
|
||||||
export interface TransformChunk {
|
export interface TransformChunk {
|
||||||
@ -23,25 +22,28 @@ export interface TransformOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isExternal (opts: TransformOptions, id: string) {
|
function isExternal (opts: TransformOptions, id: string) {
|
||||||
if (builtinModules.includes(id)) {
|
// Externals
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
const ssrConfig = (opts.viteServer.config as any).ssr
|
const ssrConfig = (opts.viteServer.config as any).ssr
|
||||||
|
|
||||||
if (!/\.[cm]?js/.test(id)) {
|
const externalOpts: ExternalsOptions = {
|
||||||
return false
|
inline: [
|
||||||
|
/virtual:/,
|
||||||
|
/\.ts$/,
|
||||||
|
// Things like '~', '@', etc.
|
||||||
|
...Object.keys(opts.viteServer.config.resolve.alias),
|
||||||
|
...ExternalsDefaults.inline,
|
||||||
|
...ssrConfig.noExternal
|
||||||
|
],
|
||||||
|
external: [
|
||||||
|
...ssrConfig.external,
|
||||||
|
/node_modules/
|
||||||
|
],
|
||||||
|
resolve: {
|
||||||
|
type: 'module',
|
||||||
|
extensions: ['.ts', '.js', '.json', '.vue', '.mjs', '.jsx', '.tsx', '.wasm']
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return _isExternal(id, opts.viteServer.config.root, externalOpts)
|
||||||
if (ssrConfig.noExternal.find(ext => id.includes(ext))) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ssrConfig.external.find(ext => id.includes(ext))) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return id.includes('node_modules')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function transformRequest (opts: TransformOptions, id: string) {
|
async function transformRequest (opts: TransformOptions, id: string) {
|
||||||
@ -53,14 +55,14 @@ async function transformRequest (opts: TransformOptions, id: string) {
|
|||||||
id = id.slice('/@id/'.length)
|
id = id.slice('/@id/'.length)
|
||||||
}
|
}
|
||||||
if (id && id.startsWith('/@fs/')) {
|
if (id && id.startsWith('/@fs/')) {
|
||||||
|
// Absolute path
|
||||||
id = id.slice('/@fs'.length)
|
id = id.slice('/@fs'.length)
|
||||||
}
|
} else if (!id.includes('entry') && id.startsWith('/')) {
|
||||||
if (id.startsWith('/node_modules')) {
|
// Relative to the root directory
|
||||||
id = join(opts.viteServer.config.root, id)
|
id = '.' + id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Externals
|
if (await isExternal(opts, id)) {
|
||||||
if (isExternal(opts, id)) {
|
|
||||||
return {
|
return {
|
||||||
code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => import('${(pathToFileURL(id))}').then(r => { ssrExportAll(r) })`,
|
code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => import('${(pathToFileURL(id))}').then(r => { ssrExportAll(r) })`,
|
||||||
deps: [],
|
deps: [],
|
||||||
|
@ -40,15 +40,15 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
ssr: {
|
ssr: {
|
||||||
external: [],
|
external: [],
|
||||||
noExternal: [
|
noExternal: [
|
||||||
...ctx.nuxt.options.build.transpile.filter(i => typeof i === 'string'),
|
...ctx.nuxt.options.build.transpile,
|
||||||
'.vue',
|
/\\.vue$/,
|
||||||
'plugin-vue:',
|
'plugin-vue:',
|
||||||
'/__vue-jsx',
|
'/__vue-jsx',
|
||||||
'#app',
|
'#app',
|
||||||
'nuxt3/dist',
|
/nuxt3\/dist/,
|
||||||
'nuxt3/src',
|
/nuxt3\/src/,
|
||||||
'@nuxt/nitro/dist',
|
/@nuxt\/nitro\/dist/,
|
||||||
'@nuxt/nitro/src'
|
/@nuxt\/nitro\/src/
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
|
@ -46,7 +46,6 @@
|
|||||||
"time-fix-plugin": "^2.0.7",
|
"time-fix-plugin": "^2.0.7",
|
||||||
"ufo": "^0.7.9",
|
"ufo": "^0.7.9",
|
||||||
"url-loader": "^4.1.1",
|
"url-loader": "^4.1.1",
|
||||||
"vue": "3.2.20",
|
|
||||||
"vue-loader": "^16.8.1",
|
"vue-loader": "^16.8.1",
|
||||||
"vue-style-loader": "^4.1.3",
|
"vue-style-loader": "^4.1.3",
|
||||||
"webpack": "^5.58.2",
|
"webpack": "^5.58.2",
|
||||||
@ -63,7 +62,11 @@
|
|||||||
"@types/webpack-dev-middleware": "^5.0.2",
|
"@types/webpack-dev-middleware": "^5.0.2",
|
||||||
"@types/webpack-hot-middleware": "^2.25.5",
|
"@types/webpack-hot-middleware": "^2.25.5",
|
||||||
"@types/webpack-virtual-modules": "^0",
|
"@types/webpack-virtual-modules": "^0",
|
||||||
"unbuild": "latest"
|
"unbuild": "latest",
|
||||||
|
"vue": "3.2.20"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"vue": "3.2.20"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^14.16.0 || ^16.11.0"
|
"node": "^14.16.0 || ^16.11.0"
|
||||||
|
29
yarn.lock
29
yarn.lock
@ -2684,6 +2684,11 @@ __metadata:
|
|||||||
vue: 3.2.20
|
vue: 3.2.20
|
||||||
vue-bundle-renderer: ^0.3.2
|
vue-bundle-renderer: ^0.3.2
|
||||||
vue-server-renderer: ^2.6.14
|
vue-server-renderer: ^2.6.14
|
||||||
|
peerDependencies:
|
||||||
|
vue: 3.2.20
|
||||||
|
peerDependenciesMeta:
|
||||||
|
vue:
|
||||||
|
optional: true
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
@ -2800,6 +2805,7 @@ __metadata:
|
|||||||
chokidar: ^3.5.2
|
chokidar: ^3.5.2
|
||||||
consola: ^2.15.3
|
consola: ^2.15.3
|
||||||
defu: ^5.0.0
|
defu: ^5.0.0
|
||||||
|
externality: 0.1.0
|
||||||
fs-extra: ^10.0.0
|
fs-extra: ^10.0.0
|
||||||
magic-string: ^0.25.7
|
magic-string: ^0.25.7
|
||||||
p-debounce: ^4.0.0
|
p-debounce: ^4.0.0
|
||||||
@ -2811,6 +2817,8 @@ __metadata:
|
|||||||
unbuild: latest
|
unbuild: latest
|
||||||
vite: ^2.6.10
|
vite: ^2.6.10
|
||||||
vue: 3.2.20
|
vue: 3.2.20
|
||||||
|
peerDependencies:
|
||||||
|
vue: 3.2.20
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
@ -2901,6 +2909,8 @@ __metadata:
|
|||||||
webpack-hot-middleware: ^2.25.1
|
webpack-hot-middleware: ^2.25.1
|
||||||
webpack-virtual-modules: ^0.4.3
|
webpack-virtual-modules: ^0.4.3
|
||||||
webpackbar: ^5.0.0-3
|
webpackbar: ^5.0.0-3
|
||||||
|
peerDependencies:
|
||||||
|
vue: 3.2.20
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
@ -4734,6 +4744,13 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"allowlist@npm:^0.1.1":
|
||||||
|
version: 0.1.1
|
||||||
|
resolution: "allowlist@npm:0.1.1"
|
||||||
|
checksum: 9e4894e8e52cfae74bdfb726895f1bb81f13e1ddf4419773224ca5f84efe9ef089c91abe2c1bd9933ca0c4f1e1d249e629dc60c8e805328de649121a649a5572
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"alphanum-sort@npm:^1.0.0, alphanum-sort@npm:^1.0.2":
|
"alphanum-sort@npm:^1.0.0, alphanum-sort@npm:^1.0.2":
|
||||||
version: 1.0.2
|
version: 1.0.2
|
||||||
resolution: "alphanum-sort@npm:1.0.2"
|
resolution: "alphanum-sort@npm:1.0.2"
|
||||||
@ -9408,6 +9425,18 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"externality@npm:0.1.0":
|
||||||
|
version: 0.1.0
|
||||||
|
resolution: "externality@npm:0.1.0"
|
||||||
|
dependencies:
|
||||||
|
allowlist: ^0.1.1
|
||||||
|
enhanced-resolve: ^5.8.3
|
||||||
|
ufo: ^0.7.9
|
||||||
|
upath: ^2.0.1
|
||||||
|
checksum: 3b20136e57ff2c1661c24544ce61adcbce2427e58a43dcd859e5d17ebe379bf125e6ee189bd6ea6ca9aa0476a0ef6550419eb1cde7aa29bff605a24872314097
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"extglob@npm:^2.0.4":
|
"extglob@npm:^2.0.4":
|
||||||
version: 2.0.4
|
version: 2.0.4
|
||||||
resolution: "extglob@npm:2.0.4"
|
resolution: "extglob@npm:2.0.4"
|
||||||
|
Loading…
Reference in New Issue
Block a user