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

Join CommonJS modules to single file. #135

Open
unc0 opened this issue Aug 6, 2013 · 1 comment
Open

Join CommonJS modules to single file. #135

unc0 opened this issue Aug 6, 2013 · 1 comment

Comments

@unc0
Copy link
Contributor

unc0 commented Aug 6, 2013

In mocha.js they using a CommonJS require to make mocha runable in browser, it is a lite version of brequire.

Compiled single JS may looks like this:

;(function(){
// ** AMD wrapper here? **
// ** jsprelude stuff here **
// CommonJS require()
function require(p){
    var path = require.resolve(p)
      , mod = require.modules[path];
    if (!mod) throw new Error('failed to require "' + p + '"');
    if (!mod.exports) {
      mod.exports = {};
      mod.call(mod.exports, mod, mod.exports, require.relative(path));
    }
    return mod.exports;
  }

require.modules = {};

require.resolve = function (path){
    var orig = path
      , reg = path + '.js'
      , index = path + '/index.js';
    return require.modules[reg] && reg
      || require.modules[index] && index
      || orig;
  };

require.register = function (path, fn){
    require.modules[path] = fn;
  };

require.relative = function (parent) {
    return function(p){
      if ('.' != p.charAt(0)) return require(p);

      var path = parent.split('/')
        , segs = p.split('/');
      path.pop();

      for (var i = 0; i < segs.length; i++) {
        var seg = segs[i];
        if ('..' == seg) path.pop();
        else if ('.' != seg) path.push(seg);
      }

      return require(path.join('/'));
    };
  };

Then gorillascript compiled with options.bare on then wrapped in this form:

require.register("path/to/file.gs", function(module, exports, require){
// [gorillascript compiled]
}); // module: path/to/file.gs
@unc0
Copy link
Contributor Author

unc0 commented Aug 7, 2013

I've create a new branch brequire in my fork, you can use this to try it out:

gorilla --commonjs -c -o all.js **/*.gs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant