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.

149 lines
4.6 KiB

7 years ago
3 months ago
7 years ago
7 years ago
7 years ago
7 years ago
3 months ago
3 months ago
7 years ago
  1. let fs = require('fs'),
  2. gulp = require('gulp'),
  3. concat = require('gulp-concat'),
  4. rename = require('gulp-rename'),
  5. cleanCSS = require('gulp-clean-css'),
  6. prefix = require('gulp-autoprefixer'),
  7. plumber = require('gulp-plumber'),
  8. sass = require('gulp-sass')(require('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. overrideBrowserslist: ['> 1%', 'last 2 versions', 'Firefox ESR', 'not dead'],
  75. cascade: false,
  76. remove: false
  77. }))
  78. .pipe( rename('style.css'))
  79. .pipe(gulp.dest(dest))
  80. .pipe(cleanCSS({ compatibility: '*' }))
  81. .pipe(rename({ suffix: '.min' }))
  82. .pipe(sourcemaps.write('.'))
  83. .pipe(gulp.dest(dest));
  84. });
  85. gulp.task('js:combine', function(){
  86. reloadSourceData();
  87. let dest = destPath.root + "/" + destPath.assets + "/" + destPath.js;
  88. let fileName = "common.js";
  89. return gulp.src( source.js )
  90. .pipe(sourcemaps.init())
  91. .pipe(concat(fileName))
  92. .pipe(minify({ext: {min : '.min.js'},noSource:true}))
  93. .pipe(size({ gzip: true, showFiles: true }))
  94. .pipe(sourcemaps.write('.'))
  95. .pipe(gulp.dest(dest));
  96. });
  97. gulp.task('sprite', function () {
  98. reloadSourceData();
  99. var spriteData = null;
  100. let dest = destPath.root + "/" + destPath.assets + "/icons";
  101. var packages = fs.readdirSync(source.icons);
  102. for(var i in packages) {
  103. spriteData = gulp.src('src/icons/'+packages[i]+'/*.png').pipe(spritesmith({
  104. imgName: packages[i] + '.png',
  105. cssName: packages[i] +'.css',
  106. padding:2,
  107. cssTemplate: 'src/icons/iconsTemplates.scss.handlebars',
  108. cssHandlebarsHelpers : {'packageName' : packages[i]},
  109. imgPath : '../icons/' + packages[i] + '.png',
  110. algorithm:'left-right',
  111. algorithmOpts:false
  112. }));
  113. spriteData.img.pipe(gulp.dest(dest));
  114. spriteData.css.pipe(gulp.dest("src/icons/"));
  115. }
  116. return true;
  117. });
  118. gulp.task('watch', function(){
  119. gulp.watch(watchPath.html, gulp.series(['html']));
  120. gulp.watch(watchPath.js, gulp.series(['js:combine']));
  121. gulp.watch(watchPath.css, gulp.series(['scss:compile']));
  122. });
  123. gulp.task('default', gulp.series(['html', 'js:combine', 'scss:compile']));