mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(build): fix resolution algorithm when build.ssr
is true (#9989)
This commit is contained in:
parent
dfa22ca52d
commit
7229251019
@ -721,7 +721,11 @@ export function tryNodeResolve(
|
||||
let resolvedId = id
|
||||
if (isDeepImport) {
|
||||
if (!pkg?.data.exports && path.extname(id) !== resolvedExt) {
|
||||
resolvedId += resolvedExt
|
||||
resolvedId = resolved.id.slice(resolved.id.indexOf(id))
|
||||
isDebug &&
|
||||
debug(
|
||||
`[processResult] ${colors.cyan(id)} -> ${colors.dim(resolvedId)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
return { ...resolved, id: resolvedId, external: true }
|
||||
|
13
playground/ssr-resolve/__tests__/ssr-resolve.spec.ts
Normal file
13
playground/ssr-resolve/__tests__/ssr-resolve.spec.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { expect, test } from 'vitest'
|
||||
import { isBuild, readFile, testDir } from '~utils'
|
||||
|
||||
test.runIf(isBuild)('correctly resolve entrypoints', async () => {
|
||||
const contents = readFile('dist/main.mjs')
|
||||
|
||||
const _ = `['"]`
|
||||
expect(contents).toMatch(new RegExp(`from ${_}entries/dir/index.js${_}`))
|
||||
expect(contents).toMatch(new RegExp(`from ${_}entries/file.js${_}`))
|
||||
expect(contents).toMatch(new RegExp(`from ${_}pkg-exports/entry${_}`))
|
||||
|
||||
await expect(import(`${testDir}/dist/main.mjs`)).resolves.toBeTruthy()
|
||||
})
|
1
playground/ssr-resolve/entries/dir/index.js
Normal file
1
playground/ssr-resolve/entries/dir/index.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = __filename.slice(__filename.lastIndexOf('entries'))
|
1
playground/ssr-resolve/entries/file.js
Normal file
1
playground/ssr-resolve/entries/file.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = __filename.slice(__filename.lastIndexOf('entries'))
|
5
playground/ssr-resolve/entries/package.json
Normal file
5
playground/ssr-resolve/entries/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "entries",
|
||||
"private": true,
|
||||
"version": "0.0.0"
|
||||
}
|
12
playground/ssr-resolve/main.js
Normal file
12
playground/ssr-resolve/main.js
Normal file
@ -0,0 +1,12 @@
|
||||
// no `exports` key, should resolve to entries/dir/index.js
|
||||
import dirEntry from 'entries/dir'
|
||||
// no `exports` key, should resolve to entries/file.js
|
||||
import fileEntry from 'entries/file'
|
||||
// has `exports` key, should resolve to pkg-exports/entry
|
||||
import pkgExportsEntry from 'pkg-exports/entry'
|
||||
|
||||
export default `
|
||||
entries/dir: ${dirEntry}
|
||||
entries/file: ${fileEntry}
|
||||
pkg-exports/entry: ${pkgExportsEntry}
|
||||
`
|
13
playground/ssr-resolve/package.json
Normal file
13
playground/ssr-resolve/package.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "ssr-resolve",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"build": "vite build",
|
||||
"debug": "node --inspect-brk ../../packages/vite/bin/vite build"
|
||||
},
|
||||
"dependencies": {
|
||||
"entries": "file:./entries",
|
||||
"pkg-exports": "file:./pkg-exports"
|
||||
}
|
||||
}
|
1
playground/ssr-resolve/pkg-exports/entry.js
Normal file
1
playground/ssr-resolve/pkg-exports/entry.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = 'pkg-exports entry'
|
1
playground/ssr-resolve/pkg-exports/index.js
Normal file
1
playground/ssr-resolve/pkg-exports/index.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = undefined
|
9
playground/ssr-resolve/pkg-exports/package.json
Normal file
9
playground/ssr-resolve/pkg-exports/package.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "pkg-exports",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"exports": {
|
||||
".": "./index.js",
|
||||
"./entry": "./entry.js"
|
||||
}
|
||||
}
|
7
playground/ssr-resolve/vite.config.js
Normal file
7
playground/ssr-resolve/vite.config.js
Normal file
@ -0,0 +1,7 @@
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
ssr: './main.js'
|
||||
}
|
||||
})
|
@ -1085,6 +1085,20 @@ importers:
|
||||
express: 4.18.1
|
||||
serve-static: 1.15.0
|
||||
|
||||
playground/ssr-resolve:
|
||||
specifiers:
|
||||
entries: file:./entries
|
||||
pkg-exports: file:./pkg-exports
|
||||
dependencies:
|
||||
entries: file:playground/ssr-resolve/entries
|
||||
pkg-exports: file:playground/ssr-resolve/pkg-exports
|
||||
|
||||
playground/ssr-resolve/entries:
|
||||
specifiers: {}
|
||||
|
||||
playground/ssr-resolve/pkg-exports:
|
||||
specifiers: {}
|
||||
|
||||
playground/ssr-vue:
|
||||
specifiers:
|
||||
'@vitejs/plugin-vue': workspace:*
|
||||
@ -9323,6 +9337,18 @@ packages:
|
||||
version: 0.0.0
|
||||
dev: false
|
||||
|
||||
file:playground/ssr-resolve/entries:
|
||||
resolution: {directory: playground/ssr-resolve/entries, type: directory}
|
||||
name: entries
|
||||
version: 0.0.0
|
||||
dev: false
|
||||
|
||||
file:playground/ssr-resolve/pkg-exports:
|
||||
resolution: {directory: playground/ssr-resolve/pkg-exports, type: directory}
|
||||
name: pkg-exports-cjs
|
||||
version: 0.0.0
|
||||
dev: false
|
||||
|
||||
file:playground/ssr-vue/example-external-component:
|
||||
resolution: {directory: playground/ssr-vue/example-external-component, type: directory}
|
||||
name: example-external-component
|
||||
|
Loading…
Reference in New Issue
Block a user