Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sourceRoot option #1174

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@
}
]
},
"sourceRoot": {
"type": "string",
"description": "The value of the `sourceRoot` field in the source map"
},
"noExternal": {
"type": "array",
"items": {
Expand Down
4 changes: 4 additions & 0 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export async function main(options: Options = {}) {
'--sourcemap [inline]',
'Generate external sourcemap, or inline source: --sourcemap inline',
)
.option(
'--source-root [path]',
'Specify the location where a debugger should locate TypeScript files instead of relative source locations',
)
.option(
'--watch [path]',
'Watch mode, if path is not specified, it watches the current folder ".". Repeat "--watch" for more than one path',
Expand Down
1 change: 1 addition & 0 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export async function runEsbuild(
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
sourcemap: options.sourcemap ? 'external' : false,
sourceRoot: options.sourceRoot,
target: options.target,
banner,
footer,
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type Options = {
dts?: boolean | string | DtsConfig
experimentalDts?: boolean | string | ExperimentalDtsConfig
sourcemap?: boolean | 'inline'
sourceRoot?: string
/** Always bundle modules matching given patterns */
noExternal?: (string | RegExp)[]
/** Don't bundle these modules */
Expand Down
1 change: 1 addition & 0 deletions src/plugins/swc-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const swcTarget = (): Plugin => {
const result = await swc.transform(code, {
filename: info.path,
sourceMaps: this.options.sourcemap,
sourceRoot: this.options.sourceRoot,
minify: Boolean(this.options.minify),
jsc: {
target,
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,3 +901,21 @@ test('generate sourcemap with --treeshake', async () => {
}),
)
})

test('include sourceRoot in sourcemap with --sourceRoot', async () => {
const { getFileContent } = await run(
getTestName(),
{
'input.ts': `export const hi = 'hi'`,
},
{
entry: ['input.ts'],
flags: ['--sourcemap', '--sourceRoot=/'],
},
)
const sourceMap = await getFileContent(`dist/input.js.map`).then(
(rawContent) => JSON.parse(rawContent),
)

expect(sourceMap.sourceRoot).toBe('/')
})