这是我的设置:
const DesktopApp = lazy(() => import(/* webpackChunkName: "DesktopApp" */'./DesktopApp'));
const MobileApp = lazy(() => import(/* webpackChunkName: "MobileApp" */'./MobileApp'));
type Props = { shouldServeMobile: boolean };
export const App = ({ shouldServeMobile }: Props): JSX.Element => (
shouldServeMobile
? (
<Suspense fallback={<AppLoading />}>
<MobileApp />
</Suspense>
) : (
<Suspense fallback={<AppLoading />}>
{/* GlobalDesktopStyle is injected in multiple places due to a bug where the
theme gets reset when lazy loading via React.Lazy + webpack */}
<GlobalDesktopStyle />
<DesktopApp />
</Suspense>
)
);
这是由webpack-dev-server
具有以下配置的 a 加载的:
devServer: {
contentBase: paths.output.path,
// this host value allows devices on a LAN to connect to the dev server
host: '0.0.0.0',
https: true,
port: 9001,
hotOnly: true,
// lets any URL work
historyApiFallback: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
现在,假设我们正在渲染
import { hot } from 'react-hot-loader/root';
// some imports omitted
const HotApp = hot(App);
ReactDOM.render(
<HotApp shouldServeMobile={true} />,
document.getElementById('root')
);
在初始加载时,这可以正常工作。MobileApp
块下载而没有DesktopApp
。但是,只要我更改组件中的任何数据并HMR
启动 - 重新加载就会下载DesktopApp
块。
这显然违背了代码拆分的目的。有谁知道如何阻止这种情况发生?
需要明确的是:我已经输出console.log(shouldServeMobile)
并且它始终是true
. 另外,我在这里尝试了这个建议:Webpack-dev-server 在每次更改后都会发出所有块,但它根本没有帮助。