Skip to content

Commit

Permalink
fix: module.exports breaking when setting directly and multiple imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Aug 7, 2019
1 parent 945951a commit d0ee223
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
)

type programWithSource struct {
pgm *goja.Program
src string
exports goja.Value
pgm *goja.Program
src string
module *goja.Object
}

// InitContext provides APIs for use in the init context.
Expand Down Expand Up @@ -135,7 +135,7 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) {

// First, check if we have a cached program already.
pgm, ok := i.programs[fileURL.String()]
if !ok || pgm.exports == nil {
if !ok || pgm.module == nil {
i.pwd = loader.Dir(fileURL)
defer func() { i.pwd = pwd }()

Expand All @@ -146,9 +146,9 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) {
defer i.runtime.Set("module", oldModule)
exports := i.runtime.NewObject()
i.runtime.Set("exports", exports)
module := i.runtime.NewObject()
_ = module.Set("exports", exports)
i.runtime.Set("module", module)
pgm.module = i.runtime.NewObject()
_ = pgm.module.Set("exports", exports)
i.runtime.Set("module", pgm.module)
if pgm.pgm == nil {
// Load the sources; the loader takes care of remote loading, etc.
data, err := loader.Load(i.filesystems, fileURL, name)
Expand All @@ -165,18 +165,16 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) {
}
}

pgm.exports = module.Get("exports")
i.programs[fileURL.String()] = pgm

// Run the program.
if _, err := i.runtime.RunProgram(pgm.pgm); err != nil {
delete(i.programs, fileURL.String())
return goja.Undefined(), err
}

pgm.exports = module.Get("exports")
}

return pgm.exports, nil
return pgm.module.Get("exports"), nil
}

func (i *InitContext) compileImport(src, filename string) (*goja.Program, error) {
Expand Down

0 comments on commit d0ee223

Please sign in to comment.