diff --git a/package.json b/package.json index 3876e87..96d21ab 100644 --- a/package.json +++ b/package.json @@ -20,18 +20,21 @@ "scripts": { "dev": "vite build --watch", "build": "vite build", - "test": "vite -c test/vite.config.ts", + "test": "vitest run", "types": "tsc", - "prepublishOnly": "npm run build" + "prepublishOnly": "npm run test && npm run build" }, "dependencies": { "fast-glob": "^3.2.12" }, "devDependencies": { + "@ant-design/icons-svg": "^4.2.1", "@types/node": "^18.11.18", + "node-fetch": "^3.3.0", "typescript": "^4.9.4", "vite": "^4.0.4", - "vite-plugin-utils": "^0.4.0" + "vite-plugin-utils": "^0.4.0", + "vitest": "^0.27.2" }, "keywords": [ "vite", diff --git a/test/__snapshots__/main.js b/test/fixtures/__snapshots__/main.js similarity index 100% rename from test/__snapshots__/main.js rename to test/fixtures/__snapshots__/main.js diff --git a/test/index.html b/test/fixtures/index.html similarity index 100% rename from test/index.html rename to test/fixtures/index.html diff --git a/test/src/main.ts b/test/fixtures/src/main.ts similarity index 100% rename from test/src/main.ts rename to test/fixtures/src/main.ts diff --git a/test/src/views/bar.mjs b/test/fixtures/src/views/bar.mjs similarity index 100% rename from test/src/views/bar.mjs rename to test/fixtures/src/views/bar.mjs diff --git a/test/src/views/baz/index.tsx b/test/fixtures/src/views/baz/index.tsx similarity index 100% rename from test/src/views/baz/index.tsx rename to test/fixtures/src/views/baz/index.tsx diff --git a/test/src/views/foo.js b/test/fixtures/src/views/foo.js similarity index 100% rename from test/src/views/foo.js rename to test/fixtures/src/views/foo.js diff --git a/test/src/views/home/index.ts b/test/fixtures/src/views/home/index.ts similarity index 100% rename from test/src/views/home/index.ts rename to test/fixtures/src/views/home/index.ts diff --git a/test/src/views/nested/nesting-dir/index.tsx b/test/fixtures/src/views/nested/nesting-dir/index.tsx similarity index 100% rename from test/src/views/nested/nesting-dir/index.tsx rename to test/fixtures/src/views/nested/nesting-dir/index.tsx diff --git a/test/src/views/nested/nesting.ts b/test/fixtures/src/views/nested/nesting.ts similarity index 100% rename from test/src/views/nested/nesting.ts rename to test/fixtures/src/views/nested/nesting.ts diff --git a/test/vite.config.ts b/test/fixtures/vite.config.ts similarity index 59% rename from test/vite.config.ts rename to test/fixtures/vite.config.ts index 686ccfb..f4320b2 100644 --- a/test/vite.config.ts +++ b/test/fixtures/vite.config.ts @@ -1,23 +1,21 @@ import fs from 'fs' import path from 'path' import { defineConfig } from 'vite' -import dynamicImport from '..' +import dynamicImport from '../..' export default defineConfig({ root: __dirname, plugins: [ - dynamicImport({ - onFiles: files => files.filter(f => !f.includes('main-output.js')), - }), + dynamicImport(), { name: 'vite-plugin-dynamic-import:test', transform(code, id) { if (/src\/main\.ts$/.test(id)) { const { dir, name } = path.parse(id) - const __snapshots__ = dir.replace('src', '__snapshots__') - !fs.existsSync(__snapshots__) && fs.mkdirSync(__snapshots__, { recursive: true }) - // Write transformed code to __snapshots__ - fs.writeFileSync(path.join(__snapshots__, `${name}.js`), code) + const dist = dir.replace('src', 'dist') + !fs.existsSync(dist) && fs.mkdirSync(dist, { recursive: true }) + // Write transformed code to dist + fs.writeFileSync(path.join(dist, `${name}.js`), code) } }, }, diff --git a/test/resolve.test.ts b/test/resolve.test.ts new file mode 100644 index 0000000..4800272 --- /dev/null +++ b/test/resolve.test.ts @@ -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) + }) +}) diff --git a/test/serve.test.ts b/test/serve.test.ts new file mode 100644 index 0000000..b2012cd --- /dev/null +++ b/test/serve.test.ts @@ -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 +}) diff --git a/test/utils.test.ts b/test/utils.test.ts new file mode 100644 index 0000000..dd67fb6 --- /dev/null +++ b/test/utils.test.ts @@ -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) + }) +})