wepy开发微信小程序的优化之gulp打包

1 篇文章 0 订阅
1 篇文章 0 订阅

小程序限制每个包不得超过2M,经常写着写着就会发现预览的时候报错,文件超过2M,这时可用分包的方法来解决这问题

如图,分为pages,packageA,packageB,具体分包实现可看小程序文档

另外代码压缩也可以解决这问题,并且能优化项目,小程序开发工具自带了一个“上传代码时自动压缩”,可当你文件超过两M时,打开这个选项再进行预览,一样会报超过2M的错误,另外在wepy开发小程序时,打开这个选项上传的代码,会导致真机computed, props.sync 等等属性失效。所以如果你想压缩代码,那建议你还是根据项目路径,用gulp自己写个配置文件进行压缩,下面以我的项目代码为例:

项目结构如图:

该项目采用wepy进行开发,开发代码写在src下,开发时运行wepy build --watch,wepy会实时监听你对src文件的改动并将wpy文件编译成原声的js,json,wxss,wxml文件存入dist文件夹中,最后用gulp压缩文件到compress文件夹下

wepy编译完的文件夹将近5M,明显不太理想,这时就需要gulp来进行压缩啦,根据我dist文件夹下文件的路径,我写了个gulpfile.js配置文件

const gulp = require('gulp')
const del = require('del')
const autoprefixer = require('gulp-autoprefixer')
const htmlmin = require('gulp-htmlmin')
const sass = require('gulp-sass')
const jsonminify = require('gulp-jsonminify2')
const gutil = require('gulp-util')
const combiner = require('stream-combiner2');
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const rename = require("gulp-rename")
const minifycss = require('gulp-minify-css')
const runSequence = require('run-sequence')
const jsonlint = require("gulp-jsonlint")

var colors = gutil.colors;
const handleError = function(err) {
  console.log('\n')
  gutil.log(colors.red('Error!'))
  gutil.log('fileName: ' + colors.red(err.fileName))
  gutil.log('lineNumber: ' + colors.red(err.lineNumber))
  gutil.log('message: ' + err.message)
  gutil.log('plugin: ' + colors.yellow(err.plugin))
};

gulp.task('clean', () => {
  return del(['./compress/**'])
})



gulp.task('watch', () => {
  gulp.watch('./dist/**/*.json', ['jsonPro']);
  gulp.watch('./dist/images/**', ['images']);
  gulp.watch('./dist/packageA/images/**', ['imagesA']);
  gulp.watch('./dist/packageB/images/**', ['imagesB']);
  gulp.watch('./dist/**/*.wxml', ['templatesPro']);
  gulp.watch('./dist/**/*.wxss', ['wxssPro']);
  gulp.watch('./dist/npm/**/*.js', ['scriptsNPM']);
  gulp.watch('./dist/*.js', ['scriptsO']);
  gulp.watch('./dist/wxs/*.wxs', ['wxs']);
  gulp.watch(['./dist/**/*.js', '!./dist/npm/**','!./dist/*.js'], ['scripts']);
  gulp.watch('./dist/components/*.wxml', ['componentsML']);
  gulp.watch('./dist/components/*.json', ['componentsJSON']);
});



gulp.task('componentsJSON', () => {
  return gulp.src('./dist/components/*.json')
    .pipe(gulp.dest('./compress/components'))
})

gulp.task('componentsML', () => {
  return gulp.src('./dist/components/*.wxml')
    .pipe(gulp.dest('./compress/components'))
});


gulp.task('jsonLint', () => {
  var combined = combiner.obj([
    gulp.src(['./dist/**/*.json']),
    jsonlint(),
    jsonlint.reporter(),
    jsonlint.failAfterError()
  ]);

  combined.on('error', handleError);
});

gulp.task('json', ['jsonLint'], () => {
  return gulp.src('./dist/**/*.json')
    .pipe(gulp.dest('./compress'))
})

gulp.task('jsonPro', ['jsonLint'], () => {
  return gulp.src('./dist/**/*.json')
    .pipe(jsonminify())
    .pipe(gulp.dest('./compress'))
})

gulp.task('wxs', () => {
  return gulp.src('./dist/wxs/**')
    .pipe(gulp.dest('./compress/wxs'))
})

gulp.task('images', () => {
  return gulp.src('./dist/images/**')
    .pipe(gulp.dest('./compress/images'))
})

gulp.task('imagesA', () => {
  return gulp.src('./dist/packageA/images/**')
    .pipe(gulp.dest('./compress/packageA/images'))
})

