|
| 1 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 2 | +import { dirname, relative, resolve } from 'pathe'; |
| 3 | +import { resolvePath } from 'mlly'; |
| 4 | + |
| 5 | +// 定义自己的类型 |
| 6 | +interface StubContext { |
| 7 | + options: { |
| 8 | + rootDir: string; |
| 9 | + outDir: string; |
| 10 | + declaration?: 'compatible' | 'node16' | boolean; |
| 11 | + }; |
| 12 | + pkg: { |
| 13 | + name?: string; |
| 14 | + type?: 'module' | 'commonjs'; |
| 15 | + exports?: Record<string, any>; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +// 简单的warn函数 |
| 20 | +function warn(ctx: StubContext, message: string): void { |
| 21 | + console.warn(`[rolldown] ${message}`); |
| 22 | +} |
| 23 | + |
| 24 | +interface ExportConfig { |
| 25 | + import?: string; |
| 26 | + require?: string; |
| 27 | + default?: string; |
| 28 | + [key: string]: any; |
| 29 | +} |
| 30 | +type ExportsMap = Record<string, string | ExportConfig>; |
| 31 | + |
| 32 | +export async function rolldownStub(ctx: StubContext): Promise<void> { |
| 33 | + const exports = ctx.pkg.exports as ExportsMap; |
| 34 | + if (!exports) { |
| 35 | + warn(ctx, 'No exports field found in package.json, skipping stub generation'); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // 计算 tsx 路径(相对 outDir) |
| 40 | + const tsxESMPath = relative( |
| 41 | + resolve(ctx.options.rootDir, ctx.options.outDir), |
| 42 | + await resolvePath('tsx', { url: import.meta.url, conditions: ['node', 'import'] }) |
| 43 | + ); |
| 44 | + const tsxCJSPath = relative( |
| 45 | + resolve(ctx.options.rootDir, ctx.options.outDir), |
| 46 | + await resolvePath('tsx', { url: import.meta.url, conditions: ['node', 'require'] }) |
| 47 | + ); |
| 48 | + |
| 49 | + await processExports(ctx, exports, tsxESMPath, tsxCJSPath); |
| 50 | +} |
| 51 | + |
| 52 | +async function processExports( |
| 53 | + ctx: StubContext, |
| 54 | + exports: ExportsMap, |
| 55 | + tsxESMPath: string, |
| 56 | + tsxCJSPath: string |
| 57 | +): Promise<void> { |
| 58 | + const outDir = resolve(ctx.options.rootDir, ctx.options.outDir); |
| 59 | + await mkdir(outDir, { recursive: true }); |
| 60 | + |
| 61 | + for (const [exportPath, exportConfig] of Object.entries(exports)) { |
| 62 | + const config: ExportConfig = typeof exportConfig === 'string' ? { import: exportConfig } : exportConfig; |
| 63 | + |
| 64 | + await generateStubForExport(ctx, exportPath, config, outDir, tsxESMPath, tsxCJSPath); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function generateStubForExport( |
| 69 | + ctx: StubContext, |
| 70 | + exportPath: string, |
| 71 | + exportConfig: ExportConfig, |
| 72 | + outDir: string, |
| 73 | + tsxESMPath: string, |
| 74 | + tsxCJSPath: string |
| 75 | +): Promise<void> { |
| 76 | + const sourcePath = exportConfig.import || exportConfig.require || exportConfig.default; |
| 77 | + if (!sourcePath) { |
| 78 | + warn(ctx, `No source path found for export: ${exportPath}`); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (sourcePath.includes('*')) { |
| 82 | + warn(ctx, `Wildcard export not supported: ${exportPath}`); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const resolvedSourcePath = resolve(ctx.options.rootDir, sourcePath.replace(/^\.\//, '')); |
| 87 | + const relativeSourcePath = relative(outDir, resolvedSourcePath); |
| 88 | + const outputBase = resolve(outDir, exportPath); |
| 89 | + const outputDir = dirname(outputBase); |
| 90 | + await mkdir(outputDir, { recursive: true }); |
| 91 | + |
| 92 | + // 生成ESM stub |
| 93 | + if (exportConfig.import || exportConfig.default) { |
| 94 | + const esmStub = generateESMStub(relativeSourcePath, tsxESMPath); |
| 95 | + await writeFile(`${outputBase}.mjs`, esmStub); |
| 96 | + } |
| 97 | + // 生成CJS stub |
| 98 | + if (exportConfig.require || exportConfig.default) { |
| 99 | + const cjsStub = generateCJSStub(relativeSourcePath, tsxCJSPath); |
| 100 | + await writeFile(`${outputBase}.cjs`, cjsStub); |
| 101 | + } |
| 102 | + // 生成类型声明文件 |
| 103 | + if (ctx.options.declaration) { |
| 104 | + const dtsContent = generateDTSStub(relativeSourcePath); |
| 105 | + await writeFile(`${outputBase}.d.ts`, dtsContent); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function generateESMStub(sourcePath: string, tsxESMPath: string): string { |
| 110 | + return [ |
| 111 | + `import { tsImport } from ${JSON.stringify(`${tsxESMPath}/esm/api`)};`, |
| 112 | + '', |
| 113 | + `const _module = await tsImport(new URL(${JSON.stringify(sourcePath)}, import.meta.url), import.meta.url);`, |
| 114 | + `export default _module?.default ?? _module;` |
| 115 | + ].join('\n'); |
| 116 | +} |
| 117 | + |
| 118 | +function generateCJSStub(sourcePath: string, tsxCJSPath: string): string { |
| 119 | + return [ |
| 120 | + `const tsx = require(${JSON.stringify(`${tsxCJSPath}/cjs/api`)});`, |
| 121 | + '', |
| 122 | + `const _module = tsx.require(require('path').resolve(__dirname, ${JSON.stringify(sourcePath)}));`, |
| 123 | + `module.exports = _module?.default ?? _module;` |
| 124 | + ].join('\n'); |
| 125 | +} |
| 126 | + |
| 127 | +function generateDTSStub(sourcePath: string): string { |
| 128 | + return [ |
| 129 | + `export * from ${JSON.stringify(sourcePath)};`, |
| 130 | + `export { default } from ${JSON.stringify(sourcePath)};` |
| 131 | + ].join('\n'); |
| 132 | +} |
0 commit comments