web程序员

小程序代码打包处理

2018-11-29  本文已影响0人  pruple_Boy

首先感谢:冰星寒水 - 微信小程序——gulp处理文件 文章

前言


因为项目原因,谈一下老生常谈的问题,前端虽然没有代理处理虽然是防君子不防小人局面,不过把es6以后的代码babel一下到es5,我发现我自己都难“李德顺”了。babel转js后,把所有的文件分别压缩一下,得到一份新的小程序代码。比较初级的做法,给自己留下个备忘录,大神勿喷。。。

方式


之前有幸在项目使用了gulp,一直有一个用gulp构建小程序的想法,今天趁此机会刚好实践一把。通过gulp实现代码处理,如下是必须的配置文件,放在小程序根目录下,然后将小程序的所有文件移至src下

const gulp = require('gulp')

const SRC_DIR = './src/**/*'
const DIST_DIR = './dist/'

// Load plugins
const $ = require('gulp-load-plugins')()

// watch
gulp.task('watch', () => {
  gulp.watch(SRC_DIR + '.js', ['jsmin'])
  gulp.watch(SRC_DIR + '.json', ['jsonmin'])
  gulp.watch('./src/assets/**', ['assets'])
  gulp.watch(SRC_DIR + '.wxml', ['wxmlmin'])
  gulp.watch(SRC_DIR + '.wxss', ['wxssmin'])
})

// js
gulp.task('jsmin', () => {
  return gulp
    .src(SRC_DIR + '.js')
    .pipe(
      $.babel({
        presets: ['es2015']
      })
    )
    .pipe(
      $.uglify({
        compress: true
      })
    )
    .pipe(gulp.dest(DIST_DIR))
})

// json
gulp.task('jsonmin', () => {
  return gulp
    .src(SRC_DIR + '.json')
    .pipe($.jsonminify2())
    .pipe(gulp.dest(DIST_DIR))
})

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

// wxml
gulp.task('wxmlmin', () => {
  return gulp
    .src(SRC_DIR + '.wxml')
    .pipe(
      $.htmlmin({
        collapseWhitespace: true,
        removeComments: true,
        keepClosingSlash: true
      })
    )
    .pipe(gulp.dest(DIST_DIR))
})

// wxss
gulp.task('wxssmin', () => {
    return gulp.src(SRC_DIR + '.wxss')
    .pipe($.autoprefixer(['iOS >= 8', 'Android >= 4.1']))
    .pipe($.rename(path => (path.extname = '.wxss')))
    .pipe($.minifyCss())
    .pipe(gulp.dest(DIST_DIR))
})

// build
gulp.task('build', [
  'jsmin',
  'jsonmin',
  'assets',
  'wxmlmin',
  'wxssmin'
]);

// no console
gulp.task("nodebug", () => {
  return gulp
    .src('./dist/**/*.js')
    .pipe($.stripDebug())
    .pipe(gulp.dest(DIST_DIR))
})

// publish - 不是gulp插件
const runSequence = require('run-sequence')
gulp.task('product', ['clean'], () => {
  // 顺序执行
  runSequence('build', 'nodebug');
})

// clean - 不是gulp插件
const del = require('del')
gulp.task('clean', () => {
  return del([DIST_DIR + '**'])
})
{
  "plugins": [
      "transform-strict-mode",
      "transform-es2015-modules-commonjs",
      "transform-es2015-spread",
      "transform-es2015-destructuring",
      "transform-es2015-parameters"
  ],
  "presets": [
      "es2015",
      "stage-0"
  ]
}
{
  "repository": {
    "type": "",
    "url": ""
  },
  "description": "deal applet codo by gulp",
  "license": "ISC",
  "dependencies": {
    "autoprefixer": "^6.6.0",
    "babel-eslint": "^7.1.1",
    "babel-preset-latest": "^6.16.0",
    "del": "^2.2.2",
    "gulp": "^3.9.1",
    "gulp-autoprefixer": "^3.1.1",
    "gulp-babel": "^6.1.2",
    "gulp-eslint": "^3.0.1",
    "gulp-htmlmin": "^3.0.0",
    "gulp-jsonlint": "^1.2.0",
    "gulp-jsonminify2": "^1.0.0",
    "gulp-minify-css": "^1.2.4",
    "gulp-postcss": "^6.2.0",
    "gulp-rename": "^1.2.2",
    "gulp-strip-debug": "^3.0.0",
    "gulp-uglify": "^2.0.0",
    "run-sequence": "^1.2.2"
  },
  "devDependencies": {
    "babel-preset-stage-0": "^6.24.1",
    "gulp-load-plugins": "^1.5.0"
  }
}

