|
1 | 1 | /** |
2 | | - * @file Unbuild Config |
3 | | - * @module config/unbuild |
4 | | - * @see https://github.com/unjs/unbuild#configuration |
| 2 | + * @file Build Config |
| 3 | + * @module config/build |
| 4 | + * @see https://github.com/flex-development/mkbuild#configuration |
5 | 5 | */ |
6 | 6 |
|
7 | | -import type { MkdistOptions } from 'mkdist' |
8 | | -import path from 'node:path' |
9 | | -import { applyChanges } from 'resolve-tspaths/dist/steps/applyChanges' |
10 | | -import { computeAliases } from 'resolve-tspaths/dist/steps/computeAliases' |
11 | | -import { generateChanges } from 'resolve-tspaths/dist/steps/generateChanges' |
12 | | -import { getFilesToProcess } from 'resolve-tspaths/dist/steps/getFilesToProcess' |
13 | | -import { loadTSConfig } from 'resolve-tspaths/dist/steps/loadTSConfig' |
14 | | -import type { Alias, Change, TextChange } from 'resolve-tspaths/dist/types' |
15 | | -import { |
16 | | - defineBuildConfig, |
17 | | - type BuildConfig, |
18 | | - type BuildContext, |
19 | | - type BuildOptions, |
20 | | - type MkdistBuildEntry |
21 | | -} from 'unbuild' |
| 7 | +import { defineBuildConfig, type Config } from '@flex-development/mkbuild' |
| 8 | +import pkg from './package.json' assert { type: 'json' } |
| 9 | +import tsconfig from './tsconfig.build.json' assert { type: 'json' } |
22 | 10 |
|
23 | | -/** @const {BuildConfig} config - Build config */ |
24 | | -const config: BuildConfig = defineBuildConfig({ |
25 | | - declaration: true, |
| 11 | +/** |
| 12 | + * Build configuration options. |
| 13 | + * |
| 14 | + * @const {Config} config |
| 15 | + */ |
| 16 | +const config: Config = defineBuildConfig({ |
26 | 17 | entries: [ |
27 | | - { builder: 'mkdist', input: 'src/' }, |
28 | | - { builder: 'mkdist', ext: 'cjs', format: 'cjs', input: 'src/' }, |
29 | | - { builder: 'mkdist', ext: 'mjs', format: 'esm', input: 'src/' } |
| 18 | + { ext: '.mjs', format: 'esm' }, |
| 19 | + { ext: '.cjs', format: 'cjs' } |
30 | 20 | ], |
31 | | - hooks: { |
32 | | - /** |
33 | | - * Transforms path aliases found in build files. |
34 | | - * |
35 | | - * @param {BuildContext} ctx - Build context |
36 | | - * @param {BuildOptions} ctx.options - Build options |
37 | | - * @return {void} Nothing when complete |
38 | | - */ |
39 | | - 'mkdist:done'({ options }: BuildContext): void { |
40 | | - const { outDir, rootDir } = options |
41 | | - |
42 | | - try { |
43 | | - const { paths = {} } = loadTSConfig(`${rootDir}/tsconfig.json`).options |
44 | | - |
45 | | - /** @const {string[]} files - Build files to process */ |
46 | | - const files: string[] = getFilesToProcess(outDir, [ |
47 | | - 'cjs', |
48 | | - 'd.cts', |
49 | | - 'd.mts', |
50 | | - 'd.ts', |
51 | | - 'mjs' |
52 | | - ]) |
53 | | - |
54 | | - /** @const {Alias[]} aliases - Path alias objects */ |
55 | | - const aliases: Alias[] = computeAliases(rootDir, paths) |
56 | | - |
57 | | - /** |
58 | | - * Changes to apply to build files. |
59 | | - * |
60 | | - * @const {Change[]} changes |
61 | | - */ |
62 | | - const changes: Change[] = generateChanges(files, aliases, { |
63 | | - outPath: outDir, |
64 | | - srcPath: path.resolve('src') |
65 | | - }).map(({ file, text, ...rest }) => { |
66 | | - /** @const {string} ext - {@link file} extension */ |
67 | | - const ext: string = path.extname(file) |
68 | | - |
69 | | - /** |
70 | | - * Text changes applied to {@link file}. |
71 | | - * |
72 | | - * Each change in {@link rest.changes} will be updated to include the |
73 | | - * current file extension, {@link ext}. |
74 | | - * |
75 | | - * @const {TextChange[]} changes |
76 | | - */ |
77 | | - const changes: TextChange[] = (rest.changes ?? []).map(chg => ({ |
78 | | - ...chg, |
79 | | - modified: !/node_modules/.test(path.resolve(file, chg.modified)) |
80 | | - ? chg.modified + ext |
81 | | - : chg.modified |
82 | | - })) |
83 | | - |
84 | | - // make sure file extensions are included in path transformations |
85 | | - for (const { modified } of changes) { |
86 | | - text = text.replace(modified.replace(ext, ''), modified) |
87 | | - } |
88 | | - |
89 | | - return { changes, file, text } |
90 | | - }) |
91 | | - |
92 | | - return void applyChanges(changes) |
93 | | - } catch (e: unknown) { |
94 | | - console.error(e instanceof Error ? e.message : e) |
95 | | - } |
96 | | - }, |
97 | | - /** |
98 | | - * Updates `mkdist` build options. |
99 | | - * |
100 | | - * @see https://github.com/unjs/mkdist#-usage |
101 | | - * |
102 | | - * @param {BuildContext} _ - Build context |
103 | | - * @param {MkdistBuildEntry} __ - `mkdist` build entry |
104 | | - * @param {MkdistOptions} options - `mkdist` build options |
105 | | - * @return {void} Nothing when complete |
106 | | - */ |
107 | | - 'mkdist:entry:options'(_, __, options: MkdistOptions): void { |
108 | | - options.pattern = ['**', '!**/{__mocks__,__snapshots__,__tests__}/**'] |
109 | | - } |
110 | | - } |
| 21 | + sourcemap: 'external', |
| 22 | + sourcesContent: false, |
| 23 | + target: [ |
| 24 | + tsconfig.compilerOptions.target, |
| 25 | + 'node' + pkg.engines.node.replace(/^\D+/, '') |
| 26 | + ], |
| 27 | + treeShaking: true, |
| 28 | + tsconfig: 'tsconfig.build.json' |
111 | 29 | }) |
112 | 30 |
|
113 | 31 | export default config |
0 commit comments