mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 13:15:12 +00:00
fix(nuxt): improve accuracy of module resolution conditions (#28846)
This commit is contained in:
parent
1bdaa08fb7
commit
a39773b1ab
@ -66,6 +66,7 @@
|
||||
"@nuxt/telemetry": "^2.5.4",
|
||||
"@nuxt/vite-builder": "workspace:*",
|
||||
"@unhead/dom": "^1.10.4",
|
||||
"@unhead/shared": "^1.10.4",
|
||||
"@unhead/ssr": "^1.10.4",
|
||||
"@unhead/vue": "^1.10.4",
|
||||
"@vue/shared": "^3.5.2",
|
||||
@ -112,6 +113,7 @@
|
||||
"uncrypto": "^0.1.3",
|
||||
"unctx": "^2.3.1",
|
||||
"unenv": "^1.10.0",
|
||||
"unhead": "^1.10.4",
|
||||
"unimport": "^3.11.1",
|
||||
"unplugin": "^1.13.1",
|
||||
"unplugin-vue-router": "^0.10.7",
|
||||
|
@ -257,7 +257,8 @@ async function initNuxt (nuxt: Nuxt) {
|
||||
addWebpackPlugin(() => ImpoundPlugin.webpack(config))
|
||||
|
||||
// add resolver for modules used in virtual files
|
||||
addVitePlugin(() => resolveDeepImportsPlugin(nuxt))
|
||||
addVitePlugin(() => resolveDeepImportsPlugin(nuxt), { client: false })
|
||||
addVitePlugin(() => resolveDeepImportsPlugin(nuxt), { server: false })
|
||||
|
||||
// Add transform for `onPrehydrate` lifecycle hook
|
||||
addBuildPlugin(prehydrateTransformPlugin(nuxt))
|
||||
|
@ -8,10 +8,14 @@ import { pkgDir } from '../../dirs'
|
||||
|
||||
export function resolveDeepImportsPlugin (nuxt: Nuxt): Plugin {
|
||||
const exclude: string[] = ['virtual:', '\0virtual:', '/__skip_vite']
|
||||
let conditions: string[]
|
||||
return {
|
||||
name: 'nuxt:resolve-bare-imports',
|
||||
enforce: 'post',
|
||||
async resolveId (id, importer, options) {
|
||||
configResolved (config) {
|
||||
conditions = config.mode === 'test' ? [...config.resolve.conditions, 'import', 'require'] : config.resolve.conditions
|
||||
},
|
||||
async resolveId (id, importer) {
|
||||
if (!importer || isAbsolute(id) || (!isAbsolute(importer) && !importer.startsWith('virtual:')) || exclude.some(e => id.startsWith(e))) {
|
||||
return
|
||||
}
|
||||
@ -22,8 +26,7 @@ export function resolveDeepImportsPlugin (nuxt: Nuxt): Plugin {
|
||||
|
||||
return await this.resolve?.(normalisedId, dir, { skipSelf: true }) ?? await resolvePath(id, {
|
||||
url: [dir, ...nuxt.options.modulesDir],
|
||||
// TODO: respect nitro runtime conditions
|
||||
conditions: options.ssr ? ['node', 'import', 'require'] : ['import', 'require'],
|
||||
conditions,
|
||||
}).catch(() => {
|
||||
logger.debug('Could not resolve id', id, importer)
|
||||
return null
|
||||
|
@ -128,6 +128,19 @@ export async function buildClient (ctx: ViteBuildContext) {
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
{
|
||||
name: 'nuxt:import-conditions',
|
||||
enforce: 'post',
|
||||
config (_config, env) {
|
||||
if (env.mode !== 'test') {
|
||||
return {
|
||||
resolve: {
|
||||
conditions: [ctx.nuxt.options.dev ? 'development' : 'production', 'web', 'browser', 'import', 'module', 'default'],
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
devStyleSSRPlugin({
|
||||
srcDir: ctx.nuxt.options.srcDir,
|
||||
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir),
|
||||
|
@ -6,6 +6,7 @@ import { logger, resolvePath, tryImportModule } from '@nuxt/kit'
|
||||
import { joinURL, withTrailingSlash, withoutLeadingSlash } from 'ufo'
|
||||
import type { ViteConfig } from '@nuxt/schema'
|
||||
import defu from 'defu'
|
||||
import type { Nitro } from 'nitro/types'
|
||||
import type { ViteBuildContext } from './vite'
|
||||
import { createViteLogger } from './utils/logger'
|
||||
import { initViteNodeServer } from './vite-node'
|
||||
@ -56,6 +57,7 @@ export async function buildServer (ctx: ViteBuildContext) {
|
||||
noDiscovery: true,
|
||||
},
|
||||
resolve: {
|
||||
conditions: ((ctx.nuxt as any)._nitro as Nitro)?.options.exportConditions,
|
||||
alias: {
|
||||
'#internal/nuxt/paths': resolve(ctx.nuxt.options.buildDir, 'paths.mjs'),
|
||||
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server'),
|
||||
@ -117,6 +119,8 @@ export async function buildServer (ctx: ViteBuildContext) {
|
||||
serverConfig.ssr!.external.push(
|
||||
// explicit dependencies we use in our ssr renderer - these can be inlined (if necessary) in the nitro build
|
||||
'unhead', '@unhead/ssr', 'unctx', 'h3', 'devalue', '@nuxt/devalue', 'radix3', 'rou3', 'unstorage', 'hookable',
|
||||
// ensure we only have one version of vue if nitro is going to inline anyway
|
||||
...((ctx.nuxt as any)._nitro as Nitro).options.inlineDynamicImports ? ['vue', '@vue/server-renderer', '@unhead/vue'] : [],
|
||||
// dependencies we might share with nitro - these can be inlined (if necessary) in the nitro build
|
||||
...runtimeDependencies,
|
||||
)
|
||||
@ -143,6 +147,20 @@ export async function buildServer (ctx: ViteBuildContext) {
|
||||
viteJsxPlugin(serverConfig.vueJsx),
|
||||
)
|
||||
|
||||
if (!ctx.nuxt.options.dev) {
|
||||
serverConfig.plugins!.push({
|
||||
name: 'nuxt:nitro:vue-feature-flags',
|
||||
configResolved (config) {
|
||||
for (const key in config.define) {
|
||||
if (key.startsWith('__VUE')) {
|
||||
// tree-shake vue feature flags for non-node targets
|
||||
((ctx.nuxt as any)._nitro as Nitro).options.replace[key] = config.define[key]
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
await ctx.nuxt.callHook('vite:configResolved', serverConfig, { isClient: false, isServer: true })
|
||||
|
||||
const onBuild = () => ctx.nuxt.callHook('vite:compiled')
|
||||
|
@ -7,11 +7,6 @@
|
||||
"start": "nuxi preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@unhead/shared": "latest",
|
||||
"@vue/devtools-api": "latest",
|
||||
"@vue/shared": "latest",
|
||||
"nuxt": "workspace:*",
|
||||
"unhead": "latest",
|
||||
"vue": "latest"
|
||||
"nuxt": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
@ -293,6 +293,9 @@ importers:
|
||||
'@unhead/dom':
|
||||
specifier: ^1.10.4
|
||||
version: 1.10.4
|
||||
'@unhead/shared':
|
||||
specifier: ^1.10.4
|
||||
version: 1.10.4
|
||||
'@unhead/ssr':
|
||||
specifier: ^1.10.4
|
||||
version: 1.10.4
|
||||
@ -431,6 +434,9 @@ importers:
|
||||
unenv:
|
||||
specifier: ^1.10.0
|
||||
version: 1.10.0
|
||||
unhead:
|
||||
specifier: ^1.10.4
|
||||
version: 1.10.4
|
||||
unimport:
|
||||
specifier: ^3.11.1
|
||||
version: 3.11.1(rollup@4.21.2)(webpack-sources@3.2.3)
|
||||
@ -945,24 +951,9 @@ importers:
|
||||
|
||||
playground:
|
||||
dependencies:
|
||||
'@unhead/shared':
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
'@vue/devtools-api':
|
||||
specifier: latest
|
||||
version: 6.6.3
|
||||
'@vue/shared':
|
||||
specifier: latest
|
||||
version: 3.5.2
|
||||
nuxt:
|
||||
specifier: workspace:*
|
||||
version: link:../packages/nuxt
|
||||
unhead:
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
vue:
|
||||
specifier: 3.5.2
|
||||
version: 3.5.2(typescript@5.5.4)
|
||||
|
||||
test/fixtures/basic:
|
||||
dependencies:
|
||||
@ -973,27 +964,12 @@ importers:
|
||||
specifier: workspace:*
|
||||
version: link:../../../packages/nuxt
|
||||
devDependencies:
|
||||
'@unhead/dom':
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
'@unhead/shared':
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
'@vue/devtools-api':
|
||||
specifier: latest
|
||||
version: 6.6.3
|
||||
'@vue/shared':
|
||||
specifier: latest
|
||||
version: 3.5.2
|
||||
iron-webcrypto:
|
||||
specifier: latest
|
||||
version: 1.2.1
|
||||
ufo:
|
||||
specifier: 1.5.4
|
||||
version: 1.5.4
|
||||
unhead:
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
unplugin:
|
||||
specifier: latest
|
||||
version: 1.13.1(webpack-sources@3.2.3)
|
||||
@ -1028,18 +1004,12 @@ importers:
|
||||
nuxt:
|
||||
specifier: workspace:*
|
||||
version: link:../../../packages/nuxt
|
||||
vue:
|
||||
specifier: 3.5.2
|
||||
version: 3.5.2(typescript@5.5.4)
|
||||
|
||||
test/fixtures/minimal-types:
|
||||
dependencies:
|
||||
nuxt:
|
||||
specifier: workspace:*
|
||||
version: link:../../../packages/nuxt
|
||||
vue:
|
||||
specifier: 3.5.2
|
||||
version: 3.5.2(typescript@5.5.4)
|
||||
|
||||
test/fixtures/remote-provider: {}
|
||||
|
||||
@ -1048,44 +1018,16 @@ importers:
|
||||
nuxt:
|
||||
specifier: workspace:*
|
||||
version: link:../../../packages/nuxt
|
||||
devDependencies:
|
||||
'@unhead/shared':
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
'@vue/devtools-api':
|
||||
specifier: latest
|
||||
version: 6.6.3
|
||||
'@vue/shared':
|
||||
specifier: latest
|
||||
version: 3.5.2
|
||||
unhead:
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
|
||||
test/fixtures/suspense:
|
||||
dependencies:
|
||||
nuxt:
|
||||
specifier: workspace:*
|
||||
version: link:../../../packages/nuxt
|
||||
vue:
|
||||
specifier: 3.5.2
|
||||
version: 3.5.2(typescript@5.5.4)
|
||||
devDependencies:
|
||||
'@unhead/shared':
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
'@vue/devtools-api':
|
||||
specifier: latest
|
||||
version: 6.6.3
|
||||
'@vue/shared':
|
||||
specifier: latest
|
||||
version: 3.5.2
|
||||
typescript:
|
||||
specifier: 5.5.4
|
||||
version: 5.5.4
|
||||
unhead:
|
||||
specifier: latest
|
||||
version: 1.10.4
|
||||
|
||||
packages:
|
||||
|
||||
|
5
test/fixtures/basic/package.json
vendored
5
test/fixtures/basic/package.json
vendored
@ -9,13 +9,8 @@
|
||||
"nuxt": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@unhead/dom": "latest",
|
||||
"@unhead/shared": "latest",
|
||||
"@vue/devtools-api": "latest",
|
||||
"@vue/shared": "latest",
|
||||
"iron-webcrypto": "latest",
|
||||
"ufo": "latest",
|
||||
"unhead": "latest",
|
||||
"unplugin": "latest",
|
||||
"vue": "latest"
|
||||
}
|
||||
|
3
test/fixtures/minimal-types/package.json
vendored
3
test/fixtures/minimal-types/package.json
vendored
@ -6,7 +6,6 @@
|
||||
"test:types": "nuxi prepare && npx vue-tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"nuxt": "workspace:*",
|
||||
"vue": "latest"
|
||||
"nuxt": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
3
test/fixtures/minimal/package.json
vendored
3
test/fixtures/minimal/package.json
vendored
@ -5,7 +5,6 @@
|
||||
"build": "nuxi build"
|
||||
},
|
||||
"dependencies": {
|
||||
"nuxt": "workspace:*",
|
||||
"vue": "latest"
|
||||
"nuxt": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
6
test/fixtures/runtime-compiler/package.json
vendored
6
test/fixtures/runtime-compiler/package.json
vendored
@ -6,11 +6,5 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"nuxt": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@unhead/shared": "latest",
|
||||
"@vue/devtools-api": "latest",
|
||||
"@vue/shared": "latest",
|
||||
"unhead": "latest"
|
||||
}
|
||||
}
|
||||
|
9
test/fixtures/suspense/package.json
vendored
9
test/fixtures/suspense/package.json
vendored
@ -5,14 +5,9 @@
|
||||
"build": "nuxi build"
|
||||
},
|
||||
"dependencies": {
|
||||
"nuxt": "workspace:*",
|
||||
"vue": "latest"
|
||||
"nuxt": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@unhead/shared": "latest",
|
||||
"@vue/devtools-api": "latest",
|
||||
"@vue/shared": "latest",
|
||||
"typescript": "latest",
|
||||
"unhead": "latest"
|
||||
"typescript": "latest"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user