-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
MpVue打包vendor.js过大问题 #1757
Comments
前阵子做过类似的,希望对你有帮助 |
我这里遇到一些疑问需要您的帮助need && process.evn.common_chunk_report 其中 common_chunk_report 无法再项目中找到,请问这个是哪里的设置? |
@CitrusHan 木有啊,桑心,vendor 过大大几率情况是第三方依赖过多或引用方式不合适,举了几个例子推荐了一下下引用依赖要注意的地方,不然开发过程 vendor 过大无法在手机微信里调试。真项目本身代码有那么大的话估计是项目需求问题,比如想在小程序里做太多事情,太复杂的需求,或公共组件过多。 要不你发下你们项目依赖列表我们来看看?比如 package.json 文件里的。 |
加wx吧,你微信多少我加你 |
我新建了一个干净的项目文件,对比了一下两个项目的依赖库除了dependencies的比新建的多几个,devDependencies中的基本差不多,不知道这会不是影响我vendor过大的原因? |
@CitrusHan 我试了你的这几个额外的包, vendor 并没有超过 300k, 可能不是第三方依赖的问题 |
@shrekuu dayjs 还缺啥你需要的功能呢? |
@iamkun 方便的时间段处理 |
@shrekuu 能具体一点吗 或者 demo code 之类的 |
处理思路与@bojueWjt 类似,首先需要用BundleAnalyzerPlugin分析vendor体积的组成。如果是node_modules体积过大,可以参考bojueWjt的帖子进行优化。(mpvue 初始化的项目已集成BundleAnalyzerPlugin, npm run build的时候加上 --report 参数即可开启 我们遇到的情况是分包的公共代码被意外打包到主包里的vendor.js里了,改成分包公共代码仅提取到分包内解决。 首先vendor.js代码来自CommonsChunkPlugin, mpvue默认的配置如下: new webpack.optimize.CommonsChunkPlugin({
name: 'common/vendor',
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf('node_modules') >= 0
) || count > 1
}
}), 可以看到只要是node_moduels里的js,或者引用次数>1的文件都会被提取进common/vendor。 查阅CommonsChunkPlugin文档, 发现有一个chunks参数,可以指定从哪些chunks提取公共代码,而mpvue里其实每个页面都是一个chunks,那么只要按照app.json里的分包规则加入多个CommonsChunkPlugin即可 参考代码: const appJson = require('../src/app.json')
let subpackages = {}
if (appJson.subpackages) {
appJson.subpackages.forEach(package => {
subpackages[package.root] = []
package.pages.forEach(subPage => subpackages[package.root].push(path.join(package.root, subPage)))
})
}
const subPackageCommonChunks = Object.keys(subpackages).map(sub =>
new webpack.optimize.CommonsChunkPlugin({
name: path.join(sub, 'common'),
chunks: subpackages[sub],
minChunks: 2,
})
) 最后加在原本的commonsChunksPlugin前即可: var webpackConfig = merge(baseWebpackConfig, {
// 省略中间代码
...subPackageCommonChunks,
new webpack.optimize.CommonsChunkPlugin({
name: 'common/vendor',
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf('node_modules') >= 0
) || count > 1
}
}),
}) |
好吧, 我举个栗子 const fromDate = moment('2019-01-01 00:00:00')
const toDate = moment('2019-01-12 00:12:00')
// 时间段 10 天 21 小时 12 分钟
const duration = moment.duration(toDate.diff(fromDate))
// 时间段的天部分
const dayPart = duration.days() // 10
小时部分
const hourPart = duration.hours() // 21
// 分钟部分
const minutePart = duration.minutes() // 12
// 共多少小时
const asHours = duration.duration.asHours() // 261.2 以上处理在 dayjs 里就木有, |
@shrekuu iamkun/dayjs#564 谢谢 请关注这里的进度 很快就会有了 😁 |
这个有效,推荐大家 |
是有效的,在windows下需要把 |
放心吧,我已经收到啦。
|
npm run dev 开发时构建在已经精简代码and图片、分包处理后、无丑化代码打包后vendor.js过大,尝试给vendor.js做了分包处理,这里参考了MpVue打包vendor过大问题解决办法,但是没有成功,请问大家还有好的建议吗?
The text was updated successfully, but these errors were encountered: