// Tell express to use the webpack-dev-middleware and use the webpack.config.js
// configuration file as a base.
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
// Serve the files on port 3000.
app.listen(3000, function () {
console.log('Example app listening on port 3000!\n');
});
opn('http://localhost:3000/')`
webpack.config.js
` const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = { entry: { first: "./bar.js", //多入口 second: "./foo.js" }, output: { //filename: './[name].js' // [name]相当于entry里面的键 first second path: __dirname + '/dist', //filename: '[name].[hash].js' // [hash] ===> first.a359094213028891de6d.js 解决缓存问题 //filename: 'dist.chunkhash=[chunkhash:10].name=[name].id=[id].js' //dist.chunkhash=[chunkhash:10].name=[name].id=[id].js filename: '[name].[chunkhash].js', publicPath: '/' }, plugins: [ //这里添加插件 new HtmlWebpackPlugin({ title: 'my html', template: './demo.html' }) ], // devServer: { // contentBase: './dist' // } }`
server.js
`const express = require('express'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); var opn = require('opn')
const app = express(); const config = require('./webpack.config.js'); const compiler = webpack(config);
// Tell express to use the webpack-dev-middleware and use the webpack.config.js // configuration file as a base. app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath }));
// Serve the files on port 3000. app.listen(3000, function () { console.log('Example app listening on port 3000!\n'); }); opn('http://localhost:3000/')`