官方示例

image.png

民间自定义配置示例

  1. /* eslint-disable no-console */
  2. const path = require('path');
  3. const gulp = require('gulp');
  4. const less = require('gulp-less');
  5. const rename = require('gulp-rename');
  6. const cleanCss = require('gulp-clean-css');
  7. const babel = require('gulp-babel');
  8. const gulpif = require('gulp-if');
  9. const isProduction = process.env.NODE_ENV === 'production';
  10. const dist = isProduction
  11. ? path.join(__dirname, '../es')
  12. : path.join(__dirname, '../demo/es');
  13. const src = path.join(__dirname, '../src');
  14. const extTypes = ['ts', 'less', 'json', 'axml', 'sjs'];
  15. gulp.task('less', () =>
  16. gulp
  17. .src(`${src}/**/*.less`) // 读取src目录下所有的less文件
  18. .pipe(
  19. less({
  20. modifyVars: {
  21. '@pixelSize': '1rpx',
  22. },
  23. }) // 采用less的默认方式处理less产出css
  24. )
  25. .on('error', e => console.error(e)) // 监听执行task过程中产生的错误
  26. .pipe(gulpif(isProduction, cleanCss())) // 压缩css
  27. .pipe(
  28. rename({
  29. extname: '.acss',
  30. }) // 重命名
  31. )
  32. .pipe(gulp.dest(dist)) //输出到dist文件夹下
  33. );
  34. gulp.task('ts', () =>
  35. gulp
  36. .src(`${src}/**/*.ts`) // 读取src目录下所有的ts文件
  37. .pipe(babel()) //将ts文件转译为es6的js文件
  38. .on('error', (err) => {
  39. console.log(err);
  40. })
  41. .pipe(gulp.dest(dist))
  42. );
  43. gulp.task('json', () => gulp.src(`${src}/**/*.json`).pipe(gulp.dest(dist)));
  44. gulp.task('axml', () => gulp.src(`${src}/**/*.axml`).pipe(gulp.dest(dist)));
  45. gulp.task('sjs', () => gulp.src(`${src}/**/*.sjs`).pipe(gulp.dest(dist)));
  46. const build = gulp.series(...extTypes);
  47. build();
  48. if (!isProduction) {
  49. extTypes.forEach((type) => {
  50. const watcher = gulp.watch(`${src}/**/*${type}`, gulp.series(type)); // 监听src目录下'ts', 'less', 'json', 'axml', 'sjs'等文件的change
  51. watcher.on('change', (event) => {
  52. console.log(`File ${event} was change`); // 打印日志
  53. });
  54. watcher.on('add', (event) => {
  55. console.log(`File ${event} was add`);
  56. });
  57. watcher.on('unlink', (event) => {
  58. console.log(`File ${event} was remove`);
  59. });
  60. });
  61. }