Skip to content
Permalink
Browse files
fix: make sure sources are relative path in sourcemap, closes #603
  • Loading branch information
egoist committed Apr 3, 2022
1 parent 3888651 commit 637ec281ab2f777ce310442d4434f9cd792efc60
Showing 2 changed files with 36 additions and 2 deletions.
@@ -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,
}
})
},
@@ -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"]`)
})

1 comment on commit 637ec28

@vercel
Copy link

@vercel vercel bot commented on 637ec28 Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.