Giter Site home page Giter Site logo

Comments (8)

sokra avatar sokra commented on March 29, 2024

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;

from webpack.

rdrey avatar rdrey commented on March 29, 2024

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?

from webpack.

sokra avatar sokra commented on March 29, 2024

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.

from webpack.

rdrey avatar rdrey commented on March 29, 2024

@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.

from webpack.

sokra avatar sokra commented on March 29, 2024

--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

from webpack.

rdrey avatar rdrey commented on March 29, 2024

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

from webpack.

sokra avatar sokra commented on March 29, 2024

And... how cool is it? ;)

from webpack.

rdrey avatar rdrey commented on March 29, 2024

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.

from webpack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.