dev-server.js 1.28 KB
const express = require('express');
const webpack = require('webpack');
const compression = require('compression');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.dev.config.js');
const path = require('path');

const compiler = webpack(webpackConfig);

const devMiddleware = webpackDevMiddleware(compiler, {
    publicPath: webpackConfig.output.publicPath,
    quiet: true,
    headers: {
        'Access-Control-Allow-Origin': '*'
    }
});
const hotMiddleware = webpackHotMiddleware(compiler, {
    log: () => {}
});

const app = express();

app.use(devMiddleware);
app.use(hotMiddleware);

app.use((req, res, next) => {
    res.set('Access-Control-Allow-Origin', '*');
    next();
});

app.use('/',
    express.static(path.join(__dirname, '../')),
    express.static(path.join(__dirname, './bundle/')),
    express.static(path.join(__dirname, './dll/')));

app.use('/event', (req, res) => {
    if (req.query.action === 'reload') {
        hotMiddleware.publish({
            action: 'reload'
        });
    }
    res.json({
        action: req.query.action,
        message: 'ok'
    });
});

app.use(compression());
app.listen(5001, () => {
    console.log('dev start 5001');
});