Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix module double load #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func CreateLoaderFromSource(source, filename string) ModuleLoader {
pwd := filepath.Dir(filename)
return func(vm *Motto) (otto.Value, error) {
// Wraps the source to create a module environment
source = "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;" + source + "\n})"
source := "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;" + source + "\n})"

// Provide the "require" method in the module scope.
jsRequire := func(call otto.FunctionCall) otto.Value {
Expand Down
24 changes: 23 additions & 1 deletion motto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package motto

import (
"github.com/robertkrimen/otto"
"io/ioutil"
"testing"

"github.com/robertkrimen/otto"
)

func TestModule(t *testing.T) {
Expand Down Expand Up @@ -66,3 +67,24 @@ func fsModuleLoader(vm *Motto) (otto.Value, error) {

return vm.ToValue(fs)
}

func TestDoubleLoadModule(t *testing.T) {
loader := CreateLoaderFromSource(`{
module.exports = "bar";
}`, "")

testLoad := func() {
vm := New()
vm.AddModule("foo", loader)
v, err := vm.Require("foo", ".")
if err != nil {
t.Fatal(err)
}
s, _ := v.ToString()
if s != "bar" {
t.Errorf("expect bar, got %s", s)
}
}
testLoad()
testLoad()
}