Skip to content

Commit

Permalink
allow "linked" source map api option (#1722)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 2, 2022
1 parent 50dc6ae commit b6f826e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Drop = 'console' | 'debugger';

interface CommonOptions {
/** Documentation: https://esbuild.github.io/api/#sourcemap */
sourcemap?: boolean | 'inline' | 'external' | 'both';
sourcemap?: boolean | 'linked' | 'inline' | 'external' | 'both';
/** Documentation: https://esbuild.github.io/api/#legal-comments */
legalComments?: 'none' | 'inline' | 'eof' | 'linked' | 'external';
/** Documentation: https://esbuild.github.io/api/#source-root */
Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ func parseOptionsImpl(
value := arg[len("--sourcemap="):]
var sourcemap api.SourceMap
switch value {
case "linked":
sourcemap = api.SourceMapLinked
case "inline":
sourcemap = api.SourceMapInline
case "external":
Expand Down Expand Up @@ -865,7 +867,7 @@ func parseOptionsForRun(osArgs []string) (*api.BuildOptions, *api.TransformOptio
}
return nil, nil, parseOptionsExtras{}, cli_helpers.MakeErrorWithNote(
fmt.Sprintf("Use \"--sourcemap\" instead of \"--sourcemap=%s\" when transforming stdin", sourceMapMode),
fmt.Sprintf("Using esbuild to transform stdin only generates one output file, so you cannot use the %q source map mode "+
fmt.Sprintf("Using esbuild to transform stdin only generates one output file. You cannot use the %q source map mode "+
"since that needs to generate two output files.", sourceMapMode),
)
}
Expand Down
35 changes: 33 additions & 2 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ let buildTests = {
assert.strictEqual(notcss, 'body {\n}\n')
},

async sourceMap({ esbuild, testDir }) {
async sourceMapTrue({ esbuild, testDir }) {
const input = path.join(testDir, 'in.js')
const output = path.join(testDir, 'out.js')
const content = 'exports.foo = 123'
Expand All @@ -296,6 +296,28 @@ let buildTests = {
assert.strictEqual(json.sourcesContent[0], content)
},

async sourceMapLinked({ esbuild, testDir }) {
const input = path.join(testDir, 'in.js')
const output = path.join(testDir, 'out.js')
const content = 'exports.foo = 123'
await writeFileAsync(input, content)
await esbuild.build({
entryPoints: [input],
outfile: output,
sourcemap: 'linked',
})
const result = require(output)
assert.strictEqual(result.foo, 123)
const outputFile = await readFileAsync(output, 'utf8')
const match = /\/\/# sourceMappingURL=(.*)/.exec(outputFile)
assert.strictEqual(match[1], 'out.js.map')
const resultMap = await readFileAsync(output + '.map', 'utf8')
const json = JSON.parse(resultMap)
assert.strictEqual(json.version, 3)
assert.strictEqual(json.sources[0], path.basename(input))
assert.strictEqual(json.sourcesContent[0], content)
},

async sourceMapExternal({ esbuild, testDir }) {
const input = path.join(testDir, 'in.js')
const output = path.join(testDir, 'out.js')
Expand Down Expand Up @@ -3875,12 +3897,21 @@ let transformTests = {
assert.strictEqual(code, `module.exports = "data:application/octet-stream;base64,AAEC";\n`)
},

async sourceMapWithName({ esbuild }) {
async sourceMapTrueWithName({ esbuild }) {
const { code, map } = await esbuild.transform(`let x`, { sourcemap: true, sourcefile: 'afile.js' })
assert.strictEqual(code, `let x;\n`)
await assertSourceMap(map, 'afile.js')
},

async sourceMapLinkedWithName({ esbuild }) {
try {
await esbuild.transform(`let x`, { sourcemap: 'linked', sourcefile: 'afile.js' })
throw new Error('Expected a transform failure')
} catch (e) {
assert.strictEqual(e + '', `Error: Transform failed with 1 error:\nerror: Cannot transform with linked source maps`)
}
},

async sourceMapExternalWithName({ esbuild }) {
const { code, map } = await esbuild.transform(`let x`, { sourcemap: 'external', sourcefile: 'afile.js' })
assert.strictEqual(code, `let x;\n`)
Expand Down

0 comments on commit b6f826e

Please sign in to comment.