graingert / slimerjs

NPM wrapper for installing phantomjs
Other
29 stars 9 forks source link

Issue with the module require implementation #18

Closed B2F closed 9 years ago

B2F commented 9 years ago

Currently, variables defined in the function scope can be accessed by modules, in both nodejs and phantom require implementation but in slimerjs modules seems to be completely isolated from the function scope.

E.g.

From file A (function scope root):

somFunc = function() {
...
  var data = { prop: ... };
  require(fileB);
...
}

From file B (module file):

// Will throw a ReferenceError in slimerjs but not in nodejs nor phantom:
var whatever = data.prop;
exports.whatever;

Anything can be done to fix this ?

graingert commented 9 years ago

You should raise this issue with the actual slimerjs binary: https://github.com/laurentj/slimerjs/issues

But it looks to me like it's by design.

http://docs.slimerjs.org/0.9/script-execution.html#modules-and-main-script-context

When you're testing things you should browserify it into one file then run that.

graingert commented 9 years ago

You should probably not depend on that behaviour

use:

# a.js
import fileB from "b"

function somFunc () {
...
  let data = { prop: ... };
  fileB.mutateWhatever(data)
...
}
# b.js

let B = {
    mutateWhatever(data){
        this.whatever = data.prop
    }
}

export default B

This will then work on any platform with Babel+Browserify

B2F commented 9 years ago

You are right it's more of a design issue on y side of things. Thanks.