fix(build): fix resolution algorithm when build.ssr is true (#9989)

This commit is contained in:
Remus Mate 2022-09-21 18:11:57 +10:00 committed by GitHub
parent dfa22ca52d
commit 7229251019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 94 additions and 1 deletions

View File

@ -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 }

View 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()
})

View File

@ -0,0 +1 @@
module.exports = __filename.slice(__filename.lastIndexOf('entries'))

View File

@ -0,0 +1 @@
module.exports = __filename.slice(__filename.lastIndexOf('entries'))

View File

@ -0,0 +1,5 @@
{
"name": "entries",
"private": true,
"version": "0.0.0"
}

View 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}
`

View 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"
}
}

View File

@ -0,0 +1 @@
module.exports = 'pkg-exports entry'

View File

@ -0,0 +1 @@
module.exports = undefined

View File

@ -0,0 +1,9 @@
{
"name": "pkg-exports",
"private": true,
"version": "0.0.0",
"exports": {
".": "./index.js",
"./entry": "./entry.js"
}
}

View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
export default defineConfig({
build: {
ssr: './main.js'
}
})

View File

@ -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