55 */
66
77import { defineBuildConfig , type Config } from '@flex-development/mkbuild'
8+ import pathe from '@flex-development/pathe'
9+ import type { BuildResult , PluginBuild } from 'esbuild'
810import pkg from './package.json' assert { type : 'json' }
911import tsconfig from './tsconfig.build.json' assert { type : 'json' }
1012
@@ -15,8 +17,77 @@ import tsconfig from './tsconfig.build.json' assert { type: 'json' }
1517 */
1618const config : Config = defineBuildConfig ( {
1719 charset : 'utf8' ,
18- sourcemap : true ,
19- sourcesContent : false ,
20+ entries : [
21+ { dts : 'only' } ,
22+ { dts : false , pattern : [ 'internal/*' ] } ,
23+ {
24+ dts : false ,
25+ pattern : [ 'index.ts' , 'utils/*' ] ,
26+ sourceRoot : pathe . join (
27+ pkg . repository . replace ( / \. g i t $ / , pathe . sep + 'blob' ) ,
28+ pkg . tagPrefix + pkg . version
29+ ) ,
30+ sourcemap : true ,
31+ sourcesContent : false
32+ }
33+ ] ,
34+ minifySyntax : true ,
35+ plugins : [
36+ {
37+ name : 'fix-sourcemaps' ,
38+
39+ /**
40+ * Makes sourcemap files relative to [`sourceRoot`][1].
41+ *
42+ * [1]: https://esbuild.github.io/api/#source-root
43+ * [2]: https://esbuild.github.io/plugins
44+ *
45+ * @see https://github.com/evanw/esbuild/issues/2218
46+ *
47+ * @param {PluginBuild } build - [esbuild plugin api][2]
48+ * @param {PluginBuild['onEnd'] } build.onEnd - Build end callback
49+ * @return {void } Nothing when complete
50+ */
51+ setup ( { initialOptions, onEnd } : PluginBuild ) : void {
52+ return void onEnd ( ( result : BuildResult < { write : false } > ) : void => {
53+ const { absWorkingDir = process . cwd ( ) } = initialOptions
54+
55+ return void ( result . outputFiles = result . outputFiles . map ( output => {
56+ if ( output . path . endsWith ( '.map' ) ) {
57+ /**
58+ * Relative path to output file sourcemap is for.
59+ *
60+ * **Note**: Relative to {@linkcode absWorkingDir}.
61+ *
62+ * @const {string} outfile
63+ */
64+ const outfile : string = output . path
65+ . replace ( absWorkingDir , '' )
66+ . replace ( / ^ \/ / , '' )
67+ . replace ( / \. m a p $ / , '' )
68+
69+ /**
70+ * Parsed sourcemap object.
71+ *
72+ * @const {{ sources: string[] }}
73+ */
74+ const map : { sources : string [ ] } = JSON . parse ( output . text )
75+
76+ // reset sources to outfile entry point
77+ map . sources = [ result . metafile ! . outputs [ outfile ] ! . entryPoint ! ]
78+
79+ // redefine outfile text
80+ Object . defineProperty ( output , 'text' , {
81+ get : ( ) : string => JSON . stringify ( map , null , 2 )
82+ } )
83+ }
84+
85+ return output
86+ } ) )
87+ } )
88+ }
89+ }
90+ ] ,
2091 target : [
2192 pkg . engines . node . replace ( / ^ \D + / , 'node' ) ,
2293 tsconfig . compilerOptions . target
0 commit comments