简体   繁体   English

带有可加载反应的 Webpack 4 中的代码拆分和动态加载

[英]Code splitting and dynamic loading in Webpack 4 with react loadable

So I have a webpack 4 react app that has 2 parts:所以我有一个 webpack 4 react 应用程序,它有两个部分:

  • Login component登录组件
  • Rest of the app应用程序的其余部分

My routes.js looks like this:我的 routes.js 看起来像这样:

import React from 'react';
import Home from 'Home';

....

const LoadableLoginComponent = Loadable({
    loader: () => import('LoginComponent'),
    loading() {
        return <div>Loading...</div>
    }
});

.....

render() {
        if (!this.loggedIn) {
            return (
                <LoadableLoginComponent />
            );
        }

        return (
            <Home />
        );
    }

My webpack generates 2 js files bundle.min.js and 0.min.js我的 webpack 生成 2 个 js 文件 bundle.min.js 和 0.min.js

My assumption was that by default if the user is not logged in only the login component will load with 0.min.js and if user is logged in, the rest of the app will load but I see both bundle.min.js and 0.min.js being loaded.我的假设是,默认情况下,如果用户未登录,则只有登录组件将加载 0.min.js,如果用户已登录,则应用程序的其余部分将加载,但我看到 bundle.min.js 和 0 .min.js 正在加载。

If you have an if statement (!this.loggedIn) in the non-login bundle that has the possibility of rendering the login component, then the login component will be included in your non-login bundle.如果您在非登录包中有一个 if 语句 (!this.loggedIn) 可以呈现登录组件,那么登录组件将包含在您的非登录包中。 This is because the entry point for your non-login bundle searches all the possible code paths and files it might need to use and bundles them together, even if your if statement always fails and never uses the Login component.这是因为您的非登录包的入口点会搜索它可能需要使用的所有可能的代码路径和文件并将它们捆绑在一起,即使您的 if 语句总是失败并且从不使用登录组件。

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

相关问题 Webpack-通过webpack代码拆分和react-loadable防止文件重复 - Webpack - Prevent file duplication with webpack code splitting & react-loadable 使用Loadable从react-loadable拆分代码会导致屏幕闪烁 - code splitting with Loadable from react-loadable causes screen flicker 代码拆分/反应可加载问题 - Code splitting/react-loadable issue 使用React Loadable进行代码拆分并使用容器进行路由 - Code splitting with React Loadable and routes with containers webpack 2反应代码拆分 - webpack 2 react code splitting 使用webpack和react-router进行延迟加载和代码分割无法加载 - Using webpack and react-router for lazyloading and code-splitting not loading 使用create-react-app和react-loadable进行代码拆分不起作用 - Code splitting with create-react-app and react-loadable is not working 是否添加.default?:反应路由器3中的动态导入webpack代码拆分 - to add .default or not?: dynamic imports in react router 3 for webpack code-splitting 添加React.lazy或react-loadable并进行动态导入(代码拆分)会导致我的某些功能中断 - Adding React.lazy or react-loadable and doing dynamic import (Code splitting) causes some of my functionalities to breaks 使用React Loadable封装在HOC中的代码拆分路由组件 - Code splitting route components wrapped in a HOC with React Loadable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM