Reputation: 63
I'm using webpack-dev-middleware in my server to compile javascript like this:
if (development){
app.use(webpackMiddleware(webpack({
// webpack options
// webpackMiddleware takes a Compiler object as first parameter
// which is returned by webpack(...) without callback.
entry: {
dashboard: path.join(__dirname, 'scripts/dashboard.jsx'),
tasks: path.join(__dirname, 'scripts/tasks.jsx')
},
output: {
path: __dirname + 'dist',
filename: '[name].bundle.js',
// no real path is required, just pass "/"
// but it will work with other paths too.
},
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{ test: /\.jsx$/, loader: "jsx" }
]
}
}
),
{
stats: {
colors: true
}
}));
}
Everything works fine in development, I can include the bundles in my views. But in production I cannot include them, because they are not build into 'dist'. This folder is always empty. What do I do wrong? Does somebody has an idea?
Best Regards Jan
Upvotes: 4
Views: 7123
Reputation: 1164
There is an available option in webpack-dev-middleware
.
writeToFile
can be a boolean or a function that receives the file path and outputs an boolean. See the documentation here
Here, I'm using a function so all the intermediate bundles don't get written to disk, only the whole bundle:
const webpack = require('webpack');
const webpackconfig = require('./webpack.config.js');
const webpackMiddleware = require("webpack-dev-middleware");
function shouldWrite(filePath) {
if (path.basename(filePath) == 'bundle.js' || path.basename(filePath) == 'bundle.js.map') {
return true
}
else {
return false;
}
}
const webpackCompiler = webpack(webpackconfig);
const wpmw = webpackMiddleware(webpackCompiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
writeToDisk: shouldWrite
});
//express
app.use(wpmw); // dev-middleware
Upvotes: 1
Reputation: 1819
if you want to use the in-memory bundle, you can use the streamed file like this:
var express = require('express');
var app = express();
var webpack = require('webpack');
var path = require('path');
var compiler = webpack(require('./webpack.config.js'));
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: '/'
}));
app.use('*', function (req, res, next) {
var filename = path.join(compiler.outputPath,'index.html');
compiler.outputFileSystem.readFile(filename, function(err, result){
if (err) {
return next(err);
}
res.set('content-type','text/html');
res.send(result);
res.end();
});
});
app.listen(3000);
Upvotes: 4
Reputation: 1032
webpack-dev-middleware
doesn't output any file. If you want to output real files, you need to use the webpack
command outside of express. You can put your webpack config into a seperate file, and refer to that with both your webpack
command and require it in your express script to access it.
Upvotes: 7