gulpfile.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const gulp = require('gulp');
  2. const shell = require('gulp-shell');
  3. const filter = require('gulp-filter-each');
  4. const fileInclude = require('gulp-file-include');
  5. const through = require('through2');
  6. const prettyHtml = require('gulp-pretty-html');
  7. const sass = require('gulp-sass')(require('sass'));
  8. const autoprefixer = require('gulp-autoprefixer');
  9. const browserSync = require('browser-sync').create();
  10. false;
  11. const port = 3001;
  12. gulp.task('build-template', async function () {
  13. return new Promise((resolve) => {
  14. gulp
  15. .src('src/template/*.html')
  16. .pipe(filter((content) => content.match(/<html>/gi)))
  17. .pipe(
  18. fileInclude({
  19. prefix: '@@',
  20. basepath: '@file',
  21. })
  22. )
  23. .pipe(
  24. through.obj(async function (file, enc, cb) {
  25. let html = file.contents.toString();
  26. // merge css link and scripts to head
  27. const links = [];
  28. const cssRegx = /<link .*\/>\n/g;
  29. html = html.replace(cssRegx, (match) => {
  30. if (links.indexOf(match) === -1) {
  31. links.push(match);
  32. }
  33. return '';
  34. });
  35. const scriptRegx = /<script .*><\/script>\n/g;
  36. html = html.replace(scriptRegx, (match) => {
  37. if (links.indexOf(match) === -1) {
  38. links.push(match);
  39. }
  40. return '';
  41. });
  42. html = html.replace(
  43. '<!-- Please DO NOT remove this line, all link and script tags will be merged to here -->\n',
  44. links.join('')
  45. );
  46. // remove comment
  47. html = html.replace(/<!-- prettier-ignore -->/g, '');
  48. // change path from absolute to relative
  49. if (require('yargs').argv.relativePath) {
  50. html = html.replace(/href="\/css\//g, 'href="./css/');
  51. html = html.replace(
  52. /--src:url\(\/assets\//g,
  53. '--src:url(../assets/'
  54. );
  55. html = html.replace(/src="\/assets\//g, 'src="./assets/');
  56. }
  57. file.contents = Buffer.from(html);
  58. this.push(file);
  59. cb();
  60. })
  61. )
  62. .pipe(prettyHtml())
  63. .pipe(gulp.dest('./web'))
  64. .on('end', resolve);
  65. });
  66. });
  67. gulp.task('build-scss', async function () {
  68. return new Promise((resolve) => {
  69. gulp
  70. .src('src/scss/*.scss')
  71. .pipe(sass().on('error', sass.logError))
  72. .pipe(autoprefixer({ cascade: false }))
  73. .pipe(gulp.dest('./web/css'))
  74. .on('end', resolve);
  75. });
  76. });
  77. false;
  78. gulp.task('copy-css', async function () {
  79. return new Promise((resolve) => {
  80. gulp
  81. .src('src/css/*.css')
  82. .pipe(autoprefixer({ cascade: false }))
  83. .pipe(gulp.dest('./web/css'))
  84. .on('end', resolve);
  85. });
  86. });
  87. gulp.task('copy-js', async function () {
  88. return new Promise((resolve) => {
  89. gulp.src('src/js/**/*.*').pipe(gulp.dest('./web/js')).on('end', resolve);
  90. });
  91. });
  92. gulp.task('copy-assets', async function () {
  93. return new Promise((resolve) => {
  94. gulp
  95. .src('src/assets/*.*')
  96. .pipe(gulp.dest('./web/assets'))
  97. .on('end', resolve);
  98. });
  99. });
  100. gulp.task('copy-fonts', async function () {
  101. return new Promise((resolve) => {
  102. gulp.src('src/fonts/*.*').pipe(gulp.dest('./web/fonts')).on('end', resolve);
  103. });
  104. });
  105. gulp.task(
  106. 'build',
  107. gulp.series(
  108. 'build-template',
  109. 'build-scss',
  110. 'copy-css',
  111. 'copy-js',
  112. 'copy-assets',
  113. 'copy-fonts'
  114. )
  115. );
  116. gulp.task('watch', function (resolve) {
  117. const watchSrcFolders = [
  118. 'src/template/**',
  119. 'src/scss/**',
  120. 'src/css/**',
  121. 'src/js/**' /*, 'src/assets/**'*/,
  122. ];
  123. gulp
  124. .watch(watchSrcFolders, gulp.series('build'))
  125. .on('change', browserSync.reload);
  126. resolve();
  127. });
  128. gulp.task('server', function () {
  129. browserSync.init({
  130. ui: false,
  131. server: {
  132. port,
  133. baseDir: './web',
  134. directory: true,
  135. },
  136. });
  137. });
  138. // dev server
  139. gulp.task('dev', gulp.series('build', 'watch', 'server'));
  140. gulp.task('deploy-init', gulp.series(shell.task(['firebase init'])));
  141. gulp.task('deploy', gulp.series('build', shell.task(['firebase deploy'])));