-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
resolve.alias
per environment in Environment API (#17583
- Loading branch information
Showing
5 changed files
with
106 additions
and
2 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,101 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import path from 'node:path' | ||
import { expect, test } from 'vitest' | ||
import { createServer } from '../server' | ||
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner' | ||
import type { ResolveIdFn } from '../idResolver' | ||
import { createIdResolver } from '../idResolver' | ||
import { normalizePath } from '../utils' | ||
|
||
const root = fileURLToPath(new URL('./', import.meta.url)) | ||
|
||
async function createDevServer() { | ||
const server = await createServer({ | ||
configFile: false, | ||
root, | ||
logLevel: 'silent', | ||
plugins: [ | ||
(() => { | ||
let idResolver: ResolveIdFn | ||
return { | ||
name: 'environment-alias-test-plugin', | ||
configResolved(config) { | ||
idResolver = createIdResolver(config, {}) | ||
}, | ||
async resolveId(id) { | ||
return await idResolver(this.environment, id) | ||
}, | ||
} | ||
})(), | ||
], | ||
environments: { | ||
client: { | ||
resolve: { | ||
alias: [ | ||
{ | ||
find: 'mod', | ||
replacement: '/fixtures/environment-alias/test.client.js', | ||
}, | ||
], | ||
}, | ||
}, | ||
ssr: { | ||
resolve: { | ||
alias: [ | ||
{ | ||
find: 'mod', | ||
replacement: '/fixtures/environment-alias/test.ssr.js', | ||
}, | ||
], | ||
}, | ||
}, | ||
rsc: { | ||
resolve: { | ||
alias: [ | ||
{ | ||
find: 'mod', | ||
replacement: '/fixtures/environment-alias/test.rsc.js', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const moduleRunner = createServerModuleRunner(server.environments.rsc) | ||
return { server, moduleRunner } | ||
} | ||
|
||
test('alias', async () => { | ||
expect.assertions(7) | ||
const { server, moduleRunner } = await createDevServer() | ||
|
||
const [clientId, ssrId, rscId, clientReq, ssrReq, rscReq, rscMod] = | ||
await Promise.all([ | ||
server.environments.client.pluginContainer.resolveId('mod'), | ||
server.environments.ssr.pluginContainer.resolveId('mod'), | ||
server.environments.rsc.pluginContainer.resolveId('mod'), | ||
server.environments.client.transformRequest('mod'), | ||
server.environments.ssr.transformRequest('mod'), | ||
server.environments.rsc.transformRequest('mod'), | ||
moduleRunner.import('mod'), | ||
]) | ||
|
||
expect(clientId?.id).toEqual( | ||
normalizePath( | ||
path.join(root, '/fixtures/environment-alias/test.client.js'), | ||
), | ||
) | ||
expect(ssrId?.id).toEqual( | ||
normalizePath(path.join(root, '/fixtures/environment-alias/test.ssr.js')), | ||
) | ||
expect(rscId?.id).toEqual( | ||
normalizePath(path.join(root, '/fixtures/environment-alias/test.rsc.js')), | ||
) | ||
|
||
expect(clientReq?.code ?? '').toContain('(client)') | ||
expect(ssrReq?.code ?? '').toContain('(ssr)') | ||
expect(rscReq?.code ?? '').toContain('(rsc)') | ||
|
||
expect(rscMod?.msg).toContain('(rsc)') | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/environment-alias/test.client.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 const msg = `[success] (client) alias to mod path` |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/environment-alias/test.rsc.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 const msg = `[success] (rsc) alias to mod path` |
1 change: 1 addition & 0 deletions
1
packages/vite/src/node/__tests__/fixtures/environment-alias/test.ssr.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 const msg = `[success] (ssr) alias to mod path` |
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