Skip to content

Commit

Permalink
build: distribute source maps for utils only
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Aug 8, 2023
1 parent 1856eaf commit a652e98
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
75 changes: 73 additions & 2 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
import pathe from '@flex-development/pathe'
import type { BuildResult, PluginBuild } from 'esbuild'
import pkg from './package.json' assert { type: 'json' }
import tsconfig from './tsconfig.build.json' assert { type: 'json' }

Expand All @@ -15,8 +17,77 @@ import tsconfig from './tsconfig.build.json' assert { type: 'json' }
*/
const config: Config = defineBuildConfig({
charset: 'utf8',
sourcemap: true,
sourcesContent: false,
entries: [
{ dts: 'only' },
{ dts: false, pattern: ['internal/*'] },
{
dts: false,
pattern: ['index.ts', 'utils/*'],
sourceRoot: pathe.join(
pkg.repository.replace(/\.git$/, pathe.sep + 'blob'),
pkg.tagPrefix + pkg.version
),
sourcemap: true,
sourcesContent: false
}
],
minifySyntax: true,
plugins: [
{
name: 'fix-sourcemaps',

/**
* Makes sourcemap files relative to [`sourceRoot`][1].
*
* [1]: https://esbuild.github.io/api/#source-root
* [2]: https://esbuild.github.io/plugins
*
* @see https://github.com/evanw/esbuild/issues/2218
*
* @param {PluginBuild} build - [esbuild plugin api][2]
* @param {PluginBuild['onEnd']} build.onEnd - Build end callback
* @return {void} Nothing when complete
*/
setup({ initialOptions, onEnd }: PluginBuild): void {
return void onEnd((result: BuildResult<{ write: false }>): void => {
const { absWorkingDir = process.cwd() } = initialOptions

return void (result.outputFiles = result.outputFiles.map(output => {
if (output.path.endsWith('.map')) {
/**
* Relative path to output file sourcemap is for.
*
* **Note**: Relative to {@linkcode absWorkingDir}.
*
* @const {string} outfile
*/
const outfile: string = output.path
.replace(absWorkingDir, '')
.replace(/^\//, '')
.replace(/\.map$/, '')

/**
* Parsed sourcemap object.
*
* @const {{ sources: string[] }}
*/
const map: { sources: string[] } = JSON.parse(output.text)

// reset sources to outfile entry point
map.sources = [result.metafile!.outputs[outfile]!.entryPoint!]

// redefine outfile text
Object.defineProperty(output, 'text', {
get: (): string => JSON.stringify(map, null, 2)
})
}

return output
}))
})
}
}
],
target: [
pkg.engines.node.replace(/^\D+/, 'node'),
tsconfig.compilerOptions.target
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"type": "module",
"files": [
"CHANGELOG.md",
"dist",
"src"
"dist"
],
"exports": {
".": "./dist/index.mjs",
Expand Down

0 comments on commit a652e98

Please sign in to comment.