Skip to content

Commit

Permalink
build: ensure resolved source map url is absolute
Browse files Browse the repository at this point in the history
- https://sourcemaps.info/spec.html

Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Aug 8, 2023
1 parent 3edba27 commit da58477
Showing 1 changed file with 4 additions and 65 deletions.
69 changes: 4 additions & 65 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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 @@ -19,75 +18,15 @@ const config: Config = defineBuildConfig({
charset: 'utf8',
entries: [
{ dts: 'only' },
{ dts: false, pattern: ['**/index.ts', 'internal/*'] },
{ dts: false, pattern: ['interfaces/index.ts'] },
{
dts: false,
pattern: ['index.ts', 'utils/*'],
sourceRoot: pathe.join(
pkg.repository.replace(/\.git$/, pathe.sep + 'blob'),
pkg.tagPrefix + pkg.version
),
sourcemap: true,
sourcesContent: false
pattern: ['index.ts', 'internal/*', 'utils/*'],
sourceRoot: 'file' + pathe.delimiter + pathe.sep.repeat(2),
sourcemap: true
}
],
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

0 comments on commit da58477

Please sign in to comment.