gpt4 book ai didi

reactjs - Webpack Code Splitting 'Loading chunk failed' 错误文件路径错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:39:23 25 4
gpt4 key购买 nike

我正在将 React + Typescript 与 Webpack 一起使用,我正在尝试在实际需要时加载我的一些 React 组件。

问题是,当通过延迟加载请求 block 时,我收到以下错误:

Uncaught Error: Loading chunk 1 failed.(error: http://localhost:58988/1.chunk.js)at HTMLScriptElement.onScriptComplete (bootstrap:114)

这个chunk生成成功,没有任何错误,只是路径不对 - http://localhost:58988/1.chunk.js 应该是http://localhost:58988/dist/1.chunk.js

我也尝试过使用最新的 React.lazy 来延迟加载 React 组件,但我遇到了同样的问题。那么,有没有办法告诉编译器如何解析这些文件路径呢?

部分代码如下:

MyComponent.tsx

import * as React from "react";
import * as ES5Promise from "promise";

export class MyComponent extends React.Component<{}, {}> {
constructor(props) {
super(props);
}

private readonly onLoadModuleClickHandler = (): void => {
import("./Button").then((module) => {
console.log(module);
});
}

render() {
return (
<div>
<input type="button" value="Load another module" onClick={this.onLoadModuleClickHandler}/>
</div>
);
}
}

tsconfig.json

{
"compilerOptions": {
"moduleResolution": "node",
"noImplicitAny": false,
"noEmitOnError": true,
"module": "esnext",
"removeComments": false,
"sourceMap": false,
"target": "es5",
"jsx": "react",
"noEmit": true,
"importHelpers": true,
"lib": ["dom", "es5", "es2015.promise"]
},
"exclude": [
"node_modules",
"wwwroot"
]
}

webpack.config.js

module.exports = {
entry: {
"app": "./src/App.tsx"
},
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: "[name].bundle.js",
chunkFilename: "[name].chunk.js"
},
module: {
rules: [
{
test: /\.(ts|tsx)?$/,
use: "awesome-typescript-loader",
exclude: /node_modules/
},
{
test: /\.(css|less)?$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader?modules&localIdentName=[local]--[hash:base64:5]"
}, {
loader: "less-loader"
}]
},
]
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".css", ".less"]
}
};

最佳答案

发生这种情况是因为默认情况下 output.publicPath/

只需更新 output.publicPath 以指向您想要的位置 => /dist/

关于reactjs - Webpack Code Splitting 'Loading chunk failed' 错误文件路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com