Reactjs 我的模块未被带有React.lazy的webpack typescript分块
我使用动态路由(react-router-dom),如下所述 路线Reactjs 我的模块未被带有React.lazy的webpack typescript分块,reactjs,typescript,lazy-loading,react-router-dom,webpack-4,Reactjs,Typescript,Lazy Loading,React Router Dom,Webpack 4,我使用动态路由(react-router-dom),如下所述 路线 const Home = React.lazy(() => import("../../home")); const Page2Component = React.lazy(() => import("../../page2Component")); const ROUTES: RoutesInterface[] = [ { id: &qu
const Home = React.lazy(() => import("../../home"));
const Page2Component = React.lazy(() => import("../../page2Component"));
const ROUTES: RoutesInterface[] = [
{
id: "home",
to: HOME_URL,
exact: true,
type: RouteType.ROUTE,
Component: Home,
},
{
id: "page_2",
to: PAGE_2_URL,
exact: true,
type: RouteType.ROUTE,
// @ts-ignore
Component: Page2Component
}]
路由器映射组件
import ROUTES from "./ducks/routes";
export default class Routes extends React.Component<RoutesPropsInterface> {
constructor(props: RoutesPropsInterface) {
super(props);
}
public render(): JSX.Element {
const { meta, analytics } = this.props;
const componentProps = {
analytics,
appmeta: meta,
};
return (
<Router>
<Suspense fallback={<span>Loading...</span>}>
<NavigationWithRouter />
<Switch>
{ROUTES.map(({ id, to, from, type, exact, Component }: RoutesInterface) =>
type === RouteType.ROUTE ?
<Route
key={id}
path={to}
exact={exact}
render={(routeProps: RouteComponentProps) => <Component
{...componentProps}
{...routeProps}
/>}
/> :
<Redirect
key={id}
from={from}
to={to}
exact={exact}
/>
)}
</Switch>
</Suspense>
</Router>
);
}
}
从“/ducks/ROUTES”导入路由;
导出默认类路由扩展React.Component{
构造器(道具:RoutesPropsInterface){
超级(道具);
}
public render():JSX.Element{
const{meta,analytics}=this.props;
常量组件props={
分析,
appmeta:meta,
};
返回(
{ROUTES.map({id,to,from,type,exact,Component}:routeInterface)=>
类型===RouteType.ROUTE?
}
/> :
)}
);
}
}
当我在映射之前调试路由时,我可以看到它即将映射延迟加载的组件
然而,当我检查最终的捆绑代码时,它只包含了我的vendors
和srcmain
为什么懒惰的邦德林在我的情况下不在这里工作?由于webpack4支持lazy配置,因此没有其他配置
解决方案
module:“commonjs
时,需要对我的tsconfig使用module:“esnext”
绝对救生员,你应该把“解决方案”作为答案。绝对救生员,你应该把“解决方案”作为答案。