(setup?: (config: UserConfig) => void)
| 335 | } |
| 336 | |
| 337 | export function defineEsmLibConfig(setup?: (config: UserConfig) => void) { |
| 338 | return defineConfig(() => { |
| 339 | const projectDir = cwd(); |
| 340 | const config = defaultBuildUserConfig(projectDir); |
| 341 | |
| 342 | const lib = config.build!.lib! as LibraryOptions; |
| 343 | const libEntry = lib.entry! as Record<string, string>; |
| 344 | |
| 345 | config.plugins!.push(nodeExternals()); |
| 346 | for (const file of fs.globSync('src/**/*.ts')) { |
| 347 | libEntry[path.relative('src', file.slice(0, file.length - path.extname(file).length))] = file; |
| 348 | } |
| 349 | |
| 350 | const declarationDir = path.resolve(projectDir, 'dist/types'); |
| 351 | config.plugins!.push(emitDtsPlugin({ projectDir, declarationDir: 'dist/types' })); |
| 352 | |
| 353 | const esmOutput: OutputOptions = { |
| 354 | dir: 'dist/', |
| 355 | format: 'es', |
| 356 | entryFileNames: '[name].mjs', |
| 357 | chunkFileNames: '[name].mjs', |
| 358 | plugins: [ |
| 359 | { |
| 360 | name: 'alphatab:filter-per-file-dts', |
| 361 | async writeBundle(opts) { |
| 362 | const dtsBaseDir = declarationDir; |
| 363 | if (!fs.existsSync(dtsBaseDir)) { |
| 364 | this.error(`Expected declaration directory ${dtsBaseDir} to exist`); |
| 365 | } |
| 366 | const dtsFiles: string[] = []; |
| 367 | const collect = (dir: string) => { |
| 368 | for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 369 | const full = path.join(dir, entry.name); |
| 370 | if (entry.isDirectory()) { |
| 371 | collect(full); |
| 372 | } else if (entry.name.endsWith('.d.ts')) { |
| 373 | dtsFiles.push(full); |
| 374 | } |
| 375 | } |
| 376 | }; |
| 377 | collect(dtsBaseDir); |
| 378 | |
| 379 | await createApiDtsFiles(dtsBaseDir, dtsFiles, projectDir, path.resolve(opts.dir!), { |
| 380 | error: msg => this.error(msg), |
| 381 | info: msg => this.info(msg), |
| 382 | log: msg => this.debug(msg), |
| 383 | warn: msg => this.warn(msg) |
| 384 | }); |
| 385 | } |
| 386 | } |
| 387 | ] |
| 388 | }; |
| 389 | const output = config.build!.rollupOptions!.output as OutputOptions[]; |
| 390 | output.push(esmOutput); |
| 391 | |
| 392 | setup?.(config); |
| 393 | return config; |
| 394 | }); |
no test coverage detected