|
1 | | -import { existsSync } from 'node:fs' |
2 | | -import { readdir, readFile } from 'node:fs/promises' |
| 1 | +import { access, readdir, readFile, writeFile } from 'node:fs/promises' |
3 | 2 | import path from 'node:path' |
4 | 3 | import fg from 'fast-glob' |
5 | 4 | import { dedupeDeps, defineConfig } from 'monoman' |
6 | 5 | import { docsLink, githubLink } from './macros/repo' |
7 | 6 | import type { PackageJson } from 'pkg-types' |
8 | 7 | import type { Options } from 'tsup' |
9 | 8 |
|
| 9 | +function exists(filePath: string) { |
| 10 | + return access(filePath).then( |
| 11 | + () => true, |
| 12 | + () => false, |
| 13 | + ) |
| 14 | +} |
| 15 | + |
10 | 16 | function getPkgName(filePath: string) { |
11 | 17 | const relative = path.relative(import.meta.dirname, filePath) |
12 | 18 | const [, pkgName] = relative.split(path.sep) |
@@ -67,8 +73,35 @@ export default defineConfig([ |
67 | 73 | if (hasRootDts) data.files.push('*.d.ts') |
68 | 74 | data.files.sort() |
69 | 75 |
|
| 76 | + if ( |
| 77 | + Object.keys(data.dependencies || {}).includes('unplugin') || |
| 78 | + data?.meta?.plugin |
| 79 | + ) { |
| 80 | + data.keywords!.push('unplugin') |
| 81 | + |
| 82 | + // write unplugin entries |
| 83 | + const entries = [ |
| 84 | + 'vite', |
| 85 | + 'webpack', |
| 86 | + 'rollup', |
| 87 | + 'esbuild', |
| 88 | + 'rspack', |
| 89 | + 'rolldown', |
| 90 | + ] |
| 91 | + Promise.all( |
| 92 | + entries.map((entry) => |
| 93 | + writeFile( |
| 94 | + path.resolve(pkgSrc, `${entry}.ts`), |
| 95 | + `import unplugin from '.'\n |
| 96 | +export default unplugin.${entry} as typeof unplugin.${entry}\n`, |
| 97 | + 'utf8', |
| 98 | + ), |
| 99 | + ), |
| 100 | + ) |
| 101 | + } |
| 102 | + |
70 | 103 | const tsupFile = path.resolve(pkgRoot, 'tsup.config.ts') |
71 | | - if (!data.meta?.skipExports && existsSync(tsupFile)) { |
| 104 | + if (!data.meta?.skipExports && (await exists(tsupFile))) { |
72 | 105 | const tsupConfig: Options = (await import(tsupFile)).default |
73 | 106 | const format = tsupConfig.format || [] |
74 | 107 | const hasCJS = format.includes('cjs') |
@@ -116,13 +149,6 @@ export default defineConfig([ |
116 | 149 | } |
117 | 150 | } |
118 | 151 |
|
119 | | - if ( |
120 | | - Object.keys(data.dependencies || {}).includes('unplugin') || |
121 | | - data?.meta?.plugin |
122 | | - ) { |
123 | | - data.keywords!.push('unplugin') |
124 | | - } |
125 | | - |
126 | 152 | return data |
127 | 153 | }, |
128 | 154 | }, |
|
0 commit comments