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)
- Build takes twice as long with >=5.92.0 compared to 5.91.0 HOT 21
- Minification produces "let t,t,t,t,t,t..." HOT 1
- Webpack performance issue when using webpack-merge HOT 1
- Entry module isn't executed when targeting webworker with a runtime chunk HOT 4
- Field 'browser' doesn't contain a valid alias configuration /opt/atlassian/pipelines/agent/build/src/css/index.css doesn't exist .tsx
- css not loaded properly?
- Webpack5 Angular SyntaxError: HookWebpackError: Module build failed
- Does CVE-2024-43788 actually affect Webpack 4? HOT 7
- EnvironmentPlugin types don't allow passing null HOT 1
- mangle destructuring default in dynamic import HOT 4
- Using aliases with dynamic imports in 5.94.0 HOT 1
- 对于rspack的出现 你们团队会因为这个竞品感到紧张吗 HOT 1
- SyntaxError: Unexpected token import on webpack build
- Webpack Stuck at 10% building forever HOT 2
- Compatibility with typescript 5.6 HOT 1
- Duplicated chunks do not merged after split chunks plugin finish work
- Dynamically importing the same file twice in a specific way, incorrectly processes the second request.
- Not able to resolve webpack loader issue
- Unable to build WebPack code in nodejs latest version HOT 1
- fatalDeprecations and verbose true not working on sass-loader HOT 1
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.