diff --git a/js/bundle.go b/js/bundle.go index 187e3ca1bf7..1b8328bd5ce 100644 --- a/js/bundle.go +++ b/js/bundle.go @@ -71,7 +71,8 @@ func collectEnv() map[string]string { // Creates a new bundle from a source file and a filesystem. func NewBundle(src *lib.SourceData, fs afero.Fs) (*Bundle, error) { // Compile sources, both ES5 and ES6 are supported. - pgm, code, err := compiler.Compile(string(src.Data), src.Filename, "", "", true) + code := string(src.Data) + pgm, _, err := compiler.Compile(code, src.Filename, "", "", true) if err != nil { return nil, err } @@ -135,7 +136,7 @@ func NewBundleFromArchive(arc *lib.Archive) (*Bundle, error) { return nil, errors.Errorf("expected bundle type 'js', got '%s'", arc.Type) } - pgm, err := goja.Compile(arc.Filename, string(arc.Data), true) + pgm, _, err := compiler.Compile(string(arc.Data), arc.Filename, "", "", true) if err != nil { return nil, err } @@ -143,11 +144,11 @@ func NewBundleFromArchive(arc *lib.Archive) (*Bundle, error) { initctx := NewInitContext(goja.New(), new(context.Context), nil, arc.Pwd) for filename, data := range arc.Scripts { src := string(data) - scr, err := goja.Compile(filename, src, true) + pgm, err := initctx.compileImport(src, filename) if err != nil { return nil, err } - initctx.programs[filename] = programWithSource{scr, src} + initctx.programs[filename] = programWithSource{pgm, src} } initctx.files = arc.Files diff --git a/js/bundle_test.go b/js/bundle_test.go index 7998a0cfddd..5e1b9d99812 100644 --- a/js/bundle_test.go +++ b/js/bundle_test.go @@ -430,7 +430,7 @@ func TestNewBundleFromArchive(t *testing.T) { assert.NoError(t, afero.WriteFile(fs, "/path/to/file.txt", []byte(`hi`), 0644)) assert.NoError(t, afero.WriteFile(fs, "/path/to/exclaim.js", []byte(`export default function(s) { return s + "!" };`), 0644)) - b, err := NewBundle(&lib.SourceData{ + src := &lib.SourceData{ Filename: "/path/to/script.js", Data: []byte(` import exclaim from "./exclaim.js"; @@ -438,7 +438,8 @@ func TestNewBundleFromArchive(t *testing.T) { export let file = open("./file.txt"); export default function() { return exclaim(file); }; `), - }, fs) + } + b, err := NewBundle(src, fs) if !assert.NoError(t, err) { return } @@ -458,10 +459,10 @@ func TestNewBundleFromArchive(t *testing.T) { assert.Equal(t, "js", arc.Type) assert.Equal(t, lib.Options{VUs: null.IntFrom(12345)}, arc.Options) assert.Equal(t, "/path/to/script.js", arc.Filename) - assert.Equal(t, "\"use strict\";Object.defineProperty(exports, \"__esModule\", { value: true });exports.file = exports.options = undefined;exports.default =\n\n\n\nfunction () {return (0, _exclaim2.default)(file);};var _exclaim = require(\"./exclaim.js\");var _exclaim2 = _interopRequireDefault(_exclaim);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var options = exports.options = { vus: 12345 };var file = exports.file = open(\"./file.txt\");;", string(arc.Data)) + assert.Equal(t, string(src.Data), string(arc.Data)) assert.Equal(t, "/path/to", arc.Pwd) assert.Len(t, arc.Scripts, 1) - assert.Equal(t, "(function(){\"use strict\";Object.defineProperty(exports, \"__esModule\", { value: true });exports.default = function (s) {return s + \"!\";};;})()", string(arc.Scripts["/path/to/exclaim.js"])) + assert.Equal(t, `export default function(s) { return s + "!" };`, string(arc.Scripts["/path/to/exclaim.js"])) assert.Len(t, arc.Files, 1) assert.Equal(t, `hi`, string(arc.Files["/path/to/file.txt"])) diff --git a/js/initcontext.go b/js/initcontext.go index 1488962bed9..2d0e59c8272 100644 --- a/js/initcontext.go +++ b/js/initcontext.go @@ -136,7 +136,8 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) { } // Compile the sources; this handles ES5 vs ES6 automatically. - pgm_, src, err := compiler.Compile(string(data.Data), data.Filename, "(function(){", "})()", true) + src := string(data.Data) + pgm_, err := i.compileImport(src, data.Filename) if err != nil { return goja.Undefined(), err } @@ -154,6 +155,11 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) { return module.Get("exports"), nil } +func (i *InitContext) compileImport(src, filename string) (*goja.Program, error) { + pgm, _, err := compiler.Compile(src, filename, "(function(){", "})()", true) + return pgm, err +} + func (i *InitContext) Open(name string) (string, error) { filename := loader.Resolve(i.pwd, name) data, ok := i.files[filename]