Skip to content

Commit 1b62738

Browse files
committed
test: add test case
1 parent 8130d97 commit 1b62738

File tree

8 files changed

+71
-0
lines changed

8 files changed

+71
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from 'vitest'
2+
import { page } from '~utils'
3+
4+
test('absolute imports keep base prefix', async () => {
5+
await expect.poll(() => page.textContent('.message')).toBe('absolute import')
6+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Base Conflict</title>
6+
</head>
7+
<body>
8+
<div id="app" class="message"></div>
9+
<script type="module" src="/src/main.ts"></script>
10+
</body>
11+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@vitejs/test-base-conflict",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
10+
"preview": "vite preview"
11+
}
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'absolute import'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import message from 'absolute-importer'
2+
3+
document.querySelector('.message')!.textContent = message
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'absolute-importer' {
2+
const msg: string
3+
export default msg
4+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { defineConfig, normalizePath } from 'vite'
2+
3+
const rootPath = normalizePath(import.meta.dirname)
4+
const absoluteRoot = rootPath.startsWith('/') ? rootPath : `/${rootPath}`
5+
const [firstSegment] = absoluteRoot.split('/').filter(Boolean)
6+
const base = firstSegment ? `/${firstSegment}/` : '/'
7+
8+
const VIRTUAL_MODULE_ID = 'absolute-importer'
9+
const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID
10+
11+
const absoluteDepPath = `${rootPath}/src/importee.ts`
12+
export default defineConfig({
13+
base,
14+
plugins: [
15+
{
16+
name: 'absolute-path-import',
17+
resolveId(id) {
18+
if (id === VIRTUAL_MODULE_ID) {
19+
return RESOLVED_VIRTUAL_MODULE_ID
20+
}
21+
},
22+
load(id) {
23+
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
24+
return (
25+
`import dep from ${JSON.stringify(absoluteDepPath)}\n` +
26+
`export default dep`
27+
)
28+
}
29+
},
30+
},
31+
],
32+
})

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)