-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
117 lines (101 loc) · 4.12 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const gulp = require('gulp'); // Gulp core
const cleanCSS = require('gulp-clean-css'); // CSS minification
const concat = require('gulp-concat'); // Files concatenation
const eslint = require('gulp-eslint'); // An AST-based pattern checker for JavaScript
const gutil = require('gulp-util'); // Utility functions for gulp plugins
const plumber = require('gulp-plumber'); // Prevent pipe breaking caused by errors
const postcss = require('gulp-postcss'); // Transforming CSS with JS plugins
const rename = require('gulp-rename'); // Rename files
const uglify = require('gulp-uglify'); // Minify JS files
const stylefmt = require('gulp-stylefmt'); // Automatically formats CSS according to stylelint rules
const autoprefixer = require('autoprefixer'); // Parse CSS and add vendor prefixes
const del = require('del'); // Delete files/folders using globs
const lost = require('lost'); // PostCSS fractional grid system built with calc()
const mqpacker = require('css-mqpacker'); // Pack same CSS media query rules into one media query rule
const path = require('path'); // Node.JS path module
const reporter = require('postcss-reporter'); // Log PostCSS messages in the console
const stylelint = require('stylelint'); // CSS linter
const fontMagician = require('postcss-font-magician'); // Automatically generates @font-face rules
const root = __dirname;
const paths = {
src: {
css: path.join(root, 'src/css/'),
scripts: path.join(root, 'src/scripts/')
},
output: path.join(root, '../files/folderName/')
};
var isProduction = true;
if (gutil.env.dev === true) {
isProduction = false;
}
/* Lint CSS files (Rules: http://stylelint.io/user-guide/rules) */
gulp.task('lint:css', () => {
return gulp.src(paths.src.css + '**/*.css')
.pipe(postcss([
stylelint(),
reporter({
clearReportedMessages: true
}),
]));
});
/* Lint JS files */
gulp.task('lint:js', () => {
return gulp.src([paths.src.scripts + '**/*.js', '!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/* Clean files */
gulp.task('clean', () => {
return del([paths.output + 'css', paths.output + 'js'], {force: true});
});
/* Formats CSS according to stylelint rules */
gulp.task('stylefmt', () => {
return gulp.src(paths.src.css + '**/*.css')
.pipe(stylefmt())
.pipe(gulp.dest(paths.output + 'css'));
});
/* Compile CSS files */
gulp.task('css', () => {
const processors = [
autoprefixer({browsers: ['last 2 versions', 'ie 9']}),
lost(),
mqpacker
];
return gulp.src(paths.src.css + '**/*.css')
.pipe(plumber())
.pipe(postcss(processors))
.pipe(isProduction ? concat('main.css') : gutil.noop())
.pipe(isProduction ? rename({suffix: '.min'}) : gutil.noop())
.pipe(isProduction ? cleanCSS({debug: true}, details => {
console.log(details.name + ': ' + Math.round(details.stats.efficiency * 100) + '% of the size was reduced.');
}) : gutil.noop())
.pipe(gulp.dest(paths.output + 'css'));
});
/* Automatically generates @font-face rules */
gulp.task('generate:font', () => {
return gulp.src(paths.src.css + '**/*.css')
.pipe(
postcss([fontMagician()]))
.pipe(gulp.dest(paths.output + 'css'));
});
/* Compile JS files */
gulp.task('scripts', () => {
return gulp.src(paths.src.scripts + '**/*.js')
.pipe(plumber())
.pipe(isProduction ? concat('main.js') : gutil.noop())
.pipe(isProduction ? rename({suffix: '.min'}) : gutil.noop())
.pipe(isProduction ? uglify() : gutil.noop())
.pipe(gulp.dest(paths.output + 'js'));
});
/* Watch files */
gulp.task('watch', () => {
gulp.watch(paths.src.css + '**/*.css', ['css']);
gulp.watch(paths.src.scripts + '**/*.js', ['scripts']);
});
/* Default task */
gulp.task('default', ['clean'], () => {
gulp.start('css', 'scripts');
});
/* Lint task */
gulp.task('lint', ['lint:js', 'lint:css']);