-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(resolve): fix resolve cache to consider
conditions
and more (#1…
- Loading branch information
Showing
15 changed files
with
284 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import path from 'node:path' | ||
import { describe, expect, onTestFinished, test } from 'vitest' | ||
import type { RollupOutput } from 'rollup' | ||
import { createServer } from '../server' | ||
import { defineConfig } from '../config' | ||
import { createBuilder } from '../build' | ||
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner' | ||
|
||
describe('custom environment conditions', () => { | ||
function getConfig() { | ||
return defineConfig({ | ||
root: import.meta.dirname, | ||
logLevel: 'error', | ||
server: { | ||
middlewareMode: true, | ||
}, | ||
environments: { | ||
// no web / default | ||
ssr: { | ||
resolve: { | ||
noExternal: true, | ||
}, | ||
build: { | ||
outDir: path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist/ssr', | ||
), | ||
rollupOptions: { | ||
input: { index: '@vitejs/test-dep-conditions' }, | ||
}, | ||
}, | ||
}, | ||
// web / worker | ||
worker: { | ||
webCompatible: true, | ||
resolve: { | ||
noExternal: true, | ||
conditions: ['worker'], | ||
}, | ||
build: { | ||
outDir: path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist/worker', | ||
), | ||
rollupOptions: { | ||
input: { index: '@vitejs/test-dep-conditions' }, | ||
}, | ||
}, | ||
}, | ||
// web / custom1 | ||
custom1: { | ||
webCompatible: true, | ||
resolve: { | ||
noExternal: true, | ||
conditions: ['custom1'], | ||
}, | ||
build: { | ||
outDir: path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist/custom1', | ||
), | ||
rollupOptions: { | ||
input: { index: '@vitejs/test-dep-conditions' }, | ||
}, | ||
}, | ||
}, | ||
// no web / custom2 | ||
custom2: { | ||
webCompatible: false, | ||
resolve: { | ||
noExternal: true, | ||
conditions: ['custom2'], | ||
}, | ||
build: { | ||
outDir: path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist/custom2', | ||
), | ||
rollupOptions: { | ||
input: { index: '@vitejs/test-dep-conditions' }, | ||
}, | ||
}, | ||
}, | ||
// no web / custom3 | ||
custom3: { | ||
webCompatible: false, | ||
resolve: { | ||
noExternal: true, | ||
conditions: ['custom3'], | ||
}, | ||
build: { | ||
outDir: path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist/custom3', | ||
), | ||
rollupOptions: { | ||
input: { index: '@vitejs/test-dep-conditions' }, | ||
}, | ||
}, | ||
}, | ||
// same as custom3 | ||
custom3_2: { | ||
webCompatible: false, | ||
resolve: { | ||
noExternal: true, | ||
conditions: ['custom3'], | ||
}, | ||
build: { | ||
outDir: path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist/custom3_2', | ||
), | ||
rollupOptions: { | ||
input: { index: '@vitejs/test-dep-conditions' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
test('dev', async () => { | ||
const server = await createServer(getConfig()) | ||
onTestFinished(() => server.close()) | ||
|
||
const results: Record<string, unknown> = {} | ||
for (const key of [ | ||
'ssr', | ||
'worker', | ||
'custom1', | ||
'custom2', | ||
'custom3', | ||
'custom3_2', | ||
]) { | ||
const runner = createServerModuleRunner(server.environments[key], { | ||
hmr: { | ||
logger: false, | ||
}, | ||
sourcemapInterceptor: false, | ||
}) | ||
const mod = await runner.import('@vitejs/test-dep-conditions') | ||
results[key] = mod.default | ||
} | ||
expect(results).toMatchInlineSnapshot(` | ||
{ | ||
"custom1": "index.custom1.js", | ||
"custom2": "index.custom2.js", | ||
"custom3": "index.custom3.js", | ||
"custom3_2": "index.custom3.js", | ||
"ssr": "index.default.js", | ||
"worker": "index.worker.js", | ||
} | ||
`) | ||
}) | ||
|
||
test('css', async () => { | ||
const server = await createServer(getConfig()) | ||
onTestFinished(() => server.close()) | ||
|
||
const modJs = await server.ssrLoadModule( | ||
'/fixtures/test-dep-conditions-app/entry.js', | ||
) | ||
const modCss = await server.ssrLoadModule( | ||
'/fixtures/test-dep-conditions-app/entry.css?inline', | ||
) | ||
expect([modCss.default.replace(/\s+/g, ' '), modJs.default]) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
".test-css { color: orange; } ", | ||
"index.default.js", | ||
] | ||
`) | ||
}) | ||
|
||
test('build', async () => { | ||
const builder = await createBuilder(getConfig()) | ||
const results: Record<string, unknown> = {} | ||
for (const key of [ | ||
'ssr', | ||
'worker', | ||
'custom1', | ||
'custom2', | ||
'custom3', | ||
'custom3_2', | ||
]) { | ||
const output = await builder.build(builder.environments[key]) | ||
const chunk = (output as RollupOutput).output[0] | ||
const mod = await import( | ||
path.join( | ||
import.meta.dirname, | ||
'fixtures/test-dep-conditions/dist', | ||
key, | ||
chunk.fileName, | ||
) | ||
) | ||
results[key] = mod.default | ||
} | ||
expect(results).toMatchInlineSnapshot(` | ||
{ | ||
"custom1": "index.custom1.js", | ||
"custom2": "index.custom2.js", | ||
"custom3": "index.custom3.js", | ||
"custom3_2": "index.custom3.js", | ||
"ssr": "index.default.js", | ||
"worker": "index.worker.js", | ||
} | ||
`) | ||
}) | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions-app/entry.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import '@vitejs/test-dep-conditions'; |
2 changes: 2 additions & 0 deletions
2
packages/vite/src/node/__tests__/fixtures/test-dep-conditions-app/entry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import dep from '@vitejs/test-dep-conditions' | ||
export default dep |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.browser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'index.browser.js' |
3 changes: 3 additions & 0 deletions
3
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.test-css { | ||
color: orange; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.custom1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'index.custom1.js' |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.custom2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'index.custom2.js' |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.custom3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'index.custom3.js' |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'index.default.js' |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'index.worker.js' |
16 changes: 16 additions & 0 deletions
16
packages/vite/src/node/__tests__/fixtures/test-dep-conditions/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@vitejs/test-dep-conditions", | ||
"private": true, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"style": "./index.css", | ||
"custom1": "./index.custom1.js", | ||
"custom2": "./index.custom2.js", | ||
"custom3": "./index.custom3.js", | ||
"worker": "./index.worker.js", | ||
"browser": "./index.browser.js", | ||
"default": "./index.default.js" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.