(config: UserConfig, projectDir: string, options: DtsRegistration = {})
| 186 | * api-extractor to produce the published bundled `dist/<name>.d.ts`. |
| 187 | */ |
| 188 | export function addDts(config: UserConfig, projectDir: string, options: DtsRegistration = {}) { |
| 189 | const tsconfigPath = options.tsconfigPath ?? path.resolve(projectDir, 'tsconfig.json'); |
| 190 | const declarationDir = path.resolve(projectDir, 'dist/types'); |
| 191 | const srcDir = path.resolve(projectDir, 'src'); |
| 192 | const shouldEmitForChunk = options.shouldEmitForChunk ?? (() => true); |
| 193 | |
| 194 | config.plugins!.push( |
| 195 | emitDtsPlugin({ projectDir, tsconfigPath, declarationDir: 'dist/types' }) |
| 196 | ); |
| 197 | |
| 198 | const externals = config.build!.rollupOptions!.external! as (string | RegExp)[]; |
| 199 | |
| 200 | const output = config.build!.rollupOptions!.output as OutputOptions[]; |
| 201 | const ensureBundlingPlugin = (o: OutputOptions) => { |
| 202 | o.plugins ??= []; |
| 203 | (o.plugins as OutputPlugin[]).push({ |
| 204 | name: 'alphatab:bundle-dts', |
| 205 | async writeBundle(opts, bundle) { |
| 206 | for (const fileName of Object.keys(bundle)) { |
| 207 | const chunk = bundle[fileName]; |
| 208 | if ( |
| 209 | chunk.type !== 'chunk' || |
| 210 | !chunk.isEntry || |
| 211 | !fileName.endsWith('.mjs') || |
| 212 | !shouldEmitForChunk(chunk) |
| 213 | ) { |
| 214 | continue; |
| 215 | } |
| 216 | // intermediates mirror the entry's path relative to src/ (rootDir). |
| 217 | const relative = path.parse(path.relative(srcDir, chunk.facadeModuleId!)); |
| 218 | const intermediate = path.join(declarationDir, relative.dir, `${relative.name}.d.ts`); |
| 219 | if (!fs.existsSync(intermediate)) { |
| 220 | this.error(`Could not find intermediate d.ts at ${intermediate}`); |
| 221 | } |
| 222 | const outFile = path.resolve(opts.dir!, fileName.replace(/\.mjs$/, '.d.ts')); |
| 223 | generateDts(projectDir, intermediate, outFile, externals); |
| 224 | } |
| 225 | } |
| 226 | }); |
| 227 | }; |
| 228 | |
| 229 | for (const o of output) { |
| 230 | if (o.format === 'es' || o.format === 'esm') { |
| 231 | ensureBundlingPlugin(o); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | export function umd( |
| 237 | config: UserConfig, |
no test coverage detected