You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. let fs = require('fs'),
  2. gulp = require('gulp'),
  3. concat = require('gulp-concat'),
  4. rename = require('gulp-rename'),
  5. cssmin = require('gulp-cssnano'),
  6. prefix = require('gulp-autoprefixer'),
  7. plumber = require('gulp-plumber'),
  8. sass = require('gulp-sass'),
  9. minify = require('gulp-minify'),
  10. notify = require('gulp-notify'),
  11. size = require('gulp-size'),
  12. cached = require('gulp-cached'),
  13. fileInclude = require('gulp-file-include'),
  14. sourcemaps = require('gulp-sourcemaps'),
  15. spritesmith = require('gulp.spritesmith');
  16. let packageFile = JSON.parse(fs.readFileSync('source.info.json'));
  17. let source = packageFile.source;
  18. let destPath = packageFile.destPath;
  19. let watchPath = packageFile.watch;
  20. function reloadSourceData()
  21. {
  22. packageFile = JSON.parse(fs.readFileSync('source.info.json'));
  23. source = packageFile.source;
  24. destPath = packageFile.destPath;
  25. watchPath = packageFile.watch;
  26. }
  27. let displayError = function(error) {
  28. // Initial building up of the error
  29. var errorString = '[' + error.plugin.error.bold + ']';
  30. errorString += ' ' + error.message.replace("\n",''); // Removes new line at the end
  31. // If the error contains the filename or line number add it to the string
  32. if(error.fileName)
  33. errorString += ' in ' + error.fileName;
  34. if(error.lineNumber)
  35. errorString += ' on line ' + error.lineNumber.bold;
  36. // This will output an error like the following:
  37. // [gulp-sass] error message in file_name on line 1
  38. console.error(errorString);
  39. };
  40. let onError = function(err) {
  41. notify.onError({
  42. title: "Gulp",
  43. subtitle: "Failure!",
  44. message: "Error: <%= error.message %>",
  45. sound: "Basso"
  46. })(err);
  47. this.emit('end');
  48. };
  49. /**********************************************************************************************************
  50. * TASKS
  51. ***********************************************************************************************************/
  52. gulp.task('html', function(){
  53. reloadSourceData();
  54. let dest = destPath.root + "/";
  55. return gulp.src(source.html)
  56. .pipe(fileInclude({
  57. prefix : '@@',
  58. context : {
  59. ROOT : '.'
  60. }
  61. }))
  62. .pipe(cached('include'))
  63. .pipe(size({ gzip:true, showFiles:true }))
  64. .pipe(gulp.dest(dest));
  65. });
  66. gulp.task('scss:compile', function () {
  67. reloadSourceData();
  68. let dest = destPath.root + "/" + destPath.assets + "/" + destPath.css;
  69. return gulp.src( source.css )
  70. .pipe(plumber({errorHandler: onError}))
  71. .pipe(sourcemaps.init())
  72. .pipe( sass({outputStyle: 'expanded'}).on('error', sass.logError))
  73. .pipe( prefix({}))
  74. .pipe( rename('style.css'))
  75. .pipe(gulp.dest(dest))
  76. .pipe(cssmin( { "discardComments": { "removeAll": true } }))
  77. .pipe(rename({ suffix: '.min' }))
  78. .pipe(sourcemaps.write('.'))
  79. .pipe(gulp.dest(dest));
  80. });
  81. gulp.task('js:combine', function(){
  82. reloadSourceData();
  83. let dest = destPath.root + "/" + destPath.assets + "/" + destPath.js;
  84. let fileName = "common.js";
  85. return gulp.src( source.js )
  86. .pipe(sourcemaps.init())
  87. .pipe(concat(fileName))
  88. .pipe(minify({ext: {min : '.min.js'},noSource:true}))
  89. .pipe(size({ gzip: true, showFiles: true }))
  90. .pipe(sourcemaps.write('.'))
  91. .pipe(gulp.dest(dest));
  92. });
  93. gulp.task('sprite', function () {
  94. reloadSourceData();
  95. var spriteData = null;
  96. let dest = destPath.root + "/" + destPath.assets + "/icons";
  97. var packages = fs.readdirSync(source.icons);
  98. for(var i in packages) {
  99. spriteData = gulp.src('src/icons/'+packages[i]+'/*.png').pipe(spritesmith({
  100. imgName: packages[i] + '.png',
  101. cssName: packages[i] +'.css',
  102. padding:2,
  103. cssTemplate: 'src/icons/iconsTemplates.scss.handlebars',
  104. cssHandlebarsHelpers : {'packageName' : packages[i]},
  105. imgPath : '../icons/' + packages[i] + '.png',
  106. algorithm:'left-right',
  107. algorithmOpts:false
  108. }));
  109. spriteData.img.pipe(gulp.dest(dest));
  110. spriteData.css.pipe(gulp.dest("src/icons/"));
  111. }
  112. return true;
  113. });
  114. gulp.task('watch', function(){
  115. gulp.watch(watchPath.html, gulp.series(['html']));
  116. gulp.watch(watchPath.js, gulp.series(['js:combine']));
  117. gulp.watch(watchPath.css, gulp.series(['scss:compile']));
  118. });
  119. gulp.task('default', gulp.series(['html', 'js:combine', 'scss:compile']));