Comments (8)
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.
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.
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.
@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.
--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.
Perfect, I'll try this in a few minutes when I'm home. Thanks!
from webpack.
And... how cool is it? ;)
from webpack.
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)
- Webpack build error for second time HOT 1
- `exportsPresence` does not work for unused imports HOT 3
- Implement ability to use "node:" prefixes for Node.js core modules HOT 1
- mangled exports breaks with destructuring assignment of JSON imports HOT 7
- there are confusing JDocs code
- Potential bug issues
- Webpack is not resolving node_modules specified in `resolve.modules` property.
- Dynamically loading chunks
- [Code implementation issues] ArrayQueue
- [feature] create cli HOT 3
- Not able to build node script
- bootstrap:27 Uncaught TypeError: __webpack_require__.nmd is not a function
- Self-reference dependency has unused export name when imported inside of a web worker
- support `import.meta.dirname` and `import.meta.filename` HOT 2
- <!-- identifier: admin-actions -->
- Remote into Web Component from React giving an error saying " Module does not exist in container"
- Multiple DefinePlugin instances cause aggressive cache invalidation due to collisions HOT 7
- Webpack fails when importing module
- import .scss build to js, the exported mapping is inconsistent with the usage【Urgent!!】
- Unused code elimination and deduplication is not applied when using sass's api: 'legacy' setting
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpack.