使用create-react-app和react-loadable进行代码拆分不起作用

dvdsmpsn

我关注了多篇文章,尤其是这些...

这是代码拆分的示例-getRoutes()在组件的render方法中调用:

// Edit: commented out original `Loadable` abstraction to use standard `Loadable`

import React from 'react'
// import L from 'react-loadable'
import Loadable from 'react-loadable'
import { BrowserRouter, Route, Switch } from 'react-router-dom'

import { LoadingIndicator } from '../components'

// const Loadable = opts =>
//   L({
//     loading: LoadingIndicator,
//     delay: 300,
//     ...opts
//   })

const AuthenticateContainer = Loadable({
  loading: LoadingIndicator,
  loader: () => import(/* webpackChunkName: "auth" */ '../containers/Authenticate')
})

...

export default function getRoutes (isAuthed, browserHistory) {

  return (
    <BrowserRouter>
      <Switch>
        <Route path="/auth" component={AuthenticateContainer} />
        ...
      </Switch>
    </BrowserRouter>
  )
}

..但我的代码未拆分:

$ npm run build

> [email protected] build /path/to/my-app
> bash -ac '. .env.production; react-scripts build'

Creating an optimized production build...

File sizes after gzip:

  854.84 KB (+4 B)  build/static/js/main.1aa92927.js
  17.53 KB          build/static/css/main.36b767d9.css

The bundle size is significantly larger than recommended.
Consider reducing it with code splitting: https: // goo.gl/9VhYWB
You can also analyze the project dependencies: https: // goo.gl/LeUzfb

The project was built assuming it is hosted at the server root.
You can control this with the homepage field in your package.json.
For example, add this to build it for GitHub Pages:

  "homepage" : "http: // myname.github.io/myapp",

The build folder is ready to be deployed.
You may serve it with a static server:

  yarn global add serve
  serve -s build

Find out more about deployment here:

  http: // bit.ly/2vY88Kr

如您所见,构建结果不是分块的,而是单个JS文件。

我究竟做错了什么?


当前使用的版本:

react-scripts: 1.1.4
react-router-dom: 4.3.1
webpack: 3.8.1
npm: 6.1.0
dvdsmpsn

好吧,我最终解决了这个问题...

我有一个/src/containers/index.js包含以下行的文件

export { default as AuthenticateContainer } from './Authenticate/AuthenticateContainer'

这使我可以AuthenticateContainer使用以下速记来导入我的代码:

import { AuthenticateContainer } from '../containers'

而不是稍长

import AuthenticateContainer from '../containers/Authenticate/AuthenticateContainer'

但是,保留该行/src/containers/index.js还可以防止代码分裂,因为在我们甚至尝试异步加载之前AuthenticateContainer逻辑已经被捆绑到一起main.js

删除行,并按预期进行代码拆分。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用WebSocket的React Native不起作用

使用Jest和create-react-app测试React异步

使用Create React App进行React样式

TypeError:使用react-create-app玩笑和酶进行测试时,调度不是函数

简单的React代码段不起作用

引导程序和导航栏崩溃不起作用做出反应(create-react-app)

React Router路由在Nginx create-react-app上不起作用

Sass在create-react-app中不起作用

使用React Children代码示例不起作用

async / await在reactjs中不起作用(使用npx create-react-app)

我应该使用react-loadable或loadable-components进行代码拆分吗?

使用sass时,autoprefixer在create-react-app中不起作用

create-react-app:使用--scripts-version和--typescript

使用Loadable从react-loadable拆分代码会导致屏幕闪烁

使用React和create-react-app动态导入

npx create react-app命令不起作用

使用Jest进行React,TypeScript和RxJS测试不起作用

使用React.lazy,Suspense和react-router-dom进行代码拆分不起作用

如何使用Create React App和TypeScript启用可选链接

使用React 0.59但挂钩不起作用

Sass ID选择器在React和create-react-library中不起作用

在create-react-app后React npm start不起作用

使用Rails中的React进行组件渲染不起作用

React路由在Facebook的create-react-app构建中不起作用

create-react-app 和 Sass 似乎不起作用

升级 npm 和 nodejs 后 create-react-app 不起作用

成功安装 create-react-app 后,Npm start 将不起作用。错误代码 Enoent 错误号 -4058

React - 使用 onChange 和 onKeyDown 时输入不起作用

如何在 react.js 中使用 react spring hook。基本代码不起作用