Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ea165f
commit 8dd5cac
Showing
14 changed files
with
182 additions
and
11 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import path from 'node:path' | ||
import { resolveConfig } from 'vite' | ||
import { | ||
describe, | ||
expect, | ||
it, | ||
} from 'vitest' | ||
import { Resolve } from '../src/resolve' | ||
|
||
const root = path.join(__dirname, 'fixtures') | ||
const importer = path.join(root, 'src/main.ts') | ||
|
||
function raw(importeeRaw: string) { | ||
return importeeRaw.slice(1, -1) | ||
} | ||
|
||
describe('src/resolve.ts', async () => { | ||
const config = await resolveConfig({ configFile: path.join(root, 'vite.config.ts'), }, 'build') | ||
const resolve = new Resolve(config) | ||
|
||
it('tryResolveAlias', async () => { | ||
const list: [string, string | undefined][] = [ | ||
[ | ||
"`@/views/${id}.js`", | ||
'./views/${id}.js', | ||
], | ||
[ | ||
"`src/views/${id}.mjs`", | ||
'./views/${id}.mjs', | ||
], | ||
[ | ||
"`/root/src/views/${id}`", | ||
'./views/${id}', | ||
], | ||
[ | ||
"`./views${id}`", // without alias | ||
undefined, | ||
], | ||
[ | ||
"`./views${id}.tsx`", | ||
undefined, | ||
], | ||
[ | ||
"'@/views/' + 'foo.js'", | ||
"./views/' + 'foo.js", | ||
], | ||
[ | ||
"`@/${id}`", | ||
'./${id}', | ||
], | ||
] | ||
|
||
for (const [form, to] of list) { | ||
const resolved = await resolve.tryResolve(raw(form), importer) | ||
expect(resolved?.import.resolved).eq(to) | ||
} | ||
}) | ||
|
||
it('tryResolveBare', async () => { | ||
const resolved = await resolve.tryResolve("@ant-design/icons-svg/es/`${filename}", importer) | ||
const value = '../../../node_modules/@ant-design/icons-svg/es/`${filename}' | ||
expect(resolved?.import.resolved).eq(value) | ||
}) | ||
}) |
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,40 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { | ||
type ViteDevServer, | ||
createServer, | ||
} from 'vite' | ||
import { | ||
afterAll, | ||
beforeAll, | ||
describe, | ||
expect, | ||
it, | ||
} from 'vitest' | ||
import fetch from 'node-fetch' | ||
|
||
const root = path.join(__dirname, 'fixtures') | ||
let server: ViteDevServer | null = null | ||
const PORT = 4000 | ||
|
||
beforeAll(async () => { | ||
fs.rmSync(path.join(root, 'dist'), { recursive: true, force: true }) | ||
server = await createServer({ configFile: path.join(root, 'vite.config.ts') }) | ||
await server.listen(PORT) | ||
}) | ||
|
||
describe('vite serve', async () => { | ||
it('__snapshots__', async () => { | ||
const mainTs = await (await fetch(`http://localhost:${PORT}/src/main.ts`)).text() | ||
const mainJs = fs.readFileSync(path.join(path.join(root, 'dist/main.js')), 'utf8') | ||
const mainJsSnap = fs.readFileSync(path.join(path.join(root, '__snapshots__/main.js')), 'utf8') | ||
|
||
expect(mainTs).string | ||
expect(mainJs).eq(mainJsSnap) | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
await server?.close() | ||
server = null | ||
}) |
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,66 @@ | ||
import { | ||
describe, | ||
expect, | ||
it, | ||
} from 'vitest' | ||
import { | ||
toLooseGlob, | ||
mappingPath, | ||
} from '../src/utils' | ||
|
||
describe('src/resolve.ts', async () => { | ||
it('toLooseGlob', async () => { | ||
const list: [string, string | string[]][] = [ | ||
[ | ||
'foo/*', | ||
'foo/**/*', | ||
], | ||
[ | ||
'foo/*.js', | ||
'foo/**/*.js', | ||
], | ||
// ---- string[] ---- | ||
[ | ||
'foo*', | ||
['foo*', 'foo*/**/*'], | ||
], | ||
[ | ||
'foo*.js', | ||
['foo*.js', 'foo*/**/*.js'], | ||
], | ||
[ | ||
'foo*bar.js', | ||
['foo*bar.js', 'foo*/**/*bar.js'], | ||
], | ||
[ | ||
'foo*/bar.js', | ||
['foo*/bar.js', 'foo*/**/bar.js'], | ||
], | ||
] | ||
|
||
for (const [form, to] of list) { | ||
const glob = toLooseGlob(form) | ||
expect(glob).toEqual(to) | ||
} | ||
}) | ||
|
||
it('mappingPath', async () => { | ||
const path1 = { | ||
'./foo/index.js': [ | ||
'./foo', | ||
'./foo/index', | ||
'./foo/index.js', | ||
], | ||
} | ||
const path2 = { | ||
'./foo/index.js': [ | ||
'@/foo', | ||
'@/foo/index', | ||
'@/foo/index.js', | ||
], | ||
} | ||
|
||
expect(mappingPath(Object.keys(path1))).toEqual(path1) | ||
expect(mappingPath(Object.keys(path2), { '.': '@' })).toEqual(path2) | ||
}) | ||
}) |