diff --git a/src/cli-main.ts b/src/cli-main.ts index 58f9dad09..eb0394328 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 aec991d05..2c0a0112f 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 0921abdae..d848eeec1 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 757fc759f..a946ad6be 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 71c946a21..a711502c6 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(),