1

我只是从 webpack 文档中复制粘贴 webpack-dev-server + express 设置,但它不起作用,因为服务器找不到要服务的文件。这个设置有什么问题?

服务器.js

const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');

const app = express();
const config = require('../webpack.config.js');
const compiler = webpack(config);

app.use(webpackDevMiddleware(compiler, {
  publicPath: config.output.publicPath
})); 
/*app.use('/',express.static('public')) it works when used insted of webpack dev middleware */
// Serve the files on port 3000.
app.listen(3000, function () {
  console.log('Example app listening on port 3000!\n');
});

webpack.config.js

const path = require('path');
const webpack = require('webpack');

const scssRules = {
    test:/\.scss$/,
    use: ['style-loader', 'css-loader', 'sass-loader']
};

const jsRules = {
    test: /\.js$/,
    use: [
        {loader: 'babel-loader'}
    ]
};

module.exports = {
    entry: './js/App.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'public'),
        publicPath: '/'
    },
    module: {
        rules: [
            jsRules,
            scssRules
        ]
    },
    devServer: {
        contentBase: './public'
    },  
    plugins:[
        new webpack.HotModuleReplacementPlugin()
    ],
  devtool: 'inline-source-map'
}

文件结构: 在此处输入图像描述

我所看到的: 在此处输入图像描述

4

1 回答 1

1

我也遇到过这个问题。

原来 webpack-dev-middleware 不输出任何文件,而是从内存中提供服务。快速服务器需要物理文件。

在这里,这个插件可能会有所帮助:https ://github.com/gajus/write-file-webpack-plugin

于 2018-01-02T21:43:58.607 回答