From 3ad30094db7d43547e116d2ff7c1462c82444823 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 10 May 2024 16:26:29 +0900 Subject: [PATCH 1/5] chore: setup test dep --- .../fixtures/mix-dep/emotion-react.cjs.js | 1 + .../fixtures/mix-dep/emotion-react.cjs.mjs | 1 + .../fixtures/mix-dep/emotion-react.esm.js | 1 + .../ssr/__tests__/fixtures/mix-dep/package.json | 13 +++++++++++++ .../vite/src/node/ssr/__tests__/package.json | 1 + .../vite/src/node/ssr/__tests__/resolve.spec.ts | 17 +++++++++++++++++ pnpm-lock.yaml | 10 ++++++++++ 7 files changed, 44 insertions(+) create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json create mode 100644 packages/vite/src/node/ssr/__tests__/resolve.spec.ts diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js new file mode 100644 index 00000000000000..81e30017b87967 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js @@ -0,0 +1 @@ +console.log('dist/emotion-react.cjs.js') diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs new file mode 100644 index 00000000000000..6010e0896a3c27 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs @@ -0,0 +1 @@ +console.log('dist/emotion-react.cjs.mjs') diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js new file mode 100644 index 00000000000000..65406a5a8406aa --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js @@ -0,0 +1 @@ +console.log('dist/emotion-react.esm.js') diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json new file mode 100644 index 00000000000000..97e9e3d74850b2 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json @@ -0,0 +1,13 @@ +{ + "name": "@vitejs/test-mix-dep", + "private": true, + "exports": { + ".": { + "module": { + "default": "./emotion-react.esm.js" + }, + "import": "./emotion-react.cjs.mjs", + "default": "./emotion-react.cjs.js" + } + } +} diff --git a/packages/vite/src/node/ssr/__tests__/package.json b/packages/vite/src/node/ssr/__tests__/package.json index a5f6e053817561..51b26c14bb20bd 100644 --- a/packages/vite/src/node/ssr/__tests__/package.json +++ b/packages/vite/src/node/ssr/__tests__/package.json @@ -3,6 +3,7 @@ "private": true, "version": "0.0.0", "dependencies": { + "@vitejs/test-mix-dep": "file:./fixtures/mix-dep", "@vitejs/cjs-ssr-dep": "link:./fixtures/cjs-ssr-dep" } } diff --git a/packages/vite/src/node/ssr/__tests__/resolve.spec.ts b/packages/vite/src/node/ssr/__tests__/resolve.spec.ts new file mode 100644 index 00000000000000..d18d9e12719320 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/resolve.spec.ts @@ -0,0 +1,17 @@ +import { fileURLToPath } from 'node:url' +import { test } from 'vitest' +import { createServer } from '../../server' + +test('resolve', async () => { + const server = await createServer({ + clearScreen: false, + configFile: false, + root: fileURLToPath(new URL('.', import.meta.url)), + }) + await server.pluginContainer.buildStart({}) + console.log( + await server.pluginContainer.resolveId('@vitejs/test-mix-dep', undefined, { + ssr: true, + }), + ) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 99ba984c13384d..e204911eeaf5b3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -456,9 +456,14 @@ importers: '@vitejs/cjs-ssr-dep': specifier: link:./fixtures/cjs-ssr-dep version: link:fixtures/cjs-ssr-dep + '@vitejs/test-mix-dep': + specifier: file:./fixtures/mix-dep + version: file:packages/vite/src/node/ssr/__tests__/fixtures/mix-dep packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep: {} + packages/vite/src/node/ssr/__tests__/fixtures/mix-dep: {} + packages/vite/src/node/ssr/runtime/__tests__: dependencies: '@vitejs/cjs-external': @@ -10711,6 +10716,11 @@ packages: resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} dev: true + file:packages/vite/src/node/ssr/__tests__/fixtures/mix-dep: + resolution: {directory: packages/vite/src/node/ssr/__tests__/fixtures/mix-dep, type: directory} + name: '@vitejs/test-mix-dep' + dev: false + file:playground/alias/dir/module: resolution: {directory: playground/alias/dir/module, type: directory} name: '@vitejs/test-aliased-module' From 60824f0bc21f10c4530a07d2745bf16761299c78 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 10 May 2024 17:24:35 +0900 Subject: [PATCH 2/5] fix: set node resolve `overrideConditions` for ssr resolveId --- packages/vite/src/node/plugins/resolve.ts | 11 ++++++++++- .../src/node/ssr/__tests__/fixtures/entry-mix-dep.ts | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index ccffd1c152972c..909a23f924257f 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -387,7 +387,16 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { (res = tryNodeResolve( id, importer, - options, + { + ...options, + overrideConditions: ssr + ? [ + ...(ssrConfig?.resolve?.externalConditions ?? []), + 'production', + 'development', + ] + : undefined, + }, targetWeb, depsOptimizer, ssr, diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts b/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts new file mode 100644 index 00000000000000..bee4a241c138ca --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts @@ -0,0 +1,2 @@ +import * as mixDep from '@vitejs/test-mix-dep' +export { mixDep } From 5ca78d69e48bfd9ce796d17afd0ced53d271e003 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 10 May 2024 17:54:30 +0900 Subject: [PATCH 3/5] test: unit --- .../ssr/__tests__/fixtures/entry-mix-dep.mjs | 2 + .../ssr/__tests__/fixtures/entry-mix-dep.ts | 2 - .../fixtures/mix-dep/emotion-react.cjs.js | 1 - .../fixtures/mix-dep/emotion-react.cjs.mjs | 1 - .../fixtures/mix-dep/emotion-react.esm.js | 1 - .../__tests__/fixtures/mix-dep/index.cjs.js | 1 + .../__tests__/fixtures/mix-dep/index.cjs.mjs | 1 + .../__tests__/fixtures/mix-dep/index.esm.js | 1 + .../__tests__/fixtures/mix-dep/package.json | 6 +- .../src/node/ssr/__tests__/resolve.spec.ts | 57 +++++++++++++++---- 10 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.mjs delete mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts delete mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js delete mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs delete mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.js create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.mjs create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.esm.js diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.mjs b/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.mjs new file mode 100644 index 00000000000000..e0a8faa6815015 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.mjs @@ -0,0 +1,2 @@ +import mixDep from '@vitejs/test-mix-dep' +export default mixDep diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts b/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts deleted file mode 100644 index bee4a241c138ca..00000000000000 --- a/packages/vite/src/node/ssr/__tests__/fixtures/entry-mix-dep.ts +++ /dev/null @@ -1,2 +0,0 @@ -import * as mixDep from '@vitejs/test-mix-dep' -export { mixDep } diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js deleted file mode 100644 index 81e30017b87967..00000000000000 --- a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.js +++ /dev/null @@ -1 +0,0 @@ -console.log('dist/emotion-react.cjs.js') diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs deleted file mode 100644 index 6010e0896a3c27..00000000000000 --- a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.cjs.mjs +++ /dev/null @@ -1 +0,0 @@ -console.log('dist/emotion-react.cjs.mjs') diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js deleted file mode 100644 index 65406a5a8406aa..00000000000000 --- a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/emotion-react.esm.js +++ /dev/null @@ -1 +0,0 @@ -console.log('dist/emotion-react.esm.js') diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.js b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.js new file mode 100644 index 00000000000000..9837417e5254d7 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.js @@ -0,0 +1 @@ +module.exports = 'default' diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.mjs b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.mjs new file mode 100644 index 00000000000000..b4dd8a6c96d5f2 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.cjs.mjs @@ -0,0 +1 @@ +export default 'import' diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.esm.js b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.esm.js new file mode 100644 index 00000000000000..4f51d884a6abf7 --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/index.esm.js @@ -0,0 +1 @@ +module.exports = 'module' diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json index 97e9e3d74850b2..10ef87e4ef9320 100644 --- a/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json +++ b/packages/vite/src/node/ssr/__tests__/fixtures/mix-dep/package.json @@ -4,10 +4,10 @@ "exports": { ".": { "module": { - "default": "./emotion-react.esm.js" + "default": "./index.esm.js" }, - "import": "./emotion-react.cjs.mjs", - "default": "./emotion-react.cjs.js" + "import": "./index.cjs.mjs", + "default": "./index.cjs.js" } } } diff --git a/packages/vite/src/node/ssr/__tests__/resolve.spec.ts b/packages/vite/src/node/ssr/__tests__/resolve.spec.ts index d18d9e12719320..1a98332d9a50ee 100644 --- a/packages/vite/src/node/ssr/__tests__/resolve.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/resolve.spec.ts @@ -1,17 +1,50 @@ import { fileURLToPath } from 'node:url' -import { test } from 'vitest' +import { describe, expect, onTestFinished, test } from 'vitest' import { createServer } from '../../server' -test('resolve', async () => { - const server = await createServer({ - clearScreen: false, - configFile: false, - root: fileURLToPath(new URL('.', import.meta.url)), +describe('exports', () => { + async function testServer() { + const server = await createServer({ + clearScreen: false, + configFile: false, + root: fileURLToPath(new URL('.', import.meta.url)), + }) + await server.pluginContainer.buildStart({}) + onTestFinished(async () => { + await server.close() + }) + return server + } + + test('resolveId ssr', async () => { + const server = await testServer() + const resolved = await server.pluginContainer.resolveId( + '@vitejs/test-mix-dep', + undefined, + { + ssr: true, + }, + ) + expect(resolved?.id).toContain('index.cjs.mjs') + }) + + test('resolveId no-ssr', async () => { + const server = await testServer() + const resolved = await server.pluginContainer.resolveId( + '@vitejs/test-mix-dep', + ) + expect(resolved?.id).toContain('index.esm.js') + }) + + test('ssrLoadModule direct', async () => { + const server = await testServer() + const mod = await server.ssrLoadModule('@vitejs/test-mix-dep') + expect(mod.default).toEqual('import') + }) + + test('ssrLoadModule external', async () => { + const server = await testServer() + const mod = await server.ssrLoadModule('/fixtures/entry-mix-dep') + expect(mod.default).toEqual('import') }) - await server.pluginContainer.buildStart({}) - console.log( - await server.pluginContainer.resolveId('@vitejs/test-mix-dep', undefined, { - ssr: true, - }), - ) }) From c3f8691b6da5973c25c3633920d1a94d075d71bc Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 10 May 2024 18:34:18 +0900 Subject: [PATCH 4/5] test: test noExternal --- .../vite/src/node/ssr/__tests__/resolve.spec.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/__tests__/resolve.spec.ts b/packages/vite/src/node/ssr/__tests__/resolve.spec.ts index 1a98332d9a50ee..d5a074cb043e9b 100644 --- a/packages/vite/src/node/ssr/__tests__/resolve.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/resolve.spec.ts @@ -1,13 +1,15 @@ import { fileURLToPath } from 'node:url' import { describe, expect, onTestFinished, test } from 'vitest' import { createServer } from '../../server' +import type { InlineConfig } from '../../config' describe('exports', () => { - async function testServer() { + async function testServer(config?: InlineConfig) { const server = await createServer({ clearScreen: false, configFile: false, root: fileURLToPath(new URL('.', import.meta.url)), + ...config, }) await server.pluginContainer.buildStart({}) onTestFinished(async () => { @@ -47,4 +49,14 @@ describe('exports', () => { const mod = await server.ssrLoadModule('/fixtures/entry-mix-dep') expect(mod.default).toEqual('import') }) + + test('ssrLoadModule inline', async () => { + const server = await testServer({ + ssr: { + noExternal: ['@vitejs/test-mix-dep'], + }, + }) + const mod = await server.ssrLoadModule('/fixtures/entry-mix-dep') + expect(mod.default).toEqual('import') + }) }) From 5ca481ca4ac6e5a08b227c1abc98c816b87bc2aa Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 10 May 2024 19:33:38 +0900 Subject: [PATCH 5/5] wip: just remove default "module"? --- packages/vite/src/node/plugins/resolve.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 909a23f924257f..9fef3b9fd2b85d 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -387,16 +387,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { (res = tryNodeResolve( id, importer, - { - ...options, - overrideConditions: ssr - ? [ - ...(ssrConfig?.resolve?.externalConditions ?? []), - 'production', - 'development', - ] - : undefined, - }, + options, targetWeb, depsOptimizer, ssr, @@ -1102,7 +1093,7 @@ function resolveExportsOrImports( options.overrideConditions || [ 'production', 'development', - 'module', + ...(targetWeb ? ['module'] : []), ...options.conditions, ], )