Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created September 26, 2014 16:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save trodrigues/f7cf379e5dd9ebf89cc1 to your computer and use it in GitHub Desktop.
commonjs and file loading

Loading a remote file through commonjs wouldn't be possible because commonjs is just the module format, not exactly a file loader. Require.js for instance is both: it's an implementation of the AMD module format AND a file loader. browserify for instance is just an implementation of the commonjs module format.

So you'd have to load the file separately, and when it's ready you could require it.

Let's say you have remote_module.js which goes:

exports.module = function(){};

and then somewhere else you'd have to do something like

$.get('http://somewhere.com/remote_module.js').then(function(){
  require('remote_module');
  // use remote module
});

This also assumes that you'd browserify remote_module.js so it would be nicely declared as a commonjs module and whatever.

@aredridel
Copy link

Yeah. The trick is to tell require "Hey, there's new code!" if you want to do that.

@operatorjen
Copy link

if someone doesn't send me a PR in 10 min, i will do it myself. COUNTDOWN STARTS MEOW!!! 💃

@trodrigues
Copy link
Author

@aredridel that would be more complex, you'd need some websockets or pushy thing to send updates down the tubes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment