From f137ed7b3b280d9bf7ce2f7a8e511e6e4ecb80a3 Mon Sep 17 00:00:00 2001 From: Joseph Carroll Date: Mon, 13 May 2024 08:54:14 -0700 Subject: [PATCH] fix: SourceMap Not Linked When Enabled #1120 --- src/cli-main.ts | 4 +-- src/esbuild/index.ts | 7 ++++- src/options.ts | 2 +- src/plugins/swc-target.ts | 5 +++- test/index.test.ts | 59 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index 58f9dad0..eb039432 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -43,8 +43,8 @@ export async function main(options: Options = {}) { 'Generate declaration file (experimental)' ) .option( - '--sourcemap [inline]', - 'Generate external sourcemap, or inline source: --sourcemap inline' + '--sourcemap [type]', + 'Generate a sourcemap, defaults to `external` or use a valid esbuild sourcemap type: `--sourcemap linked | inline | external | both`' ) .option( '--watch [path]', diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index aec991d0..2c0a0112 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -149,6 +149,11 @@ export async function runEsbuild( ...(options.esbuildPlugins || []), ] + const sourcemap = options.sourcemap === true + ? "external" + : typeof options.sourcemap === 'string' + ? options.sourcemap + : false; const banner = typeof options.banner === 'function' ? options.banner({ format }) @@ -168,7 +173,7 @@ export async function runEsbuild( globalName: options.globalName, jsxFactory: options.jsxFactory, jsxFragment: options.jsxFragment, - sourcemap: options.sourcemap ? 'external' : false, + sourcemap, target: options.target, banner, footer, diff --git a/src/options.ts b/src/options.ts index 0921abda..d848eeec 100644 --- a/src/options.ts +++ b/src/options.ts @@ -136,7 +136,7 @@ export type Options = { } dts?: boolean | string | DtsConfig experimentalDts?: boolean | string | ExperimentalDtsConfig - sourcemap?: boolean | 'inline' + sourcemap?: boolean | 'linked' | 'inline' | 'external' | 'both' /** 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 757fc759..a946ad6b 100644 --- a/src/plugins/swc-target.ts +++ b/src/plugins/swc-target.ts @@ -34,10 +34,13 @@ export const swcTarget = (): Plugin => { `@swc/core is required for ${target} target. Please install it with \`npm install @swc/core -D\`` ) } + const sourceMaps = typeof this.options.sourcemap === 'string' + ? this.options.sourcemap === 'inline' ? 'inline' : true + : this.options.sourcemap; const result = await swc.transform(code, { filename: info.path, - sourceMaps: this.options.sourcemap, + sourceMaps, minify: Boolean(this.options.minify), jsc: { target, diff --git a/test/index.test.ts b/test/index.test.ts index 71c946a2..a711502c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -198,8 +198,19 @@ test('enable --dts-resolve for specific module', async () => { expect(content).toMatchSnapshot() }) +test('bundle graphql-tools without sourcemaps', async () => { + const { output, outFiles } = await run( + getTestName(), + { + 'input.ts': `export { makeExecutableSchema } from 'graphql-tools'`, + } + ) + expect(output).not.toContain('//# sourceMappingURL') + expect(outFiles).toEqual(['input.js']) +}) + test('bundle graphql-tools with --sourcemap flag', async () => { - const { outFiles } = await run( + const { output, outFiles } = await run( getTestName(), { 'input.ts': `export { makeExecutableSchema } from 'graphql-tools'`, @@ -208,6 +219,22 @@ test('bundle graphql-tools with --sourcemap flag', async () => { flags: ['--sourcemap'], } ) + expect(output).toContain('//# sourceMappingURL=input.js.map') + expect(outFiles).toEqual(['input.js', 'input.js.map']) +}) + +test('bundle graphql-tools with --sourcemap linked flag', async () => { + const { output, outFiles } = await run( + getTestName(), + { + 'input.ts': `export { makeExecutableSchema } from 'graphql-tools'`, + }, + { + flags: ['--sourcemap', 'linked'], + } + ) + + expect(output).toContain('//# sourceMappingURL=input.js.map') expect(outFiles).toEqual(['input.js', 'input.js.map']) }) @@ -226,6 +253,36 @@ test('bundle graphql-tools with --sourcemap inline flag', async () => { expect(outFiles).toEqual(['input.js']) }) +test('bundle graphql-tools with --sourcemap external flag', async () => { + const { output, outFiles } = await run( + getTestName(), + { + 'input.ts': `export { makeExecutableSchema } from 'graphql-tools'`, + }, + { + flags: ['--sourcemap', 'external'], + } + ) + + expect(output).toContain('//# sourceMappingURL=input.js.map') + expect(outFiles).toEqual(['input.js', 'input.js.map']) +}) + +test('bundle graphql-tools with --sourcemap both flag', async () => { + const { output, outFiles } = await run( + getTestName(), + { + 'input.ts': `export { makeExecutableSchema } from 'graphql-tools'`, + }, + { + flags: ['--sourcemap', 'both'], + } + ) + + expect(output).toContain('//# sourceMappingURL=data:application/json;base64') + expect(outFiles).toEqual(['input.js', 'input.js.map']) +}) + test('multiple formats', async () => { const { output, outFiles } = await run( getTestName(),