简体   繁体   English

React / Webpack / TypeScript-无法动态导入组件

[英]React / Webpack / TypeScript - components not dynamically imported

I have dynamic imports working perfectly fine for node_modules, however I am unable to get this to work for React components. 我有动态导入对node_modules来说工作得很好,但是我无法使它对React组件起作用。 These are just compiled into the file that is dependent on them. 它们只是被编译到依赖于它们的文件中。

I have no idea what could be the cause of this and don't know how I could debug the splitting. 我不知道这可能是什么原因,也不知道如何调试拆分。

Not sure if it's relevant, but the React component is larger than 30kb. 不确定是否相关,但是React组件大于30kb。

I did update my tsconfig.json to support code splitting: 我确实更新了tsconfig.json以支持代码拆分:

{
  "allowSyntheticDefaultImports": true,
  "target": "es5",
  "module": "esnext",
  "moduleResolution": "node",
}

I don't have anything of relevance configured for dynamic code splitting in my webpack.config.js besides maybe the output: 除了可能的输出外,我在webpack.config.js中没有配置任何与动态代码拆分相关的内容:

output: {
    path: path.join(__dirname, "wwwroot"),
    publicPath: "/wwwroot/",
    filename: "[name].js"
},

And this works perfectly fine for a node_module dependency which is asynchronously loaded: 对于异步加载的node_module依赖项,这工作得很好:

private async loadComponent(): Promise<void> {
    const NivoBar = await import(/* webpackChunkName: "nivo-bar" */ "@nivo/bar");
    this.setState({ bar: NivoBar.Bar });
}

However this does not work for my React component which is just compiled into the entry file: 但是,这不适用于我的React组件,后者只是被编译到了入口文件中:

private async loadComponent(): Promise<void> {
    const fup = await import(/* webpackChunkName: "fileuploadpopup" */ "portal/components/fileUpload/FileUploadPopup");
    this.setState({ fileUploadPopup: fup.default });
}

What could be the cause of this? 这可能是什么原因?

Turns out there was another component that had a (non-async) dependency to the same component which is why webpack decided to compile it. 事实证明,还有另一个组件与该组件具有(非异步)依赖性,这就是webpack决定对其进行编译的原因。 Still don't know how to properly debug this with webpack... 仍然不知道如何使用webpack正确调试...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM