|
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 { resolveId } from 'externality' |
8 | | -import type { MkdistOptions } from 'mkdist' |
9 | | -import { findStaticImports } from 'mlly' |
10 | | -import fs from 'node:fs' |
11 | | -import path from 'node:path' |
12 | | -import { applyChanges } from 'resolve-tspaths/dist/steps/applyChanges' |
13 | | -import { computeAliases } from 'resolve-tspaths/dist/steps/computeAliases' |
14 | | -import { generateChanges } from 'resolve-tspaths/dist/steps/generateChanges' |
15 | | -import { loadTSConfig } from 'resolve-tspaths/dist/steps/loadTSConfig' |
16 | | -import type { Alias, Change } from 'resolve-tspaths/dist/types' |
17 | | -import { |
18 | | - defineBuildConfig, |
19 | | - type BuildConfig, |
20 | | - type BuildContext, |
21 | | - type BuildOptions, |
22 | | - type MkdistBuildEntry |
23 | | -} 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' } |
24 | 10 |
|
25 | 11 | /** |
26 | | - * Build options. |
| 12 | + * Build configuration options. |
27 | 13 | * |
28 | | - * @const {BuildConfig} |
| 14 | + * @const {Config} config |
29 | 15 | */ |
30 | | -const config: BuildConfig = defineBuildConfig({ |
31 | | - declaration: true, |
| 16 | +const config: Config = defineBuildConfig({ |
32 | 17 | entries: [ |
33 | | - { builder: 'mkdist', ext: 'cjs', format: 'cjs', input: 'src/' }, |
34 | | - { builder: 'mkdist', ext: 'mjs', format: 'esm', input: 'src/' } |
| 18 | + { ext: '.mjs', format: 'esm' }, |
| 19 | + { ext: '.cjs', format: 'cjs' } |
35 | 20 | ], |
36 | | - hooks: { |
37 | | - /** |
38 | | - * Transforms path aliases and bare specifiers in `output.writtenFiles`. |
39 | | - * |
40 | | - * @param {BuildContext} ctx - Build context |
41 | | - * @param {BuildOptions} ctx.options - Build options |
42 | | - * @param {MkdistBuildEntry} entry - `mkdist` build entry |
43 | | - * @param {{ writtenFiles: string[] }} results - Build results |
44 | | - * @param {string[]} results.writtenFiles - Files created during build |
45 | | - * @return {Promise<void>} Nothing when complete |
46 | | - */ |
47 | | - async 'mkdist:entry:build'( |
48 | | - ctx: BuildContext, |
49 | | - entry: MkdistBuildEntry, |
50 | | - results: { writtenFiles: string[] } |
51 | | - ): Promise<void> { |
52 | | - const { outDir, rootDir } = ctx.options |
53 | | - const { writtenFiles } = results |
54 | | - |
55 | | - // transform path aliases |
56 | | - try { |
57 | | - const { paths = {} } = loadTSConfig(`${rootDir}/tsconfig.json`).options |
58 | | - |
59 | | - /** |
60 | | - * Path alias objects. |
61 | | - * |
62 | | - * @const {Alias[]} aliases |
63 | | - */ |
64 | | - const aliases: Alias[] = computeAliases(rootDir, paths) |
65 | | - |
66 | | - /** |
67 | | - * Changes to apply to {@link writtenFiles}. |
68 | | - * |
69 | | - * @const {Change[]} changes |
70 | | - */ |
71 | | - const changes: Change[] = generateChanges(writtenFiles, aliases, { |
72 | | - outPath: outDir, |
73 | | - srcPath: path.resolve('src') |
74 | | - }) |
75 | | - |
76 | | - applyChanges(changes) |
77 | | - } catch (e: unknown) { |
78 | | - console.error(e instanceof Error ? e.message : e) |
79 | | - } |
80 | | - |
81 | | - // include file extensions in import specifiers |
82 | | - // https://nodejs.org/docs/latest-v16.x/api/esm.html#import-specifiers |
83 | | - for (const file of writtenFiles) { |
84 | | - try { |
85 | | - /** |
86 | | - * {@link file} content. |
87 | | - * |
88 | | - * @var {string} content |
89 | | - */ |
90 | | - let content: string = await fs.promises.readFile(file, 'utf8') |
91 | | - |
92 | | - for (const { code, specifier } of findStaticImports(content)) { |
93 | | - if (path.extname(specifier)) continue |
94 | | - |
95 | | - if (/^(\w|@)/.test(specifier)) { |
96 | | - const { path: id } = await resolveId(specifier, process.cwd(), { |
97 | | - type: 'module' |
98 | | - }) |
99 | | - |
100 | | - content = content.replace( |
101 | | - code, |
102 | | - code.replace(specifier, id.split('node_modules/')[1]!) |
103 | | - ) |
104 | | - } else { |
105 | | - content = content.replace( |
106 | | - code, |
107 | | - code.replace(specifier, specifier + '.' + entry.ext!) |
108 | | - ) |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - await fs.promises.writeFile(file, content) |
113 | | - } catch (e: unknown) { |
114 | | - console.error(e instanceof Error ? e.message : e) |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - return void 0 |
119 | | - }, |
120 | | - /** |
121 | | - * Updates `mkdist` build options. |
122 | | - * |
123 | | - * @see https://github.com/unjs/mkdist#-usage |
124 | | - * |
125 | | - * @param {BuildContext} ctx - Build context |
126 | | - * @param {MkdistBuildEntry} entry - `mkdist` build entry |
127 | | - * @param {MkdistOptions} options - `mkdist` build options |
128 | | - * @return {void} Nothing when complete |
129 | | - */ |
130 | | - 'mkdist:entry:options'( |
131 | | - ctx: BuildContext, |
132 | | - entry: MkdistBuildEntry, |
133 | | - options: MkdistOptions |
134 | | - ): void { |
135 | | - options.pattern = ['**', '!**/{__mocks__,__snapshots__,__tests__}/**'] |
136 | | - } |
137 | | - } |
| 21 | + minify: true, |
| 22 | + sourcemap: 'external', |
| 23 | + sourcesContent: false, |
| 24 | + target: [ |
| 25 | + tsconfig.compilerOptions.target, |
| 26 | + 'node' + pkg.engines.node.replace(/^\D+/, '') |
| 27 | + ], |
| 28 | + treeShaking: true, |
| 29 | + tsconfig: 'tsconfig.build.json' |
138 | 30 | }) |
139 | 31 |
|
140 | 32 | export default config |
0 commit comments