From b5f8824b77f8f9e73d7fa8786e0dc622f7259a83 Mon Sep 17 00:00:00 2001 From: Remus Mate Date: Wed, 21 Sep 2022 18:11:57 +1000 Subject: [PATCH] fix(build): fix resolution algorithm when `build.ssr` is true (#9989) --- packages/vite/src/node/plugins/resolve.ts | 6 ++++- .../ssr-resolve/__tests__/ssr-resolve.spec.ts | 13 ++++++++++ playground/ssr-resolve/entries/dir/index.js | 1 + playground/ssr-resolve/entries/file.js | 1 + playground/ssr-resolve/entries/package.json | 5 ++++ playground/ssr-resolve/main.js | 12 +++++++++ playground/ssr-resolve/package.json | 13 ++++++++++ playground/ssr-resolve/pkg-exports/entry.js | 1 + playground/ssr-resolve/pkg-exports/index.js | 1 + .../ssr-resolve/pkg-exports/package.json | 9 +++++++ playground/ssr-resolve/vite.config.js | 7 +++++ pnpm-lock.yaml | 26 +++++++++++++++++++ 12 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 playground/ssr-resolve/__tests__/ssr-resolve.spec.ts create mode 100644 playground/ssr-resolve/entries/dir/index.js create mode 100644 playground/ssr-resolve/entries/file.js create mode 100644 playground/ssr-resolve/entries/package.json create mode 100644 playground/ssr-resolve/main.js create mode 100644 playground/ssr-resolve/package.json create mode 100644 playground/ssr-resolve/pkg-exports/entry.js create mode 100644 playground/ssr-resolve/pkg-exports/index.js create mode 100644 playground/ssr-resolve/pkg-exports/package.json create mode 100644 playground/ssr-resolve/vite.config.js diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 2f2ccb2fb9af96..6ad8ecfe645d83 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -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 } diff --git a/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts b/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts new file mode 100644 index 00000000000000..a560173281ebe6 --- /dev/null +++ b/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts @@ -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() +}) diff --git a/playground/ssr-resolve/entries/dir/index.js b/playground/ssr-resolve/entries/dir/index.js new file mode 100644 index 00000000000000..29a5acb7305fc0 --- /dev/null +++ b/playground/ssr-resolve/entries/dir/index.js @@ -0,0 +1 @@ +module.exports = __filename.slice(__filename.lastIndexOf('entries')) diff --git a/playground/ssr-resolve/entries/file.js b/playground/ssr-resolve/entries/file.js new file mode 100644 index 00000000000000..29a5acb7305fc0 --- /dev/null +++ b/playground/ssr-resolve/entries/file.js @@ -0,0 +1 @@ +module.exports = __filename.slice(__filename.lastIndexOf('entries')) diff --git a/playground/ssr-resolve/entries/package.json b/playground/ssr-resolve/entries/package.json new file mode 100644 index 00000000000000..2e6761d4a6b63c --- /dev/null +++ b/playground/ssr-resolve/entries/package.json @@ -0,0 +1,5 @@ +{ + "name": "entries", + "private": true, + "version": "0.0.0" +} diff --git a/playground/ssr-resolve/main.js b/playground/ssr-resolve/main.js new file mode 100644 index 00000000000000..c6ba016ebd6ad5 --- /dev/null +++ b/playground/ssr-resolve/main.js @@ -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} +` diff --git a/playground/ssr-resolve/package.json b/playground/ssr-resolve/package.json new file mode 100644 index 00000000000000..f022293b5b4f08 --- /dev/null +++ b/playground/ssr-resolve/package.json @@ -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" + } +} diff --git a/playground/ssr-resolve/pkg-exports/entry.js b/playground/ssr-resolve/pkg-exports/entry.js new file mode 100644 index 00000000000000..880189c611bf02 --- /dev/null +++ b/playground/ssr-resolve/pkg-exports/entry.js @@ -0,0 +1 @@ +module.exports = 'pkg-exports entry' diff --git a/playground/ssr-resolve/pkg-exports/index.js b/playground/ssr-resolve/pkg-exports/index.js new file mode 100644 index 00000000000000..c5f2ccf114fb46 --- /dev/null +++ b/playground/ssr-resolve/pkg-exports/index.js @@ -0,0 +1 @@ +module.exports = undefined diff --git a/playground/ssr-resolve/pkg-exports/package.json b/playground/ssr-resolve/pkg-exports/package.json new file mode 100644 index 00000000000000..227450812e416c --- /dev/null +++ b/playground/ssr-resolve/pkg-exports/package.json @@ -0,0 +1,9 @@ +{ + "name": "pkg-exports", + "private": true, + "version": "0.0.0", + "exports": { + ".": "./index.js", + "./entry": "./entry.js" + } +} diff --git a/playground/ssr-resolve/vite.config.js b/playground/ssr-resolve/vite.config.js new file mode 100644 index 00000000000000..430c2331977703 --- /dev/null +++ b/playground/ssr-resolve/vite.config.js @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + ssr: './main.js' + } +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca8688cf2f451a..49d74c12e38abb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1096,6 +1096,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:* @@ -9347,6 +9361,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