Answer the question
In order to leave comments, you need to log in
How to properly set up webpack in production mode if I use webpackDevMiddleware, webpackHotMiddleware?
Good afternoon.
There is a server on express, in which I use webpackDevMiddleware and webpackHotMiddleware for development.
const express = require('express');
const session = require('express-session');
const http = require('http');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const compiler = webpack(webpackConfig);
const webpackDevMiddleware = require('webpack-dev-middleware')(
compiler, {
publicPath: webpackConfig.output.publicPath,
},
);
const webpackHotMiddleware = require('webpack-hot-middleware')(
compiler,
);
const app = express();
app.use(webpackDevMiddleware);
app.use(webpackHotMiddleware);
app.set('trust proxy', 1);
app.set('port', process.env.PORT || 3000);
app.use(session(.....}));
app.use('/admin', (req, res, next) => { // админка
if (req.session.log_in) next();
else res.redirect('/login');
}, express.static('dist'));
const server = http.createServer(app);
server.listen(3000, () => {
console.log('Сервер запущен на 3000 порту');
});
const webpack = require('webpack');
module.exports = {
mode: 'development',
entry: {
main: ['./admin/main.js'],
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
overlay: true,
inline: true,
hot: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
],
output: {
filename: '[name].bundle.js',
publicPath: '/dist/',
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
{
test: /\.css$/i,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
],
},
],
},
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question