From 637ec281ab2f777ce310442d4434f9cd792efc60 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Sun, 3 Apr 2022 17:18:31 +0000 Subject: [PATCH] fix: make sure `sources` are relative path in sourcemap, closes #603 --- src/esbuild/swc.ts | 18 ++++++++++++++++-- test/index.test.ts | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/esbuild/swc.ts b/src/esbuild/swc.ts index dd081159..c0ab2b20 100644 --- a/src/esbuild/swc.ts +++ b/src/esbuild/swc.ts @@ -3,6 +3,7 @@ */ import { JscConfig } from '@swc/core' import { Plugin } from 'esbuild' +import path from 'path' import { Logger } from '../log' import { localRequire } from '../utils' @@ -42,13 +43,26 @@ export const swcPlugin = ({ logger }: { logger: Logger }): Plugin => { const result = await swc.transformFile(args.path, { jsc, - sourceMaps: 'inline', + sourceMaps: true, configFile: false, swcrc: false, }) + let code = result.code + if (result.map) { + const map: { sources: string[] } = JSON.parse(result.map) + // Make sure sources are relative path + map.sources = map.sources.map((source) => { + return path.isAbsolute(source) + ? path.relative(path.dirname(args.path), source) + : source + }) + code += `//# sourceMappingURL=data:application/json;base64,${Buffer.from( + JSON.stringify(map) + ).toString('base64')}` + } return { - contents: result.code, + contents: code, } }) }, diff --git a/test/index.test.ts b/test/index.test.ts index 11e2c5cd..cc99ce53 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -819,3 +819,23 @@ test('native-node-module plugin should handle *.node(.js) import properly', asyn } ) }) + +test('proper sourcemap sources path when swc is enabled', async () => { + const { getFileContent } = await run( + getTestName(), + { + 'input.ts': `export const hi = 'hi'`, + 'tsconfig.json': JSON.stringify({ + compilerOptions: { + emitDecoratorMetadata: true, + }, + }), + }, + { + entry: ['input.ts'], + flags: ['--sourcemap'], + } + ) + const map = await getFileContent('dist/input.js.map') + expect(map).toContain(`["../input.ts"]`) +})