Skip to content

Commit

Permalink
Include uncompiled sources in archives
Browse files Browse the repository at this point in the history
Closes #387
  • Loading branch information
Emily Ekberg committed Dec 3, 2017
1 parent 0730937 commit d618db7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
9 changes: 5 additions & 4 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -135,19 +136,19 @@ 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
}

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

Expand Down
9 changes: 5 additions & 4 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,16 @@ 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";
export let options = { vus: 12345 };
export let file = open("./file.txt");
export default function() { return exclaim(file); };
`),
}, fs)
}
b, err := NewBundle(src, fs)
if !assert.NoError(t, err) {
return
}
Expand All @@ -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"]))

Expand Down
8 changes: 7 additions & 1 deletion js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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]
Expand Down

0 comments on commit d618db7

Please sign in to comment.