Skip to content

Commit

Permalink
fix(build): fix resolution algorithm when build.ssr is true (#9989)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrm007 authored Sep 21, 2022
1 parent dfa22ca commit 7229251
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
13 changes: 13 additions & 0 deletions playground/ssr-resolve/__tests__/ssr-resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions playground/ssr-resolve/entries/dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = __filename.slice(__filename.lastIndexOf('entries'))
1 change: 1 addition & 0 deletions playground/ssr-resolve/entries/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = __filename.slice(__filename.lastIndexOf('entries'))
5 changes: 5 additions & 0 deletions playground/ssr-resolve/entries/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "entries",
"private": true,
"version": "0.0.0"
}
12 changes: 12 additions & 0 deletions playground/ssr-resolve/main.js
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions playground/ssr-resolve/package.json
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions playground/ssr-resolve/pkg-exports/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'pkg-exports entry'
1 change: 1 addition & 0 deletions playground/ssr-resolve/pkg-exports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = undefined
9 changes: 9 additions & 0 deletions playground/ssr-resolve/pkg-exports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "pkg-exports",
"private": true,
"version": "0.0.0",
"exports": {
".": "./index.js",
"./entry": "./entry.js"
}
}
7 changes: 7 additions & 0 deletions playground/ssr-resolve/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'

export default defineConfig({
build: {
ssr: './main.js'
}
})
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7229251

Please sign in to comment.