Webpack热中间件不起作用

时间:2018-08-09 19:41:49

标签: javascript node.js express webpack

我对webpack还是很陌生,我正尝试在将express与ejs结合使用时创建一个实时服务器。有人告诉我要使用webpack-middleware-hot,但是当我尝试运行服务器时,出现以下错误...

fs.js:1649
      binding.lstat(baseLong);
              ^
Error: ENOENT: no such file or directory, lstat 'C:\index.js'
    at Object.realpathSync (fs.js:1649:15)
    at Object.eval (webpack:///./node_modules/mini-css-extract-plugin/dist/index.js?:28:48)
    at eval (webpack:///./node_modules/mini-css-extract-plugin/dist/index.js?:329:30)
    at Object../node_modules/mini-css-extract-plugin/dist/index.js (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:9092:1)
    at __webpack_require__ (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:20:30)
    at eval (webpack:///./node_modules/mini-css-extract-plugin/dist/cjs.js?:3:18)
    at Object../node_modules/mini-css-extract-plugin/dist/cjs.js (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:9080:1)
    at __webpack_require__ (C:\Users\School\Desktop\WebpackTest\dist\server\app.js:20:30)
    at Object.eval (webpack:///./webpack.config.js?:4:30)
    at eval (webpack:///./webpack.config.js?:139:30)

在server.js(我称为app.js)中,看起来需要webpack和webpack.config才是导致问题的原因。

webpack.config.js

const webpack = require("webpack");
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = [{
    name: 'server',
    entry: ['./src/server/app.js'],
    target: 'node',
    output: {
        path: path.resolve(__dirname, 'dist/server'),
        filename: 'app.js'
    },
    plugins: [
        new CopyWebpackPlugin([
            {from: 'src/server/views', to: 'views'}
        ])
    ]
},
{
    name: 'client',
    entry: ['babel-polyfill', "webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000", './src/client/js/index.js'],
    output: {
        path: path.resolve(__dirname, 'dist/client'),
        filename: 'js/bundle.js',
        publicPath: "/"
    },
    devServer: {
        contentBase: './dist'
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style.css',
        }),
        new CopyWebpackPlugin([
            {from: 'src/client/img', to: 'img'}
        ]),
        new CleanWebpackPlugin('dist'),
        new webpack.HotModuleReplacementPlugin()
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /\.s?[ac]ss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    { loader: 'css-loader', options: { url: false, sourceMap: true } },
                    { loader: 'sass-loader', options: { sourceMap: true } }
                ]
            },
            {
                test   : /\.(png|jpg|jpeg|gif?)(\?[a-z0-9=&.]+)?$/,
                loader : 'url-loader',
                options: {
                    limit: 100000
                }
            }
        ]
    }
}];

server.js

var express = require('express'),
    app     = express(),
    path = require('path'),
    axios   = require('axios'),
    ejs     = require("ejs").__express;

var webpack = require('webpack'),
    webpackConfig = require('../../webpack.config'),
    compiler = webpack(webpackConfig);

// I commented these lines out while trying to find the issue, it looks like it is the line above that is the culprit
// app.use(require('webpack-dev-middleware')(compiler, {
//         noInfo: true,
//         publicPath: webpackConfig.output.publicPath
//     })
// );

// app.use(require('webpack-hot-middleware')(compiler));

app.set("view engine", "ejs");
app.set("views", "dist/server/views")
app.engine('.ejs', ejs);
app.use(express.static("dist/client"));

app.listen(8080, 'localhost', () => {
    console.log('The server has started');
});

我的文件结构是

project/
└── src/
    ├── client/
    │   ├── img/
    │   ├── js/
    │   └── styles/
    └── server/
        ├── views
        └── app.js

0 个答案:

没有答案