From a652e98514b0204222b6300e8c1c3f3952a830b3 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Mon, 7 Aug 2023 20:15:51 -0400 Subject: [PATCH] build: distribute source maps for `utils` only Signed-off-by: Lexus Drumgold --- build.config.ts | 75 +++++++++++++++++++++++++++++++++++++++++++++++-- package.json | 3 +- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/build.config.ts b/build.config.ts index 75840313..139167df 100644 --- a/build.config.ts +++ b/build.config.ts @@ -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' } @@ -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 diff --git a/package.json b/package.json index 032fe928..0476d3d1 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "type": "module", "files": [ "CHANGELOG.md", - "dist", - "src" + "dist" ], "exports": { ".": "./dist/index.mjs",