Skip to content

Instantly share code, notes, and snippets.

@madx
Created September 15, 2015 13:55
Show Gist options
  • Save madx/53853c3d7b527744917f to your computer and use it in GitHub Desktop.
Save madx/53853c3d7b527744917f to your computer and use it in GitHub Desktop.
Webpack config for an Express app in Node.js
const path = require("path")
const fs = require("fs")
// -- Webpack configuration --
const config = {}
// Application entry point
config.entry = "./src/server/index.js"
// We build for node
config.target = "node"
// Node module dependencies should not be bundled
config.externals = fs.readdirSync("node_modules")
.reduce(function(acc, mod) {
if (mod === ".bin") {
return acc
}
acc[mod] = "commonjs " + mod
return acc
}, {})
// We are outputting a real node app!
config.node = {
console: false,
global: false,
process: false,
Buffer: false,
__filename: false,
__dirname: false,
}
// Output files in the build/ folder
config.output = {
path: path.join(__dirname, "build"),
filename: "[name].js",
}
config.resolve = {
extensions: [
"",
".js",
".json",
],
}
config.module = {}
config.module.loaders = [
// Use babel and eslint to build and validate JavaScript
{
test: /\.js$/,
exclude: /node_modules/,
loaders: [
"babel?{stage:0,jsxPragma:'this.createElement'}",
"eslint",
],
},
// Allow loading of JSON files
{
test: /\.json$/,
loader: "json",
},
]
module.exports = config
@nvs2394
Copy link

nvs2394 commented Nov 9, 2016

Good Job. Thank you

@585
Copy link

585 commented Dec 25, 2016

Thank you, this helped me out 👍

@divyanshverma
Copy link

Great!

@itaditya
Copy link

Which webpack version is compatible with this

@DarwinniwraD
Copy link

Awesome! 👍

@spock123
Copy link

Question: the webpack builds one file. What happens to relative urls in the code, for example loading assets or accessing json files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment