Webpack 2 beta + 历史 API 回退不起作用

Webpack 2 beta + history API fallback not working

今天,在删除我的 node_modules 并使用 npm install 重新安装它们后,我的项目似乎无法运行。

这是我的 webpack 配置

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

const srcPath = path.join(__dirname, './client');
const nodeEnv = process.env.NODE_ENV || 'development';
const isProd = nodeEnv === 'production';

module.exports = {
  devtool: isProd ? 'hidden-source-map' : 'cheap-module-eval-source-map',
  context: path.join(__dirname, './client'),
  entry: {
    js: './index.js',
    vendor: ['react']
  },
  output: {
    path: path.join(__dirname, './static'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.html$/,
        loader: 'file',
        query: {
          name: '[name].[ext]'
        }
      },
      {
        test: /\.scss$/,
        loaders: ['style', 'css', 'sass']
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loaders: ['babel-loader']
      },
      {
        test: /\.(png|jpg|gif|otf|eot|svg|ttf|woff|woff2)/,
        loader: 'url-loader'
      },
      {
        test: /\.(txt|json)/,
        loader: 'raw-loader'
      }
    ],
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
    modules: [
      path.resolve('./client'),
      'node_modules'
    ],
    alias: {
      stores: `${srcPath}/stores/`,
      components: `${srcPath}/components/`,
      services: `${srcPath}/services`,
      models: `${srcPath}/models`,
      constants: `${srcPath}/constants`,
      sources: `${srcPath}/sources`,
      images: `${srcPath}/assets/images`,
      appConstants: isProd ? `${srcPath}/constants/_prod` : `${srcPath}/constants/_dev`
    }
  },
  plugins: [
    new webpack.IgnorePlugin(/regenerator|nodent|js\-beautify/, /ajv/),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: Infinity,
      filename: 'vendor.bundle.js'
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: isProd
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: !isProd
      },
      output: {
        comments: !isProd
      },
      sourceMap: !isProd
    }),
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.optimize.OccurrenceOrderPlugin()
  ],
  devServer: {
    contentBase: './client',
    hot: true,
    port: 3000,
    historyApiFallback: true
  }
};

我的 "client" 文件夹 index.html 和我的其余代码都在与 webpack 配置相同的文件夹中。

Webpack 确实成功构建,但是转到 localhost:3000,我收到错误消息:"Cannot GET /"

转到 localhost:3000/client/index.html 确实为我的 index.html 服务,但我使用

插入的构建文件
<script src="./vendor.bundle.js"></script>
<script src="./bundle.js"></script>

未加载(GET 到“http://localhost:3000/client/bundle.js”导致 404)

有人知道发生了什么事吗?我无法解决这个问题,我想我已经尝试了所有方法,从更改路径、publicPath 到更改 contentBase 以及将我的静态文件移动到不同的文件夹。 很奇怪,这个问题是我重新安装项目依赖后才出现的

非常感谢您的每一点帮助。谢谢。

webpack-dev-server@2.1.0-beta.3 中存在问题,导致 contentBase 选项被忽略。您可以尝试升级到 2.1.0-beta.4 吗?刚刚发布。