diff --git a/parser/parser.go b/parser/parser.go index e5bb2eb0..75b7c500 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -35,6 +35,7 @@ package parser import ( "bytes" + "encoding/base64" "errors" "io" "io/ioutil" @@ -161,6 +162,19 @@ func ParseFileWithSourceMap(fileSet *file.FileSet, filename string, javascriptSo return nil, err } + if sourcemapSource == nil { + lines := bytes.Split(src, []byte("\n")) + lastLine := lines[len(lines)-1] + if bytes.HasPrefix(lastLine, []byte("//# sourceMappingURL=data:application/json")) { + bits := bytes.SplitN(lastLine, []byte(","), 2) + if len(bits) == 2 { + if d, err := base64.StdEncoding.DecodeString(string(bits[1])); err == nil { + sourcemapSource = d + } + } + } + } + sm, err := ReadSourceMap(filename, sourcemapSource) if err != nil { return nil, err diff --git a/sourcemap_test.go b/sourcemap_test.go index 93b72a8b..afb82410 100644 --- a/sourcemap_test.go +++ b/sourcemap_test.go @@ -8,6 +8,7 @@ const ( testSourcemapCodeOriginal = "function functionA(argA, argB) {\n functionB(argA, argB);\n}\n\nfunction functionB(argA, argB) {\n functionExternal(argA, argB);\n}" testSourcemapCodeMangled = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}" testSourcemapContent = `{"version":3,"sources":["hello.js"],"names":["functionA","argA","argB","functionB","functionExternal"],"mappings":"AAAA,QAASA,WAAUC,KAAMC,MACvBC,UAAUF,KAAMC,MAGlB,QAASC,WAAUF,KAAMC,MACvBE,iBAAiBH,KAAMC"}` + testSourcemapInline = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImhlbGxvLmpzIl0sIm5hbWVzIjpbImZ1bmN0aW9uQSIsImFyZ0EiLCJhcmdCIiwiZnVuY3Rpb25CIiwiZnVuY3Rpb25FeHRlcm5hbCJdLCJtYXBwaW5ncyI6IkFBQUEsUUFBU0EsV0FBVUMsS0FBTUMsTUFDdkJDLFVBQVVGLEtBQU1DLE1BR2xCLFFBQVNDLFdBQVVGLEtBQU1DLE1BQ3ZCRSxpQkFBaUJILEtBQU1DIn0=" testSourcemapOriginalStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:3)\n at functionA (hello.js:2:3)\n at :1:1\n" testSourcemapMangledStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:1:82)\n at functionA (hello.js:1:31)\n at :1:1\n" testSourcemapMappedStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:2)\n at functionA (hello.js:2:2)\n at :1:1\n" @@ -79,6 +80,28 @@ func TestSourceMapMangledWithSourcemap(t *testing.T) { }) } +func TestSourceMapMangledWithInlineSourcemap(t *testing.T) { + tt(t, func() { + vm := New() + + s, err := vm.CompileWithSourceMap("hello.js", testSourcemapInline, nil) + if err != nil { + panic(err) + } + + if _, err := vm.Run(s); err != nil { + panic(err) + } + + _, err = vm.Run(`functionA()`) + if err == nil { + panic("error should not be nil") + } + + is(err.(*Error).String(), testSourcemapMappedStack) + }) +} + func TestSourceMapContextPosition(t *testing.T) { tt(t, func() { vm := New()