gulpfile.js 1.2 KB
const gulp = require('gulp')
const rename = require('gulp-rename')
const less = require('gulp-less')
const ts = require('gulp-typescript')

const dist = 'miniprogram_dist'
const demoDir = 'demo'
const lessFiles = ['./src/index.less']
const tsFiles = ['./src/*.ts']

const tsProject = ts.createProject('tsconfig.json')

function buildts() {
  return gulp.src(tsFiles)
    .pipe(tsProject())
    .pipe(gulp.dest(dist))
}

function buildless() {
  return gulp.src(lessFiles)
    .pipe(less())
    .pipe(rename({extname: '.wxss'}))
    .pipe(gulp.dest(dist))
}

function cp() {
  return gulp.src([
    './src/index.json',
    './src/index.wxml'
  ]).pipe(gulp.dest(dist))
}

function demo() {
  return gulp.src(`${dist}/**/*`).pipe(gulp.dest(`${demoDir}/components`))
}

function watchless() {
  return gulp.watch(lessFiles, gulp.series(buildless, cp, demo))
}

function watchts() {
  return gulp.watch(tsFiles, gulp.series(buildts, cp, demo))
}

function watch() {
  return gulp.watch([
    'src/**/*',
    ...[...lessFiles, ...tsFiles].map(v => `!${v}`)
  ], gulp.series(cp, demo))
}

exports.build = gulp.series(gulp.parallel(buildless, buildts), cp, demo)
exports.default = gulp.parallel(exports.build, watch, watchless, watchts)