44 * @see https://github.com/flex-development/mkbuild
55 */
66
7- import { defineBuildConfig , type Config } from '@flex-development/mkbuild'
7+ import {
8+ defineBuildConfig ,
9+ type Config ,
10+ type OutputMetadata
11+ } from '@flex-development/mkbuild'
12+ import pathe from '@flex-development/pathe'
13+ import type { BuildResult , OutputFile , PluginBuild } from 'esbuild'
814import pkg from './package.json' assert { type : 'json' }
915import tsconfig from './tsconfig.build.json' assert { type : 'json' }
1016
@@ -15,9 +21,96 @@ import tsconfig from './tsconfig.build.json' assert { type: 'json' }
1521 */
1622const config : Config = defineBuildConfig ( {
1723 charset : 'utf8' ,
18- entries : [ { format : 'esm' } , { format : 'cjs' } ] ,
19- sourcemap : true ,
20- sourcesContent : false ,
24+ entries : [
25+ { dts : 'only' } ,
26+ { dts : false , pattern : [ '**/index.ts' , 'enums/*' ] } ,
27+ {
28+ dts : false ,
29+ pattern : [ '!utils/*.options.ts' , 'index.ts' , 'utils/*' ] ,
30+ sourceRoot : pathe . join ( pkg . homepage , 'tree' , pkg . tagPrefix + pkg . version ) ,
31+ sourcemap : true ,
32+ sourcesContent : true
33+ }
34+ ] ,
35+ minifySyntax : true ,
36+ plugins : [
37+ {
38+ /**
39+ * Plugin name.
40+ *
41+ * @const {string} name
42+ */
43+ name : 'fix-sourcemaps' ,
44+
45+ /**
46+ * Makes sourcemap files relative to [`sourceRoot`][1].
47+ *
48+ * [1]: https://esbuild.github.io/api/#source-root
49+ * [2]: https://esbuild.github.io/plugins
50+ *
51+ * @see https://github.com/evanw/esbuild/issues/2218
52+ *
53+ * @param {PluginBuild } build - [esbuild plugin api][2]
54+ * @param {PluginBuild['onEnd'] } build.onEnd - Build end callback
55+ * @return {void } Nothing when complete
56+ */
57+ setup ( { initialOptions, onEnd } : PluginBuild ) : void {
58+ const { absWorkingDir = process . cwd ( ) } = initialOptions
59+
60+ return void onEnd ( ( result : BuildResult ) : void => {
61+ /**
62+ * Output file objects.
63+ *
64+ * @const {OutputFile[]} outputFiles
65+ */
66+ const outputFiles : OutputFile [ ] = [ ]
67+
68+ for ( const output of result . outputFiles ! ) {
69+ if ( output . path . endsWith ( '.map' ) ) {
70+ /**
71+ * Relative path to output file sourcemap is for.
72+ *
73+ * **Note**: Relative to {@linkcode absWorkingDir}.
74+ *
75+ * @const {string} outfile
76+ */
77+ const outfile : string = output . path
78+ . replace ( absWorkingDir , '' )
79+ . replace ( / ^ \/ / , '' )
80+ . replace ( / \. m a p $ / , '' )
81+
82+ /**
83+ * Output metadata for {@linkcode outfile}.
84+ *
85+ * @const {OutputMetadata} metadata
86+ */
87+ const metadata : OutputMetadata =
88+ result . metafile ! . outputs [ outfile ] !
89+
90+ /**
91+ * Parsed sourcemap object.
92+ *
93+ * @const {{ sources: string[] }}
94+ */
95+ const map : { sources : string [ ] } = JSON . parse ( output . text )
96+
97+ // reset sources to outfile entry point
98+ map . sources = [ metadata . entryPoint ! ]
99+
100+ // redefine outfile text
101+ Object . defineProperty ( output , 'text' , {
102+ get : ( ) : string => JSON . stringify ( map , null , 2 )
103+ } )
104+ }
105+
106+ outputFiles . push ( output )
107+ }
108+
109+ return void ( result . outputFiles = outputFiles )
110+ } )
111+ }
112+ }
113+ ] ,
21114 target : [
22115 pkg . engines . node . replace ( / ^ \D + / , 'node' ) ,
23116 tsconfig . compilerOptions . target
0 commit comments