From bf48097bdf9bb6846d3c5a819ce97d8026bfcd1f Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Sat, 3 Aug 2024 13:30:04 +0900 Subject: [PATCH 1/2] add sourceRoot option --- schema.json | 3 +++ src/cli-main.ts | 4 ++++ src/esbuild/index.ts | 1 + src/options.ts | 1 + src/plugins/swc-target.ts | 1 + test/index.test.ts | 18 ++++++++++++++++++ 6 files changed, 28 insertions(+) diff --git a/schema.json b/schema.json index a033bf762..b90984b63 100644 --- a/schema.json +++ b/schema.json @@ -224,6 +224,9 @@ } ] }, + "sourceRoot": { + "type": "string" + }, "noExternal": { "type": "array", "items": { diff --git a/src/cli-main.ts b/src/cli-main.ts index 070236e1e..4841cce08 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -46,6 +46,10 @@ export async function main(options: Options = {}) { '--sourcemap [inline]', 'Generate external sourcemap, or inline source: --sourcemap inline', ) + .option( + '--sourceRoot [path]', + 'Specify the location where a debugger should locate TypeScript files instead of relative source locations', + ) .option( '--watch [path]', 'Watch mode, if path is not specified, it watches the current folder ".". Repeat "--watch" for more than one path', diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 9b07f24e9..6a58e9193 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -169,6 +169,7 @@ export async function runEsbuild( jsxFactory: options.jsxFactory, jsxFragment: options.jsxFragment, sourcemap: options.sourcemap ? 'external' : false, + sourceRoot: options.sourceRoot, target: options.target, banner, footer, diff --git a/src/options.ts b/src/options.ts index 68c8d3ff4..1db309910 100644 --- a/src/options.ts +++ b/src/options.ts @@ -137,6 +137,7 @@ export type Options = { dts?: boolean | string | DtsConfig experimentalDts?: boolean | string | ExperimentalDtsConfig sourcemap?: boolean | 'inline' + sourceRoot?: string /** Always bundle modules matching given patterns */ noExternal?: (string | RegExp)[] /** Don't bundle these modules */ diff --git a/src/plugins/swc-target.ts b/src/plugins/swc-target.ts index 7ec4b5c45..c0dee4356 100644 --- a/src/plugins/swc-target.ts +++ b/src/plugins/swc-target.ts @@ -38,6 +38,7 @@ export const swcTarget = (): Plugin => { const result = await swc.transform(code, { filename: info.path, sourceMaps: this.options.sourcemap, + sourceRoot: this.options.sourceRoot, minify: Boolean(this.options.minify), jsc: { target, diff --git a/test/index.test.ts b/test/index.test.ts index 5b07554fe..4d2d239c5 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -901,3 +901,21 @@ test('generate sourcemap with --treeshake', async () => { }), ) }) + +test('include sourceRoot in sourcemap with --sourceRoot', async () => { + const { getFileContent } = await run( + getTestName(), + { + 'input.ts': `export const hi = 'hi'`, + }, + { + entry: ['input.ts'], + flags: ['--sourcemap', '--sourceRoot=/'], + }, + ) + const sourceMap = await getFileContent(`dist/input.js.map`).then( + (rawContent) => JSON.parse(rawContent), + ) + + expect(sourceMap.sourceRoot).toBe('/') +}) From 5402ba0fe7f91bda809e8fb2972a93c072ebc45a Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Tue, 17 Sep 2024 15:13:17 +0900 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: ocavue --- schema.json | 3 ++- src/cli-main.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/schema.json b/schema.json index b90984b63..d7edb6989 100644 --- a/schema.json +++ b/schema.json @@ -225,7 +225,8 @@ ] }, "sourceRoot": { - "type": "string" + "type": "string", + "description": "The value of the `sourceRoot` field in the source map" }, "noExternal": { "type": "array", diff --git a/src/cli-main.ts b/src/cli-main.ts index 4841cce08..d3916dfdc 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -47,7 +47,7 @@ export async function main(options: Options = {}) { 'Generate external sourcemap, or inline source: --sourcemap inline', ) .option( - '--sourceRoot [path]', + '--source-root [path]', 'Specify the location where a debugger should locate TypeScript files instead of relative source locations', ) .option(