-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
239 lines (210 loc) · 6.95 KB
/
gulpfile.js
File metadata and controls
239 lines (210 loc) · 6.95 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const gulp = require('gulp'),
sass = require('gulp-sass'),
csso = require('gulp-csso'),
gutil = require('gulp-util'),
clean = require('gulp-clean'),
merge = require('merge-stream'),
notify = require('gulp-notify'),
uglify = require('gulp-uglify'),
ghPages = require('gulp-gh-pages'),
imagemin = require('gulp-imagemin'),
sourcemaps = require('gulp-sourcemaps'),
minifyHTML = require('gulp-minify-html'),
spritesmith = require('gulp.spritesmith'),
autoprefixer = require('gulp-autoprefixer'),
fileinclude = require('gulp-file-include'),
browserSync = require('browser-sync').create(),
babel = require('gulp-babel'),
jsImport = require('gulp-js-import'),
svgSprite = require('gulp-svg-sprite'),
eslint = require('gulp-eslint'),
minify = require('gulp-minify');
// start the server
gulp.task('server', function() {
browserSync.init({
server: {
baseDir: "./"
},
port: "7777"
});
gulp.watch(['./**/*.html']).on('change', browserSync.reload);
gulp.watch('./js/**/*.js').on('change', browserSync.reload);
gulp.watch('./scripts/**/*', ['import'],).on('change', browserSync.reload);
gulp.watch('./scripts/**/*.js').on('change', browserSync.reload);
gulp.watch([
'./templates/**/*.html',
'./pages/**/*.html'
], ['fileinclude']);
gulp.watch([
'./scripts/**/*.js',
'./js/**/*.js'
], ['babel']);
gulp.watch([
'./scripts/**/*.js'
], ['lint']);
gulp.watch(['./sass/**/*'], ['lint-css']);
gulp.watch('./sass/**/*', ['sass']);
});
// import js
gulp.task('import', function() {
return gulp.src('scripts/index.js')
.pipe(jsImport({hideConsole: true}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('js'));
});
// babel
gulp.task('babel',['import'],function() {
return gulp.src('./js/**/index.js')
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(gulp.dest('js'))
});
//stylelint
gulp.task('lint-css', function lintCssTask() {
const gulpStylelint = require('gulp-stylelint');
return gulp
.src(['./sass/**/*.scss', './sass/**/*.sass'])
.pipe(gulpStylelint({
reporters: [
{formatter: 'string', console: true}
]
}));
});
// fix-csslint
gulp.task('fix-css', function fixCssTask() {
const gulpStylelint = require('gulp-stylelint');
return gulp
.src(['./sass/**/*.scss', './sass/**/*.sass'])
.pipe(gulpStylelint({
fix: true
}))
.pipe(gulp.dest('sass'));
});
//eslint
gulp.task('lint', function () {
// ESLint ignores files with "node_modules" paths.
// So, it's best to have gulp ignore the directory as well.
// Also, Be sure to return the stream from the task;
// Otherwise, the task may end before the stream has finished.
return gulp.src(['./scripts/**.js'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
// compiling sass / scss into css
gulp.task('sass', function() {
gulp.src(['./sass/**/*.scss', './sass/**/*.sass'])
.pipe(sourcemaps.init())
.pipe(
sass({ outputStyle: 'expanded' })
.on('error', gutil.log)
)
.on('error', notify.onError())
.pipe(sourcemaps.write())
.pipe(gulp.dest('./css/'))
.pipe(browserSync.stream());
});
// build a page from templates
gulp.task('fileinclude', function() {
gulp.src('./pages/**/*.html')
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}).on('error', gutil.log))
.on('error', notify.onError())
.pipe(gulp.dest('./'))
});
// compression svg, png, jpeg
gulp.task('minify:img', function() {
//we take all the pictures except the folder where the pictures are for the sprite
return gulp.src(['./images/**/*', '!./images/sprite/*'])
.pipe(imagemin().on('error', gutil.log))
.pipe(gulp.dest('./public/images/'));
});
// compression css
gulp.task('minify:css', function() {
gulp.src('./css/**/*.css')
.pipe(autoprefixer({
browsers: ['last 30 versions'],
cascade: false
}))
.pipe(csso())
.pipe(gulp.dest('./public/css/'));
});
// compression js
gulp.task('minify:js', function() {
gulp.src('./js/**/index.js')
.pipe(minify({
ext:{
min:'.js'
},
noSource: true
}))
.pipe(gulp.dest('./public/js/'))
});
// compression html
gulp.task('minify:html', function() {
var opts = {
conditionals: true,
spare: true
};
return gulp.src(['./*.html'])
.pipe(minifyHTML(opts))
.pipe(gulp.dest('./public/'));
});
// delete folder public
gulp.task('clean', function() {
return gulp.src('./public', { read: false }).pipe(clean());
});
// create a picture sprite from the images / sprite folder
gulp.task('sprite', function() {
var spriteData = gulp.src('images/sprite/*.png').pipe(
spritesmith({
imgName: 'sprite.png',
cssName: '_icon-mixin.scss',
retinaImgName: 'sprite@2x.png',
retinaSrcFilter: ['images/sprite/*@2x.png'],
cssVarMap: function(sprite) {
sprite.name = 'icon-' + sprite.name;
}
})
);
var imgStream = spriteData.img.pipe(gulp.dest('images/'));
var cssStream = spriteData.css.pipe(gulp.dest('sass/'));
return merge(imgStream, cssStream);
});
//svg spite
gulp.task('svgSprite', function () {
return gulp.src('images/sprite_src/*.svg') // svg files for sprite
.pipe(svgSprite({
mode: {
stack: {
sprite: "sprite.svg" //sprite file name
}
}
}
))
.pipe(gulp.dest('images/sprite_src'));
});
// publication on gh-pages
gulp.task('deploy', function() {
return gulp.src('./public/**/*').pipe(ghPages());
});
// when called in the gulp command terminal, tasks will be started
// server - to start the server,
// sass - to compile sass into css because the browser
// does not understand the previous syntax,
// fileinclude - to assemble a full page from small templates
gulp.task('default', ['server','import','babel','sass','fileinclude','lint']);
// when calling the gulp production command
// all resources will be compressed into the public folder
// then the gulp deploy command can publish them to github
gulp.task('production', ['minify:html', 'minify:css', 'minify:js', 'minify:img']);
//test scss npx stylelint "**/*.scss"