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

process.browser and suggestions #35

Closed
rdrey opened this issue Nov 6, 2012 · 8 comments
Closed

process.browser and suggestions #35

rdrey opened this issue Nov 6, 2012 · 8 comments

Comments

@rdrey
Copy link
Contributor

rdrey commented Nov 6, 2012

Hey @sokra

I just hacked together this little module: (not related to components/components)

I am using the ComponentLoader.available() function to return every component, so I can use them in a user interface.

var ComponentLoader = {
  get: function(name) {
    return require('../components/' + name);
  },
  available: function() {
    var components;
    if (typeof window !== 'undefined') {
      components = require.context('../components').keys();
    } else {
      components = require('fs').readdirSync('./components');
    }
    return components.map(function(fileName) {
      return fileName.replace('./', '').replace('.js', '');
    });
  }
};

module.exports = ComponentLoader;

It works surprisingly well. My main grief points are the following:

  1. Could we get a process.browser or some other utility variable to tell us where our code is running?
  2. I tried requiring webpack/enhanced-require and removed the if statement to run the same components = require.context('../components').keys(); in the browser and the server, but got the following error:

Error:

TypeError: Object function () { [native code] } has no method 'keys'
    at Object.ComponentLoader.available (/Users/rainerdreyer/Workspace/PipeDream/src/ComponentLoader.js:9:55)
    at Object.<anonymous> (/Users/rainerdreyer/Workspace/PipeDream/main.js:12:45)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Object..js (/usr/local/lib/node_modules/node-dev/wrapper.js:121:15)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/node-dev/wrapper.js:177:1)

So is it possible to use the exact same code on both server and client?

Thanks!

sokra added a commit to webpack/enhanced-require that referenced this issue Nov 7, 2012
@sokra
Copy link
Member

sokra commented Nov 7, 2012

It's recommended to use the .web.js extension for switching between browser and server impl.

// componentsList.js
module.exports = require('fs')
  .readdirSync('./components')
  .map(function(c) { return c.replace('.js', ''); });

// componentsList.web.js
module.exports = require
  .context('../components').keys()
  .map(function(c) { return c.replace('./', '').replace('.js', ''); });

// ComponentsLoader.js
var ComponentLoader = {
  get: function(name) {
    return require('../components/' + name);
  },
  available: function() {
    var components;
    return require("./componentsList");
  }
};

module.exports = ComponentLoader;

It would not include server side code into browser bundle.

Elsewise testing window is fine and you can test process.platform == "browser".


yes require.context(...).keys() should be also availible on server side with enhanced-require. It's a bug that it is missing. So I fixed it and you can use the same code and server and client side:

var ComponentLoader = {
  get: function(name) {
    return require('../components/' + name);
  },
  available: function() {
    var components;
    components = require.context('../components').keys();
    return components.map(function(fileName) {
      return fileName.replace('./', '').replace('.js', '');
    });
  }
};

module.exports = ComponentLoader;

@sokra sokra closed this as completed Nov 7, 2012
@rdrey
Copy link
Contributor Author

rdrey commented Nov 7, 2012

Haha, thanks. I like the fact that I can do 2. on both client and server now. :)

I didn't know about process.platform, I feel like that's neater than testing for window. Maybe add it to README.md under Bonus Features?

@sokra
Copy link
Member

sokra commented Nov 7, 2012

So it should be :)

The problem with process.platform is that is includes some modules from node.js compatibility. process uses events and events use util. That makes the bundle bigger. So process.platform is more for compatibility and not recommended for browser testing. The best is to create different files for client and server. Or test the feature you want to use.

@rdrey
Copy link
Contributor Author

rdrey commented Nov 8, 2012

@sokra One more question, if you don't mind:

For debugging purposes, I'd love it if each require()d module arrived in a separate file (much easier to set breakpoints in chrome's dev tools, than scrolling through the massive webpack file with all my components).

How would you do this? Would you use require.ensure in the ComponentLoader.get() function? Does that make all require's async? Sorry, I haven't had a chance to test this yet since I'm at work right now, but I'd love to see how you would implement it.

@sokra
Copy link
Member

sokra commented Nov 8, 2012

--debug / { debug: true }

This make chrome dev tools display each module like a separate file.


require.ensure is Code Splitting which makes only the require.ensure async. See code-splitting example

@rdrey
Copy link
Contributor Author

rdrey commented Nov 8, 2012

Perfect, I'll try this in a few minutes when I'm home. Thanks!

@sokra
Copy link
Member

sokra commented Nov 8, 2012

And... how cool is it? ;)

@rdrey
Copy link
Contributor Author

rdrey commented Nov 8, 2012

Beautiful! :D

Now I just need to hook up your connect middleware into my node server to remove that compile step... I might even try out the Hot Module Reloading, while I'm at it.

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

2 participants