From 94b1a3153f133d1b73d6399526e7dba13dff5cbb Mon Sep 17 00:00:00 2001 From: Remus Mate Date: Mon, 5 Sep 2022 00:18:51 +0000 Subject: [PATCH 1/5] fix(build): resolution algorithm when `build.ssr` is `true` --- packages/vite/src/node/plugins/resolve.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 371609fdf23c83..d0fe7c763489e0 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -718,6 +718,11 @@ export function tryNodeResolve( 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 } From a2e57a7c6ef3ec3b1552053891eeae8e0e1d9c99 Mon Sep 17 00:00:00 2001 From: Remus Mate Date: Mon, 5 Sep 2022 00:23:51 +0000 Subject: [PATCH 2/5] test: add tests for resolve plugin fix --- .../ssr-resolve/__tests__/ssr-resolve.spec.ts | 14 +++++++++++++ playground/ssr-resolve/main.js | 12 +++++++++++ playground/ssr-resolve/package.json | 15 +++++++++++++ playground/ssr-resolve/vite.config.js | 12 +++++++++++ pnpm-lock.yaml | 21 +++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 playground/ssr-resolve/__tests__/ssr-resolve.spec.ts create mode 100644 playground/ssr-resolve/main.js create mode 100644 playground/ssr-resolve/package.json create mode 100644 playground/ssr-resolve/vite.config.js 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..329cc6e3e921ba --- /dev/null +++ b/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts @@ -0,0 +1,14 @@ +import { expect, test } from 'vitest' +import { isBuild, testDir } from '~utils' + +test.runIf(isBuild)('correctly resolve entrypoints', async () => { + const { default: output } = await import(`${testDir}/dist/main.mjs`) + + expect(output).toMatchInlineSnapshot(` + " + Matches: 5,7 + React: 18.2.0 + Lodash: true + " + `) +}) diff --git a/playground/ssr-resolve/main.js b/playground/ssr-resolve/main.js new file mode 100644 index 00000000000000..33d10a7f70f16c --- /dev/null +++ b/playground/ssr-resolve/main.js @@ -0,0 +1,12 @@ +// no `exports` key, should resolve to autosuggest-highlight/match/index.js +import match from 'autosuggest-highlight/match' +// no `exports` key, should resolve to lodash/isInteger.js +import isInteger from 'lodash/isInteger' +// has `exports` key, should resolve to react-dom/server +import { version } from 'react-dom/server' + +export default ` + Matches: ${match('some text', 'te')} + React: ${version} + Lodash: ${isInteger(42)} +` diff --git a/playground/ssr-resolve/package.json b/playground/ssr-resolve/package.json new file mode 100644 index 00000000000000..c03a6eb19a9a0d --- /dev/null +++ b/playground/ssr-resolve/package.json @@ -0,0 +1,15 @@ +{ + "name": "ssr-resolve", + "private": true, + "version": "0.0.0", + "scripts": { + "build": "vite build", + "debug": "node --inspect-brk ../../packages/vite/bin/vite build" + }, + "dependencies": { + "autosuggest-highlight": "^3.3.4", + "lodash": "^4.17.21", + "react": "^18.2.0", + "react-dom": "^18.2.0" + } +} diff --git a/playground/ssr-resolve/vite.config.js b/playground/ssr-resolve/vite.config.js new file mode 100644 index 00000000000000..80e70f855e1dc0 --- /dev/null +++ b/playground/ssr-resolve/vite.config.js @@ -0,0 +1,12 @@ +import { resolve } from 'node:path' +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + ssr: true, + minify: false, + rollupOptions: { + input: resolve(__dirname, 'main.js') + } + } +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1d459cc2893bd6..ce7221d7887e3e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1100,6 +1100,18 @@ importers: express: 4.18.1 serve-static: 1.15.0 + playground/ssr-resolve: + specifiers: + autosuggest-highlight: ^3.3.4 + lodash: ^4.17.21 + react: ^18.2.0 + react-dom: ^18.2.0 + dependencies: + autosuggest-highlight: 3.3.4 + lodash: 4.17.21 + react: 18.2.0 + react-dom: 18.2.0_react@18.2.0 + playground/ssr-vue: specifiers: '@vitejs/plugin-vue': workspace:* @@ -3372,6 +3384,12 @@ packages: postcss-value-parser: 4.2.0 dev: false + /autosuggest-highlight/3.3.4: + resolution: {integrity: sha512-j6RETBD2xYnrVcoV1S5R4t3WxOlWZKyDQjkwnggDPSjF5L4jV98ZltBpvPvbkM1HtoSe5o+bNrTHyjPbieGeYA==} + dependencies: + remove-accents: 0.4.2 + dev: false + /axios/0.27.2: resolution: {integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==} dependencies: @@ -8116,6 +8134,9 @@ packages: engines: {node: '>=8'} dev: true + /remove-accents/0.4.2: + resolution: {integrity: sha512-7pXIJqJOq5tFgG1A2Zxti3Ht8jJF337m4sowbuHsW30ZnkQFnDzy9qBNhgzX8ZLW4+UBcXiiR7SwR6pokHsxiA==} + /require-directory/2.1.1: resolution: {integrity: sha1-jGStX9MNqxyXbiNE/+f3kqam30I=} engines: {node: '>=0.10.0'} From 8ade09db3e021af06f72e4f5a71bab0fb5d2edd7 Mon Sep 17 00:00:00 2001 From: Remus Mate Date: Mon, 5 Sep 2022 13:48:04 +1000 Subject: [PATCH 3/5] chore: clean up --- packages/vite/src/node/plugins/resolve.ts | 1 - playground/ssr-resolve/package.json | 8 ++++---- playground/ssr-resolve/vite.config.js | 1 - pnpm-lock.yaml | 8 ++++---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index d0fe7c763489e0..f3851319474b53 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -717,7 +717,6 @@ 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( diff --git a/playground/ssr-resolve/package.json b/playground/ssr-resolve/package.json index c03a6eb19a9a0d..100c0716401043 100644 --- a/playground/ssr-resolve/package.json +++ b/playground/ssr-resolve/package.json @@ -7,9 +7,9 @@ "debug": "node --inspect-brk ../../packages/vite/bin/vite build" }, "dependencies": { - "autosuggest-highlight": "^3.3.4", - "lodash": "^4.17.21", - "react": "^18.2.0", - "react-dom": "^18.2.0" + "autosuggest-highlight": "3.3.4", + "lodash": "4.17.21", + "react": "18.2.0", + "react-dom": "18.2.0" } } diff --git a/playground/ssr-resolve/vite.config.js b/playground/ssr-resolve/vite.config.js index 80e70f855e1dc0..e9a3acfba23454 100644 --- a/playground/ssr-resolve/vite.config.js +++ b/playground/ssr-resolve/vite.config.js @@ -4,7 +4,6 @@ import { defineConfig } from 'vite' export default defineConfig({ build: { ssr: true, - minify: false, rollupOptions: { input: resolve(__dirname, 'main.js') } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ce7221d7887e3e..7a8a758585a694 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1102,10 +1102,10 @@ importers: playground/ssr-resolve: specifiers: - autosuggest-highlight: ^3.3.4 - lodash: ^4.17.21 - react: ^18.2.0 - react-dom: ^18.2.0 + autosuggest-highlight: 3.3.4 + lodash: 4.17.21 + react: 18.2.0 + react-dom: 18.2.0 dependencies: autosuggest-highlight: 3.3.4 lodash: 4.17.21 From ebaaaf72da3356e2f324639eaa0adddc5d876615 Mon Sep 17 00:00:00 2001 From: Remus Mate Date: Tue, 6 Sep 2022 11:39:21 +1000 Subject: [PATCH 4/5] chore: core review feedback --- .../ssr-resolve/__tests__/ssr-resolve.spec.ts | 17 ++++---- 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 | 18 ++++----- playground/ssr-resolve/package.json | 6 +-- 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 | 5 +-- pnpm-lock.yaml | 39 +++++++++++-------- 11 files changed, 60 insertions(+), 43 deletions(-) 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/pkg-exports/entry.js create mode 100644 playground/ssr-resolve/pkg-exports/index.js create mode 100644 playground/ssr-resolve/pkg-exports/package.json diff --git a/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts b/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts index 329cc6e3e921ba..a560173281ebe6 100644 --- a/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts +++ b/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts @@ -1,14 +1,13 @@ import { expect, test } from 'vitest' -import { isBuild, testDir } from '~utils' +import { isBuild, readFile, testDir } from '~utils' test.runIf(isBuild)('correctly resolve entrypoints', async () => { - const { default: output } = await import(`${testDir}/dist/main.mjs`) + const contents = readFile('dist/main.mjs') - expect(output).toMatchInlineSnapshot(` - " - Matches: 5,7 - React: 18.2.0 - Lodash: true - " - `) + 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 index 33d10a7f70f16c..c6ba016ebd6ad5 100644 --- a/playground/ssr-resolve/main.js +++ b/playground/ssr-resolve/main.js @@ -1,12 +1,12 @@ -// no `exports` key, should resolve to autosuggest-highlight/match/index.js -import match from 'autosuggest-highlight/match' -// no `exports` key, should resolve to lodash/isInteger.js -import isInteger from 'lodash/isInteger' -// has `exports` key, should resolve to react-dom/server -import { version } from 'react-dom/server' +// 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 ` - Matches: ${match('some text', 'te')} - React: ${version} - Lodash: ${isInteger(42)} + entries/dir: ${dirEntry} + entries/file: ${fileEntry} + pkg-exports/entry: ${pkgExportsEntry} ` diff --git a/playground/ssr-resolve/package.json b/playground/ssr-resolve/package.json index 100c0716401043..f022293b5b4f08 100644 --- a/playground/ssr-resolve/package.json +++ b/playground/ssr-resolve/package.json @@ -7,9 +7,7 @@ "debug": "node --inspect-brk ../../packages/vite/bin/vite build" }, "dependencies": { - "autosuggest-highlight": "3.3.4", - "lodash": "4.17.21", - "react": "18.2.0", - "react-dom": "18.2.0" + "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 index e9a3acfba23454..bbedef4b53a6d3 100644 --- a/playground/ssr-resolve/vite.config.js +++ b/playground/ssr-resolve/vite.config.js @@ -3,9 +3,6 @@ import { defineConfig } from 'vite' export default defineConfig({ build: { - ssr: true, - rollupOptions: { - input: resolve(__dirname, 'main.js') - } + ssr: './main.js' } }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a8a758585a694..62df0c03a90195 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1102,15 +1102,17 @@ importers: playground/ssr-resolve: specifiers: - autosuggest-highlight: 3.3.4 - lodash: 4.17.21 - react: 18.2.0 - react-dom: 18.2.0 + entries: file:./entries + pkg-exports: file:./pkg-exports dependencies: - autosuggest-highlight: 3.3.4 - lodash: 4.17.21 - react: 18.2.0 - react-dom: 18.2.0_react@18.2.0 + 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: @@ -3384,12 +3386,6 @@ packages: postcss-value-parser: 4.2.0 dev: false - /autosuggest-highlight/3.3.4: - resolution: {integrity: sha512-j6RETBD2xYnrVcoV1S5R4t3WxOlWZKyDQjkwnggDPSjF5L4jV98ZltBpvPvbkM1HtoSe5o+bNrTHyjPbieGeYA==} - dependencies: - remove-accents: 0.4.2 - dev: false - /axios/0.27.2: resolution: {integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==} dependencies: @@ -8134,9 +8130,6 @@ packages: engines: {node: '>=8'} dev: true - /remove-accents/0.4.2: - resolution: {integrity: sha512-7pXIJqJOq5tFgG1A2Zxti3Ht8jJF337m4sowbuHsW30ZnkQFnDzy9qBNhgzX8ZLW4+UBcXiiR7SwR6pokHsxiA==} - /require-directory/2.1.1: resolution: {integrity: sha1-jGStX9MNqxyXbiNE/+f3kqam30I=} engines: {node: '>=0.10.0'} @@ -9863,6 +9856,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 From 41610f30facae904e089d3f4382e99f457ef6f72 Mon Sep 17 00:00:00 2001 From: Remus Mate Date: Wed, 21 Sep 2022 11:50:51 +1000 Subject: [PATCH 5/5] chore: code review feedback --- playground/ssr-resolve/vite.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/playground/ssr-resolve/vite.config.js b/playground/ssr-resolve/vite.config.js index bbedef4b53a6d3..430c2331977703 100644 --- a/playground/ssr-resolve/vite.config.js +++ b/playground/ssr-resolve/vite.config.js @@ -1,4 +1,3 @@ -import { resolve } from 'node:path' import { defineConfig } from 'vite' export default defineConfig({