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 overwrites custom objects passed into resolve.unsafeCache HOT 11
- Error: Uncaught ReferenceError: __webpack_require__ is not defined at HOT 2
- Option to keep /*webpackIgnore:true*/ in the bundle
- Can't import child_process HOT 1
- Module Federation loadRemoteModule catch error E[n].get is not a function
- Resolver type from webpack/enhanced-resolve are not compatible HOT 3
- v5.90.3 pollution global variables jquery $
- License scan badge shown as failing HOT 10
- `snapshot.unmanagedPaths` is ignored
- Module Federation - Some chunks are not loaded when navigating from the Remote module to the Host module
- failed to import ffmpeg-core.js HOT 1
- Webpack 5 doesnt generate the chunks of imports HOT 1
- Error: Cannot find module 'webpack-cli/package.json' HOT 2
- Unable to bust cache of module federation remotes....
- Re-exported Ant Design components don't have same style with the original components
- failed to import ffmpeg-core.js HOT 3
- DllPlugin: When processing multiple chunks, does the `path` option must have a string template?
- Webpack BannerPlugin with `raw` option should put `use strict` on the very first line HOT 10
- After webpack 5 migration not able to use local packages in a mono repo
- 95% emitting emit phase takes much longer in Node >=16
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.