Skip to content

Commit

Permalink
Detect source map URL when the source is wrapped in "(function() { ..…
Browse files Browse the repository at this point in the history
…. })". See dop251#235.
  • Loading branch information
dop251 authored and tsedgwick committed Apr 14, 2021
1 parent 390bc9b commit 7b3c226
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
17 changes: 17 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,20 @@ func TestPosition(t *testing.T) {
is(node.(*ast.FunctionLiteral).Source, "function(){ return abc; }")
})
}

func TestExtractSourceMapLine(t *testing.T) {
tt(t, func() {
is(extractSourceMapLine(""), "")
is(extractSourceMapLine("\n"), "")
is(extractSourceMapLine(" "), "")
is(extractSourceMapLine("1\n2\n3\n4\n"), "")

src := `"use strict";
var x = {};
//# sourceMappingURL=delme.js.map`
modSrc := `(function(exports, require, module) {` + src + `
})`
is(extractSourceMapLine(modSrc), "//# sourceMappingURL=delme.js.map")
is(extractSourceMapLine(modSrc+"\n\n\n\n"), "//# sourceMappingURL=delme.js.map")
})
}
26 changes: 22 additions & 4 deletions parser/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,29 @@ func (self *_parser) parseProgram() *ast.Program {
}
}

func extractSourceMapLine(str string) string {
for {
p := strings.LastIndexByte(str, '\n')
line := str[p+1:]
if line != "" && line != "})" {
if strings.HasPrefix(line, "//# sourceMappingURL=") {
return line
}
break
}
if p >= 0 {
str = str[:p]
} else {
break
}
}
return ""
}

func (self *_parser) parseSourceMap() *sourcemap.Consumer {
lastLine := self.str[strings.LastIndexByte(self.str, '\n')+1:]
if strings.HasPrefix(lastLine, "//# sourceMappingURL") {
urlIndex := strings.Index(lastLine, "=")
urlStr := lastLine[urlIndex+1:]
if smLine := extractSourceMapLine(self.str); smLine != "" {
urlIndex := strings.Index(smLine, "=")
urlStr := smLine[urlIndex+1:]

var data []byte
if strings.HasPrefix(urlStr, "data:application/json") {
Expand Down

0 comments on commit 7b3c226

Please sign in to comment.