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

Support dynamic require #9

Open
sindresorhus opened this issue Sep 11, 2013 · 7 comments
Open

Support dynamic require #9

sindresorhus opened this issue Sep 11, 2013 · 7 comments

Comments

@sindresorhus
Copy link

Many modules uses require to dynamically load JSON files.

Example:

require('./path/' + name + '.json');
@ghost
Copy link

ghost commented Sep 11, 2013

This is pretty tricky but might be possible in some cases where name is statically resolvable. I think a static resolver module would be the best way to implement this.

@sindresorhus
Copy link
Author

Another usecase (from cheerio):

var api = ['attributes', 'traversing', 'manipulation'];

api.forEach(function(mod) {
  _.extend(Cheerio.prototype, require('./api/' + mod));
});

Related ticket

Probably more of a browsersify core issue, but it does seem slightly related.

@fb55
Copy link

fb55 commented Sep 11, 2013

The sledge hammer approach taken by nodejitsu/require-analyzer is to first check if all requires can be resolved statically, and if they can't, fire up a new node instance, monkey-patch Module#require and track everything that's required.

While this would certainly work for browserify, it might complicate the development process, as the compilation thread in the background might run into an infinite loop and block the system.

browserify should at least add support for statically resolving variables, plus some of the array extras. Especially map is interesting here, as I can imagine using ES6 destructuring expressions to require multiple modules at once will become a common pattern:

let [fs, path] = ["fs", "path"].map(require)

@leesei
Copy link

leesei commented Apr 29, 2016

I have this use case

[ 'a.js', 'b.js' ].forEach(js => {
  injectJS(fs.readFileSync(js));
});

@YerkoPalma
Copy link

I had this issue too, but as said before, couldn't achieve this with brfs. What I did was a simply fetch request. Would it be possible to replace

fs.readFile('posts/' + post + '.md', function (err, content) {
  // process file
})

with

fetch('posts/' + post + '.md')
   .then(response => response.text())
   .then(content => {
     // process file
   })

So it would dynamically load files with ajax?

@dy
Copy link

dy commented Oct 26, 2018

@YerkoPalma even better now:

var content = (await fetch(`posts/${post}.md`)).text()

@goto-bus-stop
Copy link
Member

@dy fetch()'s return value doesn't have a .text() method, so with async/await you have to do something like

var resp = await fetch(`posts/${post}.md`)
var content = await resp.text()

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

6 participants