Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

打包后文件太大怎么办 #675

Closed
liuxingling opened this issue Mar 6, 2017 · 15 comments
Closed

打包后文件太大怎么办 #675

liuxingling opened this issue Mar 6, 2017 · 15 comments
Labels

Comments

@liuxingling
Copy link

image
这是我的roadhog文件,打包出来后的文件平均八百多kb,太大了啊,求指教

image

@sorrycc
Copy link
Member

sorrycc commented Mar 7, 2017

roadhog build --analyze 分析下打包文件构成看。

@liuxingling
Copy link
Author

@sorrycc 我是还要安装什么东西吗? 通过浏览器看,有很多依赖的js都打包进去了
image

image

@sorrycc
Copy link
Member

sorrycc commented Mar 7, 2017

npm i roadhog -g

@liuxingling
Copy link
Author

liuxingling commented Mar 7, 2017

@sorrycc 大神,我看不懂。。。 附上我的package.json文件
image
image

@liuxingling
Copy link
Author

@sorrycc 分析了下 是引用了moment导致文件这么大,怎么样才能解决这个问题啊

@sorrycc
Copy link
Member

sorrycc commented Mar 16, 2017

参考这个给 moment 加 noParse 试试。

@sorrycc sorrycc closed this as completed Mar 16, 2017
@iceshu
Copy link

iceshu commented Apr 8, 2017

@liuxingling 你是怎么打包成多个js文件的啊?我每次打包都是一个index.js 文件

@xufei
Copy link
Contributor

xufei commented Apr 9, 2017

@iceshu
Copy link

iceshu commented Apr 10, 2017

@xufei 多谢啊~ 还有个问题啊 弱弱的问句就是打包出来的时候是1.1.js 2.2.js 更换这些名字是在哪配置的啊,我用的是roadhog

@K-walker
Copy link

K-walker commented May 8, 2017

@iceshu 哥们,你好,我也遇到跟你一样的问题,打包后实际文件大于打包时输出的大小,请问你是怎么解决的啊,能不能分享解决的方法啊,我按照上面给的方法,通过require.ensure 去加载,但是没有任何效果

@iceshu
Copy link

iceshu commented May 10, 2017

@K-walker 可以的啊
主路由

    const rootRoute = [
        {
            path: '/',
            indexRoute: {onEnter: (nextState, replace) => replace('home')},
            component: require('./routes/home/pages/index'),
            getChildRoutes(partialNextState, cb) {
                require.ensure([], (require) => {
                    cb(null, [
                        require('./routes/home/router')(app),
                        require('./routes/competing/router')(app),
                        require('./routes/buynum/router')(app),
                        require('./routes/myinfo/router')(app),
                        {
                            path: 'login',
                            getComponent(nextState, cb) {
                                registerModel(app, require('./models/pages/login'));
                                require.ensure([], (require) => {
                                    cb(null, require('./routes/myinfo/pages/login'))
                                }, 'login')
                            }
                        },
                        {
                            path: 'register',
                            getComponent(nextState, cb) {
                                registerModel(app, require('./models/pages/register'));
                                require.ensure([], (require) => {
                                    cb(null, require('./routes/myinfo/pages/register'))
                                }, 'register')
                            }
                        }, {
                            path: 'getpwd',
                            getComponent(nextState, cb) {
                                require.ensure([], (require) => {
                                    registerModel(app, require('./models/pages/myinfo-security'));
                                    cb(null, require('./routes/myinfo/pages/getpwd'))
                                }, 'getpwd')
                            }
                        },
                        {
                            path: '*',
                            getComponent(nextState, cb) {
                                require.ensure([], (require) => {
                                    cb(null, require('./routes/myinfo/pages/pagenotfound'))
                                }, 'getpwd')
                            }
                        }
                    ]);
                });
            },
        }
    ];

    return <Router history={history} routes={rootRoute}/>;

自路由home

export default (app)=> ({
    path: 'home',
    component: require('../../routes/home/pages/index'),
    indexRoute: {onEnter: (nextState, replace) => replace('home/index')},
    getChildRoutes(partialNextState, cb) {
        require.ensure([], (require) => {
            registerModel(app, require('../../models/home/home'));
            registerModel(app, require('../../models/home/lotterys'));
            cb(null, [
                {
                    path: 'index',
                    component: require('./pages/home'),
                },
                {
                    path: 'listshow',
                    component: require('./pages/listshow'),
                },
                {
                    path: 'listshowinfo',
                    component: require('./pages/listsshowinfo'),
                },
                {
                    path: 'lotterylist',
                    component: require('./pages/lotterylist'),
                },
                {
                    path: 'zxlists',
                    component: require('./pages/zxlists'),
                },
                {
                    path: 'zxinfo',
                    component: require('./pages/zxinfo'),
                }
            ]);
        });
    },
});

@K-walker
Copy link

多谢,好像是减小了包的大小,还有个问题,不知道能不能请教下如何不把 antd.css 打进去,因为这个样式属于官方的,我其实不用对其修改的,我只要放在本地就可以了,然后引用,但是如果打包进去也会占用包的大小,再比如jquery,如果用到了话,我也可以不用将其打包,放在本地引用,这样就减小了包的大小,我只需要打包我写的jsx文件即可,不知道有没有这样的方法!

@shuxiaotai
Copy link

@iceshu 你好,请问你知道更换文件名是在哪里配置了吗

@iceshu
Copy link

iceshu commented Aug 15, 2018

@K-walker .roadhogrc.js 里配置externals 例子大概是这样
externals: {
"react": 'React',
'react-dom': 'ReactDOM',
"redux": 'Redux',
'redux-saga': 'ReduxSaga',
'raven-js': 'Raven',
}
然后这些用url 引用下就行了
antd.css的话 官网有插件 引用import{XXX} from antd 这样只会打包 单个组件的样式跟js

@iceshu
Copy link

iceshu commented Aug 15, 2018

@shuxiaotai require.ensure([], (require) => {
cb(null, require('./routes/myinfo/pages/pagenotfound'))
}, 'getpwd') getpwd 就是 文件名

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants