Skip to content

Commit 9389340

Browse files
committed
build: distribute sourcemaps for utils only
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 1856eaf commit 9389340

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

build.config.ts

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
8+
import pathe from '@flex-development/pathe'
9+
import type { BuildResult, PluginBuild } from 'esbuild'
810
import pkg from './package.json' assert { type: 'json' }
911
import tsconfig from './tsconfig.build.json' assert { type: 'json' }
1012

@@ -15,8 +17,77 @@ import tsconfig from './tsconfig.build.json' assert { type: 'json' }
1517
*/
1618
const 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(/\.git$/, 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(/\.map$/, '')
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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"type": "module",
2828
"files": [
2929
"CHANGELOG.md",
30-
"dist",
31-
"src"
30+
"dist"
3231
],
3332
"exports": {
3433
".": "./dist/index.mjs",

0 commit comments

Comments
 (0)