dva icon indicating copy to clipboard operation
dva copied to clipboard

dva router ensure 拆分代码无效

Open iceshu opened this issue 9 years ago • 3 comments

问题描述:react router ensure 配置 无法拆分代码 代码配置:

function RouterConfig({history, app}) {
    const rootRoute = [
        {
            path: '/',
            name: 'home',
            indexRoute: {onEnter: (nextState, replace) => replace('home')},
            getComponents(location, cb){
                require.ensure(['./routes/IndexPage'], (require)=> {
                    cb(null, require('./routes/IndexPage'))
                })
            }
        },
        {
            path: '/home',
            getComponent(nextState, cb) {
                require.ensure([], (require) => {
                    cb(null, require('./routes/IndexPage'))
                }, 'home')
            }
        }, {
            path: '/activites',
            getComponent(nextState, cb) {
                require.ensure([], (require) => {
                    cb(null, require('./routes/home/activities'))
                }, 'activites')
            }
        }, {
            path: '/login',
            getComponent(nextState, cb) {
                require.ensure([], (require) => {
                    cb(null, require('./routes/home/login'))
                }, 'login')
            }
        }
    ];
    return <Router history={history} routes={rootRoute}/>;

}

以前做的那个项目是可以拆分的 这次的新项目却无法拆分 打包始终都是index.js 包删了重新安装还是这样 不知道问题出在哪 "roadhog": "^0.6.0"

iceshu avatar May 24 '17 02:05 iceshu

好了 解决了 具体原因我真心母鸡啊,我安装了 react router loader 然后在roadhog里配置了下 ,路由配置还是jsx那样写不需要ensure 异步写法

{
                test: /\.js$/,
                include: /src\/routes/, // 我的页面文件放的地方 src/routes
                loaders: ['react-router?name=routes/[name]', 'babel'],  // 打包之后生成 在dist/routes 里
            }, 

然后结果是这样的(需要路霸的lib webpack配置文件里 chunkFilename: '[id].async.js' 改成 chunkFilename: '[name].js') http://common-1253410441.costj.myqcloud.com/coverimg/WechatIMG164.jpeg

iceshu avatar May 24 '17 07:05 iceshu

I do like you say,but I still can't resolve it......................

DelTheCode avatar Jun 22 '18 04:06 DelTheCode

just npm install react-router-loader and then find the configfile in node_modules/roadhog/lib/config/webpack.config.prod add

{
                test: /\.js$/,
                include: /src\/routes/, // 我的页面文件放的地方 src/routes
                loaders: ['react-router?name=routes/[name]', 'babel'],  // 打包之后生成 在dist/routes 里
            }, 

chunkFilename: '[id].async.js' modification chunkFilename: '[name].js')

now you just npm run build to try

iceshu avatar Jun 22 '18 06:06 iceshu