使用React Router捆绑Webpack

一切正常,然后出于某种原因,我决定更新一些软件包。不好的举动。

我正在使用React.lazy和Webpack进行代码拆分和捆绑我的JavaScript。如果我位于根URL(http://localhost:9000/),那没有问题。它将加载http://localhost:9000/dist/24.js。但是,如果(使用react-router)我在应用程序中的任何位置,则说http://localhost:9000/page/category/,它将尝试加载http://localhost:9000/page/category/dist/26.js。显然那不是文件的保存位置。

我可以使用通配符app.get('*/dist/:js' ...来忽略生产中的问题,但是我的devserver无法做到这一点。我敢肯定有一种方法可以告诉它始终使用绝对路径,但是我不知道该怎么做。 我如何告诉它总是从http://localhost:9000/dist/*.js加载?

这是webpack.config.js的相关内容。

    output: {
      filename: '[name].js',chunkFilename: '[name].js',path: path.resolve(process.cwd(),'dist'),publicPath: 'dist/',},

我正在使用webpack 4.41.2,webpack-dev-server 3.9.0,react-router 5.1.2和react-hot-loader 4.12.16。如果您想知道为什么我使用process.cwd()而不是__dirname,那是因为我在少数应用程序中通过符号链接使用了此文件。

ccc287718265 回答:使用React Router捆绑Webpack

在任何<base href="/" />之前将index.html添加到<script/>

找到了here

本文链接:https://www.f2er.com/3141646.html

大家都在问