Skip to content
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
2 changes: 2 additions & 0 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ var helpText = func(colors logger.Colors) string {
--sourcemap=external Do not link to the source map with a comment
--sourcemap=inline Emit the source map with an inline data URL
--sources-content=false Omit "sourcesContent" in generated source maps
--exclude-sourcemap=... Exclude all files matching a regular expression from
getting included in the source map
--supported:F=... Consider syntax F to be supported (true | false)
--tree-shaking=... Force tree shaking on or off (false | true)
--tsconfig=... Use this tsconfig.json file instead of other ones
Expand Down
16 changes: 12 additions & 4 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,18 @@ func parseFile(args parseArgs) {
source.Contents = ""
}

var omitSourceMap bool = false
if args.options.ExcludeSourceMap != nil {
omitSourceMap = args.options.ExcludeSourceMap.MatchString(args.prettyPath)
}

result := parseResult{
file: scannerFile{
inputFile: graph.InputFile{
Source: source,
Loader: loader,
SideEffects: args.sideEffects,
OmitFromSourceMaps: omitSourceMap,
},
pluginData: pluginData,
},
Expand Down Expand Up @@ -593,7 +599,7 @@ func parseFile(args parseArgs) {
}

// Attempt to parse the source map if present
if loader.CanHaveSourceMap() && args.options.SourceMap != config.SourceMapNone {
if loader.CanHaveSourceMap() && args.options.SourceMap != config.SourceMapNone && !omitSourceMap {
var sourceMapComment logger.Span
switch repr := result.file.inputFile.Repr.(type) {
case *graph.JSRepr:
Expand Down Expand Up @@ -1350,7 +1356,8 @@ func ScanBundle(
Repr: &graph.JSRepr{
AST: ast,
},
OmitFromSourceMapsAndMetafile: true,
OmitFromMetafile: true,
OmitFromSourceMaps: true,
},
},
ok: ok,
Expand Down Expand Up @@ -2250,7 +2257,8 @@ func (s *scanner) generateResultForGlobResolve(
Repr: &graph.JSRepr{
AST: ast,
},
OmitFromSourceMapsAndMetafile: true,
OmitFromMetafile: true,
OmitFromSourceMaps: true,
},
},
ok: true,
Expand Down Expand Up @@ -3156,7 +3164,7 @@ func (b *Bundle) generateMetadataJSON(results []graph.OutputFile, allReachableFi
// Write inputs
isFirst := true
for _, sourceIndex := range allReachableFiles {
if b.files[sourceIndex].inputFile.OmitFromSourceMapsAndMetafile {
if b.files[sourceIndex].inputFile.OmitFromMetafile {
continue
}
if file := &b.files[sourceIndex]; len(file.jsonMetadataChunk) > 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ type Options struct {
NeedsMetafile bool
SourceMap SourceMap
ExcludeSourcesContent bool
ExcludeSourceMap *regexp.Regexp
}

type TSImportsNotUsedAsValues uint8
Expand Down
3 changes: 2 additions & 1 deletion internal/graph/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type InputFile struct {
Source logger.Source
Loader config.Loader

OmitFromSourceMapsAndMetafile bool
OmitFromMetafile bool
OmitFromSourceMaps bool
}

type OutputFile struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5849,7 +5849,7 @@ func (c *linkerContext) generateChunkJS(chunkIndex int, chunkWaitGroup *sync.Wai
}

// Don't include the runtime in source maps
if c.graph.Files[compileResult.sourceIndex].InputFile.OmitFromSourceMapsAndMetafile {
if c.graph.Files[compileResult.sourceIndex].InputFile.OmitFromSourceMaps {
prevOffset.AdvanceString(string(compileResult.JS))
j.AddBytes(compileResult.JS)
} else {
Expand Down
2 changes: 2 additions & 0 deletions lib/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ function flagsForBuildOptions(
let write = getFlag(options, keys, 'write', mustBeBoolean) ?? writeDefault; // Default to true if not specified
let allowOverwrite = getFlag(options, keys, 'allowOverwrite', mustBeBoolean)
let mangleCache = getFlag(options, keys, 'mangleCache', mustBeObject)
let excludeSourceMap = getFlag(options, keys, 'excludeSourceMap', mustBeRegExp)
keys.plugins = true; // "plugins" has already been read earlier
checkForInvalidFlags(options, keys, `in ${callName}() call`)

Expand All @@ -305,6 +306,7 @@ function flagsForBuildOptions(
if (assetNames) flags.push(`--asset-names=${assetNames}`)
if (mainFields) flags.push(`--main-fields=${validateAndJoinStringArray(mainFields, 'main field')}`)
if (conditions) flags.push(`--conditions=${validateAndJoinStringArray(conditions, 'condition')}`)
if (excludeSourceMap) flags.push(`--exclude-sourcemap=${jsRegExpToGoRegExp(excludeSourceMap)}`)
if (external) for (let name of external) flags.push(`--external:${validateStringValue(name, 'external')}`)
if (alias) {
for (let old in alias) {
Expand Down
2 changes: 2 additions & 0 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export interface BuildOptions extends CommonOptions {
absWorkingDir?: string
/** Documentation: https://esbuild.github.io/api/#node-paths */
nodePaths?: string[]; // The "NODE_PATH" variable from Node.js
/** Documentation: TODO */
excludeSourceMap?: RegExp
}

export interface StdinOptions {
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ type BuildOptions struct {
Sourcemap SourceMap // Documentation: https://esbuild.github.io/api/#sourcemap
SourceRoot string // Documentation: https://esbuild.github.io/api/#source-root
SourcesContent SourcesContent // Documentation: https://esbuild.github.io/api/#sources-content
ExcludeSourceMap string // Documentation: TODO

Target Target // Documentation: https://esbuild.github.io/api/#target
Engines []Engine // Documentation: https://esbuild.github.io/api/#target
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ func validateBuildOptions(
LegalComments: validateLegalComments(buildOpts.LegalComments, buildOpts.Bundle),
SourceRoot: buildOpts.SourceRoot,
ExcludeSourcesContent: buildOpts.SourcesContent == SourcesContentExclude,
ExcludeSourceMap: validateRegex(log, "exclude source map", buildOpts.ExcludeSourceMap),
MinifySyntax: buildOpts.MinifySyntax,
MinifyWhitespace: buildOpts.MinifyWhitespace,
MinifyIdentifiers: buildOpts.MinifyIdentifiers,
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ func parseOptionsImpl(
}
}

case strings.HasPrefix(arg, "--exclude-sourcemap=") && buildOpts != nil:
value := arg[len("--exclude-sourcemap="):]
buildOpts.ExcludeSourceMap = value

case strings.HasPrefix(arg, "--sourcefile="):
if buildOpts != nil {
if buildOpts.Stdin == nil {
Expand Down Expand Up @@ -848,6 +852,7 @@ func parseOptionsImpl(
"cors-origin": true,
"drop-labels": true,
"entry-names": true,
"exclude-sourcemap": true,
"footer": true,
"format": true,
"global-name": true,
Expand Down
27 changes: 27 additions & 0 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,33 @@ let buildTests = {
assert.strictEqual(json.sourcesContent[0], content)
},

async sourceMapExcludePaths({ esbuild, testDir }) {
const input1 = path.join(testDir, 'in1.js')
const input2 = path.join(testDir, 'in2.js')
const output = path.join(testDir, 'out.js')
const content1 = 'import { foo } from "./in2.js"; export {foo}'
const content2 = 'exports.foo = 123'
await Promise.all([
await writeFileAsync(input1, content1),
await writeFileAsync(input2, content2)
])
await esbuild.build({
entryPoints: [input1],
outfile: output,
sourcemap: 'inline',
excludeSourceMap: /in2.js/,
})
const result = require(output)
assert.strictEqual(result.foo, 123)
const outputFile = await readFileAsync(output, 'utf8')
const match = /\/\/# sourceMappingURL=data:application\/json;base64,(.*)/.exec(outputFile)
const json = JSON.parse(Buffer.from(match[1], 'base64').toString())
assert.strictEqual(json.version, 3)
assert.strictEqual(json.sources.length, 1)
assert.strictEqual(json.sources[0], path.basename(input1))
assert.strictEqual(json.sourcesContent[0], content1)
},

async resolveExtensionOrder({ esbuild, testDir }) {
const input = path.join(testDir, 'in.js');
const inputBare = path.join(testDir, 'module.js')
Expand Down