webpack4兼容性中的历史api后备功能

时间:2018-03-31 01:01:10

标签: webpack ecmascript-6 react-router

我目前面临一个奇怪的问题,我刚从webpack 3中的应用程序转换到webpack 4。

我可以使用地址栏来处理这些工作:

$myapp/a/b$myapp/a/b/c$myapp/a/

但是一切都像: $myapp/a$myapp/b完美运作

在应用内,我可以使用历史记录从第一个类别到达路径。

我以这种方式启动我的应用程序:

webpack-dev-server --mode development --host 0.0.0.0 --history-api-fallback

我的webpack配置如下:

/* eslint-disable */
const path = require('path')
const resolve = path.resolve
const DotenvPlugin = require('webpack-dotenv-plugin')
const HtmlWebPackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: true }
          }
        ]
      },
      {
        test: /\.scss$/,
        use: [{
            loader: "style-loader"
        }, {
            loader: "css-loader"
        },
        {
          loader: "sass-loader",
          options: {
              includePaths: [__dirname]
          }
        }]
      },
      { test: /\.css$/,
        loader: "style-loader!css-loader",
        include: __dirname
      },
      {
        test: /\.(woff|woff2)$/,
        use: {
          loader: 'url-loader',
          options: {
            name: 'fonts/[hash].[ext]',
            limit: 5000,
            mimetype: 'application/font-woff'
          }
        }
      },
      {
        test: /\.(ttf|eot|svg|png)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: 'fonts/[hash].[ext]'
          }
        }
      }
    ]
  },
  resolve: {
      extensions: [".js"],
      alias: {
          ["~"]: resolve(__dirname)
      }
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: './src/index.html',
      filename: './index.html'
    }),
    new DotenvPlugin({
      sample: './.env.sample',
      path: './.env'
    })
  ]
}

在浏览器中,我收到以下错误消息: GET http://localhost:8081/path/main.js net::ERR_ABORTED d:1 Refused to execute script from 'http://localhost:8081/path/main.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

有人对此有所了解吗?

1 个答案:

答案 0 :(得分:1)

在输出中添加public path选项后解决,如下所示:

output: {
    filename: '[name].js',
    chunkFilename: '[name].chunk.js',
    publicPath: '/'
},