【问题标题】:History api fallback feature in webpack4 compatibilitywebpack4 兼容性中的历史 api 回退功能
【发布时间】:2018-09-10 00:20:15
【问题描述】:

我目前面临一个奇怪的问题,我刚刚从 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.

有人对此有想法吗?

【问题讨论】:

    标签: webpack ecmascript-6 react-router


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-04-30
      • 1970-01-01
      • 2013-11-21
      • 2017-12-05
      • 2015-07-01
      • 2017-10-13
      • 2016-04-18
      • 1970-01-01
      • 2010-11-06
      相关资源
      最近更新 更多