gulp.task('imagesB', () => {
  return gulp.src('./dist/packageB/images/**')
    .pipe(gulp.dest('./compress/packageB/images'))
})

gulp.task('templatesPro', () => {
  return gulp.src('./dist/**/*.wxml')
    .pipe(gulp.dest('./compress'))
});

// 对于wxss的处理,发现处理后文件反而更大了
// gulp.task('wxss', () => {
//   var combined = combiner.obj([
//     gulp.src(['./dist/**/*.{wxss,sass}', '!./dist/styles/**']),
//     sass().on('error', sass.logError),
//     autoprefixer([
//       'iOS >= 8',
//       'Android >= 4.1'
//     ]),
//     rename((path) => path.extname = '.wxss'),
//     gulp.dest('./compress')
//   ]);

//   combined.on('error', handleError);
// });

gulp.task('wxssPro', () => {
  return gulp.src('./dist/**/*.wxss')
  .pipe(gulp.dest('./compress'))
});

gulp.task('scriptsO', () => {
  return gulp.src('./dist/*.js')
    .pipe(gulp.dest('./compress'))
})

gulp.task('scriptsNPM', () => {
  return gulp.src('./dist/npm/**/*.js')
    .pipe(gulp.dest('./compress/npm'))
})

gulp.task('scripts', () => {
  return gulp.src(['./dist/**/*.js', '!./dist/npm/**','!./dist/*.js'])
    .pipe(babel({
      presets: ['es2015']
    }))
    .pipe(uglify({
      compress: true,
    }))
    .pipe(gulp.dest('./compress'))
})
gulp.task('dev', ['clean'], () => {
  runSequence(  'jsonPro',
  'componentsML',
  'componentsJSON',
  'images',
  'imagesA',
  'imagesB',
  'templatesPro',
  'wxssPro',
  'scriptsO',
  'scriptsNPM',
  'scripts',
  'wxs',
  'watch');
});

gulp.task('compress', [
  'jsonPro',
  'componentsML',
  'componentsJSON',
  'images',
  'imagesA',
  'imagesB',
  'templatesPro',
  'wxssPro',
  'scriptsO',
  'scriptsNPM',
  'scripts',
  'wxs'
]);

gulp.task('pro', ['clean'], () => {
  runSequence('build');
})

package.json下配置:

正式部署时可以运行,npm run compress进行压缩

开发时可以运行 npm run compress-w,会监听dist文件的变化,实时压缩。

这是压缩后的文件夹:

可以发现压缩后的文件是压缩前的50%,这是可以直接用开发者工具打开compress文件夹,进行预览上传等操作

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wepy 是一个小程序组件化开发框架。 组件 小程序支持js模块化,但彼此独立,业务代码与交互事件仍需在页面处理。无法实现组件化的松耦合与复用的效果。 例如模板A中绑定一个bindtap="myclick",模板B中同样绑定一样bindtap="myclick",那么就会影响同一个页面事件。对于数据同样如此。因此只有通过改变变量或者事件方法,或者给其加不同前缀才能实现绑定不同事件或者不同数据。当页面复杂之后就十分不利于开发维护。 因此wepy让小程序支持组件化开发,组件的所有业务与功能在组件本身实现,组件与组件之间彼此隔离,上述例子在wepy的组件化开发过程中,A组件只会影响到A绑定的myclick,B也如此。 wepy编译组件的过程如下: 组件引用 当页面或者组件需要引入子组件时,需要在页面或者script中的components给组件分配唯一id,并且在template中添加标签,如index.wpy。 页面和组件都可以引入子组件,引入若干组件后,如下图: Index页面引入A,B,C三个组件,同时组件A和B又有自己的子组件D,E,F,G,H。 项目目录结构 ├── dist                   微信开发者工具指定的目录 ├── node_modules            ├── src                    代码编写的目录 |   ├── components         组件文件夹(非完整页面) |   |   ├── com_a.wpy      可复用组件 a |   |   └── com_b.wpy      可复用组件 b |   ├── pages              页面文件夹(完整页面) |   |   ├── index.wpy      页面 index |   |   └── page.wpy       页面 page |   └── app.wpy            小程序配置项(全局样式配置、声明钩子等) └── package.json           package 配置 主要解决问题 开发模式转换 支持组件化开发 支持加载外部 NPM 包 单文件模式,使得目录结构更加清晰 默认使用 babel 编译,支持 ES6/7 的一些新特性 针对原生 API 进行优化 标签:wepy  小程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值