-
Notifications
You must be signed in to change notification settings - Fork 202
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
Support building for a server-side target #59 #76
Conversation
Hi, I think the problem here is your webpack configuration. Webpack will load modules from the node_modules folder and bundle them in. While this is fine for frontend code, backend modules aren't prepared for this. It would be nice if you share your webpack configuration or try to add this to your webpack.config.js var fs = require('fs');
var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
}); ps: I just tried contentfuljs in a small express app using webpack and I had no problem Please let us know if this helped. |
Hey @Khaledgarbaya Here is a webpack config where it fails for me. I believe the key here is the
|
Hi, I think it is related to the modules loading in my config I am using externals var nodeModules = fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
});
//......
var backendConfig = config({
entry: [
'./src/main.js'
],
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'backend.js'
},
node: {
__dirname: true,
__filename: true
},
externals: [
function(context, request, callback) {
var pathStart = request.split('/')[0];
if (nodeModules.indexOf(pathStart) >= 0) {
return callback(null, "commonjs " + request);
};
callback();
}
],
//...... rest of the config Can you try it and tell me if it is working. |
Interesting. That does fix the issue for me. I really don't think adding that code is an acceptable solution for me. I would rather not introduce sophisticated logic into externals -- it is intended for things like FWIW, matching the node target to an appropriate file in package.json seems like a more appropriate solution. I haven't (knowingly!) encountered a library where the default main js file in package.json is not intended for primary use. Given that webpack supports many targets beyond 'node' and 'browser', maybe there are other patterns for setting up files for various targets that are more appropriate. |
Thanks for all the feedback, I will definitely look into that more with the team. |
Hello @zamiang I was having a better look at this (I was away for the last week so couldn't look into this immediately), as I setup the current build, and I was investigating this a bit better and doing a few more tests. Your solution does indeed seem to fix the issue and from what I can tell so far, it looks like the only thing we could really do from our side that could help out with people using targets in webpack other than browser. However, this is one of those package.json fields (like What I'm trying to understand is: is the I'll do some more investigation on this, but if you could provide a source of information for where this field comes from, that would be very helpful, as otherwise I'd feel a bit weary of adding something when we can't truly understand the full impact it would have. |
Quick update, found some additional info about webpack targets. They are documented here: https://webpack.github.io/docs/configuration.html#target I am still in a scenario where I do need to use the Alternatively, if this is not a change you are interested in, I would love some guidance on how I might use a fork the project. Today's attempts have not been successful due to changes that happen when the module is packaged and published to npm vs how files appear in the repo. |
Hi @zamiang , |
Changes Unknown when pulling 787f718 on zamiang:server-side-webpack into * on contentful:master*. |
Hi @zamiang , |
After our discussion in #59, I did a little digging and found what was wrong (I think?)
Unlike in your example app which is building for browsers, I am using webpack to build for a node target. Since there was no node option in the
package.json
, it would fall back toindex.js
and error trying to loadlib/contentful
.This PR adds a node option to the package.json to support server-side builds such as this.