|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { |
| 3 | + InputOptions, |
| 4 | + OutputOptions, |
| 5 | + RollupOptions, |
| 6 | + rolldown, |
| 7 | +} from '@rolldown/node' |
| 8 | +import path from 'path' |
| 9 | +import fs from 'fs' |
| 10 | + |
| 11 | +runCases() |
| 12 | + |
| 13 | +function runCases() { |
| 14 | + const testCasesRoot = path.join(__dirname, 'cases') |
| 15 | + const cases = fs.readdirSync(testCasesRoot) |
| 16 | + |
| 17 | + for (const name of cases) { |
| 18 | + describe(name, async () => { |
| 19 | + const subCasesRoot = path.join(testCasesRoot, name) |
| 20 | + const subCases = fs.readdirSync(subCasesRoot) |
| 21 | + |
| 22 | + for (const subCaseName of subCases) { |
| 23 | + const caseRoot = path.join(subCasesRoot, subCaseName) |
| 24 | + const config = await getCaseConfig(caseRoot) |
| 25 | + |
| 26 | + test(subCaseName, async () => { |
| 27 | + try { |
| 28 | + await runCaseBundle(caseRoot, config) |
| 29 | + expect(true) |
| 30 | + } catch (error) { |
| 31 | + throw error |
| 32 | + } |
| 33 | + }) |
| 34 | + } |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function runCaseBundle(caseRoot: string, config?: RollupOptions) { |
| 40 | + config = normalizedOptions(caseRoot, config) |
| 41 | + const build = await rolldown(config as InputOptions) |
| 42 | + await build.write(config.output as OutputOptions) |
| 43 | +} |
| 44 | + |
| 45 | +function normalizedOptions(caseRoot: string, config?: RollupOptions) { |
| 46 | + if (Array.isArray(config?.output)) { |
| 47 | + throw new Error(`The ${caseRoot} output shouldn't be array`) |
| 48 | + } |
| 49 | + const output = config?.output ?? {} |
| 50 | + |
| 51 | + return { |
| 52 | + input: config?.input ?? path.join(caseRoot, 'main.js'), |
| 53 | + output: { |
| 54 | + dir: output.dir ?? path.join(caseRoot, 'dist'), |
| 55 | + }, |
| 56 | + ...config, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +async function getCaseConfig(caseRoot: string) { |
| 61 | + const caseConfigPath = path.join(caseRoot, 'config.ts') |
| 62 | + return fs.existsSync(caseConfigPath) |
| 63 | + ? (await import(caseConfigPath)).default |
| 64 | + : undefined |
| 65 | +} |
0 commit comments