Dva: 请问Dva为什么所有的代码全部都到了 index.js 文件?有分块打包 demo 吗?

Created on 23 Dec 2016  ·  19Comments  ·  Source: dvajs/dva

请问Dva为什么所有的代码全部都到了 index.js 文件?有分块打包 demo 吗?

question

Most helpful comment

All 19 comments

我看了一下,这个打包是到同一个文件,并没有分离开来?

不是按需加载,是分块打包...

一个 entry 里要拆包,可以试试 CommonsChunkPlugin

我写过的 Vue 的项目,Vue 的项目是这样实现的:
路由:
const Login = r => require.ensure([], () => r(require('../components/login/Login.vue')), 'user') export default new Router({ mode: 'history', scrollBehavior: () => ({ y: 0 }), routes: [ { name: 'login', path: '/login', component: Login } ] })
webpack:
output: { path: config.build.assetsRoot, publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath, filename: '[name].js' },
但是在dva里配置这样并不生效

这不就是按需加载吗?

不是,它也实现了分块打包,比如我系统上有二个路由:a,b;
dva 项目中
无论访问 a 还是 b,请求的 index.js;都是包含 a 和 b 的逻辑代码的;
vue 项目中
访问 a 页面,请求的 js 是 a.index.js,访问 b 页面,请求的 js 是 b.index.js。是不相互包含的

https://github.com/dvajs/dva/blob/master/examples/dynamic-load/router.js#L6-L27
这段是用来实现你描述的 vue 项目的效果的。

我有看到这个动态的路由的代码,但是并没有实现 vue 项目中的想过,我现在的代码是这样的
`const Login = c => require.ensure([], require => (
app.model(require('./models/login/loginModel')),
c(null, require('./routes/login/Login'))
));

const Main = c => require.ensure([], require => (
app.model(require('./models/main/mainModel')),
c(null, require('./routes/main/Main'))
));

function interceptPermissions() {
const token = kits.getCookies('tf-token');
const uid = kits.getCookies('tf-uid');
if (!uid || !token) {
app._store.dispatch(routerRedux.replace('/login'))
}
}

return getComponent={(location, callback) => Login(callback)}/>
onenter={interceptPermissions()}
getComponent={(location, callback) => Main(callback)}>




`
并没有实现,无论是run start 还是 build 所有的代码都输出到了一个 index.js 文件

mark

mark

mark

https://github.com/dvajs/dva/tree/master/examples/dynamic-load
被移走了,按需加载的dva配置我可以从哪里看到?谢谢

mark

@AlaiNiGuests 你地址里的文档,我看了一下,应该已经不能在dva 2.x以上使用了吧?
那个react-router的语法,是3.x版本的还是4.x版本的?

mark

楼主是怎么解决的? 能贴下代码吗? 万分感谢!

Was this page helpful?
0 / 5 - 0 ratings