const gulp = require('gulp')
const clean = require('gulp-clean')

const config = require('./tools/config')
const BuildTask = require('./tools/build')
const id = require('./package.json').name || 'miniprogram-custom-component'
const postcss = require('gulp-postcss')
const pxtorem = require('postcss-pxtransform')
// 构建任务实例
// eslint-disable-next-line no-new
new BuildTask(id, config.entry)

// 清空生成目录和文件
gulp.task('clean', gulp.series(() => gulp.src(config.distPath, {read: false, allowEmpty: true}).pipe(clean()), done => {
  if (config.isDev) {
    return gulp.src(config.demoDist, {read: false, allowEmpty: true})
      .pipe(clean())
  }

  return done()
}))
// 监听文件变化并进行开发模式构建
gulp.task('watch', gulp.series(`${id}-watch`))
// 开发模式构建
gulp.task('dev', gulp.series(`${id}-dev`))
// 生产模式构建
gulp.task('default', gulp.series(`${id}-default`))
// 将miniprogram_dist中样式px转为rpx
const deviceRatio = {
  '640': 2.34 / 2,
  '750': 1 / 2,
  '828': 1.81 / 2
}
gulp.task('css', function () {
  const processors = [
    pxtorem({
      platform: 'weapp',
      designWidth: 750,
      deviceRatio
    })
  ]
  return gulp.src(['miniprogram_dist/**/**/*.wxss'])
    .pipe(postcss(processors))
    .pipe(gulp.dest('miniprogram_dist/'))
})
