gulpfile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. const { src, dest, watch } = require("gulp");
  3. const postcss = require("gulp-postcss");
  4. const browserSync = require("browser-sync").create();
  5. const sass = require("gulp-sass");
  6. const babel = require('gulp-babel');
  7. var sourcemaps = require("gulp-sourcemaps"); //生成map文件
  8. //html
  9. const layout1 = require('layout1') //layout布局
  10. const frontMatter = require('./cr-front-matter.js'); //html模板处理
  11. let fileinclude = require('gulp-file-include'); //html 文件引用 if for。。。
  12. const htmlbeautify = require('gulp-html-beautify'); //美化html
  13. //postcss插件
  14. const clearfix = require('postcss-clearfix'); //clear: fix;
  15. const color_rgba_fallback = require('postcss-color-rgba-fallback'); //降级兼容ie
  16. const opacity = require('postcss-opacity'); //降级兼容ie
  17. const autoprefixer = require('autoprefixer'); //降级兼容ie
  18. const postcssShort = require('postcss-short'); //css简写
  19. const colorFunction = require("postcss-color-function") //color 函数
  20. const del = require('del');
  21. const color = require('colors-cli')
  22. // 定义路径
  23. const path = {
  24. html: ["src/pages/**/*.html","src/passport/**/*.html"],
  25. outHtml: "html",
  26. scss: "src/scss/*.scss",
  27. css: "mobileAssets/css",
  28. js: "mobileAssets/js/*.js"
  29. };
  30. //监听scss
  31. function watchScss() {
  32. const processors = [
  33. postcssShort,
  34. clearfix,
  35. color_rgba_fallback,
  36. opacity,
  37. colorFunction,
  38. autoprefixer
  39. ];
  40. return src(path.scss)
  41. .pipe(sourcemaps.init())
  42. //outputStyle: expanded:扩大 compressed:压缩
  43. .pipe(sass({ outputStyle: "expanded" }).on("error", sass.logError))
  44. .pipe(postcss(processors))
  45. .pipe(sourcemaps.write("./maps"))
  46. .pipe(dest(path.css))
  47. .pipe(browserSync.stream());
  48. }
  49. //监听html
  50. function watchHtml() {
  51. return src(path.html)
  52. .pipe(frontMatter({
  53. property: 'fm'
  54. }))
  55. .pipe(layout1.ejs('src/layout/layout.ejs'))
  56. .pipe(fileinclude({
  57. prefix: '@@',
  58. basepath: 'src/components/'
  59. }))
  60. .pipe(htmlbeautify())
  61. .pipe(dest(path.outHtml))
  62. .pipe(browserSync.stream());
  63. }
  64. //监听js
  65. function watchJs() {
  66. return src(path.js)
  67. // .pipe(sourcemaps.init())
  68. // .pipe(babel({
  69. // presets: ['@babel/preset-env']
  70. // }))
  71. // .pipe(sourcemaps.write("./maps"))
  72. // .pipe(dest('dist/js'))
  73. .pipe(browserSync.stream());
  74. }
  75. //整合
  76. function service() {
  77. browserSync.init({
  78. server: "./"
  79. });
  80. watch([...path.html,'src/**/*.html','/src/layout/layout.ejs'], watchHtml);
  81. watch([path.scss,'src/scss/**/*.scss'], watchScss);
  82. watch(path.js, watchJs);
  83. // 监听layout
  84. }
  85. // 移动到wwwroot
  86. let log = (str)=>console.log(color.green(str));
  87. function file_move() {
  88. //删除原始文件
  89. log('删除原始文件...');
  90. del('../CZKJ.CMS.Web/wwwroot/mobileAssets/**', { force: true });
  91. log('开始移动文件...');
  92. //移动
  93. return src('./mobileAssets/**')
  94. .pipe(dest('../CZKJ.CMS.Web/wwwroot/mobileAssets'))
  95. .on('end', () => {
  96. // del('./dist');
  97. log('移动完成...');
  98. })
  99. }
  100. exports.service = service;
  101. exports.move = file_move;
  102. exports.default = service;