说明


备注


更新 - gulp 4.0 方案


{
  "repository": {
    "type": "",
    "url": ""
  },
  "description": "deal applet codo by gulp",
  "license": "ISC",
  "dependencies": {
    "gulp": "^4.0.0",
    "autoprefixer": "^6.6.0",
    "babel-eslint": "^7.1.1",
    "babel-preset-latest": "^6.16.0",
    "del": "^2.2.2",
    "gulp-autoprefixer": "^3.1.1",
    "gulp-babel": "^6.1.2",
    "gulp-eslint": "^3.0.1",
    "gulp-htmlmin": "^3.0.0",
    "gulp-jsonlint": "^1.2.0",
    "gulp-jsonminify2": "^1.0.0",
    "gulp-minify-css": "^1.2.4",
    "gulp-postcss": "^6.2.0",
    "gulp-rename": "^1.2.2",
    "gulp-strip-debug": "^3.0.0",
    "gulp-uglify": "^2.0.0"
  },
  "devDependencies": {
    "babel-preset-stage-0": "^6.24.1",
    "gulp-load-plugins": "^1.5.0"
  }
}
const gulp = require('gulp')

const SRC_DIR = './src/**/*'
const DIST_DIR = './dist/'

// Load plugins
const $ = require('gulp-load-plugins')()

// watch
gulp.task('watch', () => {
  gulp.watch(SRC_DIR + '.js', ['jsmin'])
  gulp.watch(SRC_DIR + '.json', ['jsonmin'])
  gulp.watch('./src/assets/**', ['assets'])
  gulp.watch(SRC_DIR + '.wxml', ['wxmlmin'])
  gulp.watch(SRC_DIR + '.wxss', ['wxssmin'])
})

// js
function jsmin() {
  return gulp
    .src(SRC_DIR + '.js')
    .pipe(
      $.babel({
        presets: ['es2015']
      })
    )
    .pipe(
      $.uglify({
        compress: true
      })
    )
    .pipe(gulp.dest(DIST_DIR))
}

// json
function jsonmin() {
  return gulp
    .src(SRC_DIR + '.json')
    .pipe($.jsonminify2())
    .pipe(gulp.dest(DIST_DIR))
}

// assets
function assets() {
  return gulp.src('./src/assets/**').pipe(gulp.dest('./dist/assets'))
}

// wxml
function wxmlmin() {
  return gulp
    .src(SRC_DIR + '.wxml')
    .pipe(
      $.htmlmin({
        collapseWhitespace: true,
        removeComments: true,
        keepClosingSlash: true
      })
    )
    .pipe(gulp.dest(DIST_DIR))
}

// wxss
function wxssmin() {
  return gulp
    .src(SRC_DIR + '.wxss')
    .pipe($.autoprefixer(['iOS >= 8', 'Android >= 4.1']))
    .pipe($.rename(path => (path.extname = '.wxss')))
    .pipe($.minifyCss())
    .pipe(gulp.dest(DIST_DIR))
}

// no console
function nodebug () {
  return gulp
    .src('./dist/**/*.js')
    .pipe($.stripDebug())
    .pipe(gulp.dest(DIST_DIR))
}

// build
gulp.task(
  'build',
  gulp.parallel(jsmin, jsonmin, assets, wxmlmin, wxssmin)
)

// publish
gulp.task('product', gulp.series(clean, "build", nodebug))

// clean - 不是gulp插件
const del = require('del')
function clean() {
  return del([DIST_DIR + '**'])
}

感谢:苏星河 - gulp 4.0 升级指南

  1. gulp.task 移除了三参数语法,现在不能使用数组来指定一个任务的依赖。gulp 4.0 加入了 gulp.series 和 gulp.parallel 来实现任务的串行化和并行化。
  2. 任务函数中,如果任务是同步的,需要使用 done 回调。这样做是为了让 gulp 知道你的任务何时完成。
  3. 如果任务是异步的,有多个方法可以标记任务完成:1. 回调,2. 返回流(Stream),3. 返回 Promise,4. 返回子进程。
  4. 将task通过 js 函数的提升特性,可以将一些task定义为函数,好处就是私有化,不过就不再是gulp的命令,需要通过主task来启动

神坑


2018.11.29 malk

上一篇下一篇

猜你喜欢

热点阅读