fix: make sure sources are relative path in sourcemap, closes #603

This commit is contained in:
EGOIST 2022-04-03 17:18:31 +00:00 committed by GitHub
parent 38886519bd
commit 637ec281ab
2 changed files with 36 additions and 2 deletions

View File

@ -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,
}
})
},

View File

@ -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"]`)
})