-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
329 lines (286 loc) · 8.37 KB
/
gulpfile.babel.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"use strict";
const fs = require('fs')
// components
import gulp from 'gulp';
import browserSync from 'browser-sync';
import rename from 'gulp-rename';
import plumber from 'gulp-plumber';
import sourcemaps from 'gulp-sourcemaps';
import watch from 'gulp-watch';
import concat from 'gulp-concat';
import cssmin from 'gulp-cssmin';
import changed from 'gulp-changed';
import stylus from 'gulp-stylus';
import sass from 'gulp-sass';
import browserify from 'browserify';
import babelify from 'babelify';
import uglify from "gulp-uglify";
import source from "vinyl-source-stream";
import buffer from "vinyl-buffer";
import gulpEdge from 'gulp-edgejs';
import prettify from 'gulp-html-prettify';
import imagemin from 'gulp-imagemin';
import cache from 'gulp-cache';
import pngquant from 'imagemin-pngquant';
import htmlreplace from 'gulp-html-replace';
// import rimraf from 'rimraf';
sass.compiler = require('node-sass');
// gulp settings
const liveReload = browserSync.create();
const reload = liveReload.reload;
const path = {
app: {
styl: './resources/assets/stylus/',
scss: './resources/assets/scss/',
less: './resources/assets/less/',
js: './resources/assets/js/',
edge: './resources/views/',
images: './public/img/',
fonts: './resources/assets/fonts/'
},
dist: {
js: './public/js/',
css: './public/css/',
html: './public',
fonts: './public/fonts/',
},
lib: {
css: [
'node_modules/normalize.css/normalize.css',
'node_modules/bulma/css/bulma.css'
],
js: [
'node_modules/modernizr/bin/modernizr.js',
'node_modules/jquery/dist/jquery.js'
],
fonts: [
]
},
build: {
js: './public/js/',
css: './public/css/',
public: './public/',
images: './public/img',
fonts: './public/fonts'
}
};
// name tasks
const BROWSER_SYNC = 'browser-sync';
const JAVA_SCRIPT = 'javascript';
const EDGE = 'edge';
const STYLUS = 'stylus';
const SCSS = 'scss';
const LIBS = 'libs';
const BUILD = 'build';
const WATCHER = 'watcher';
const DEFAULT = 'default';
// name task javascript
const TYPE_FILE = '.js';
// array type file javascript
const ARRAY_TYPE_FILE = [...TYPE_FILE];
// error message
const errorAlert = err => {
console.log(err.toString());
this.emit("end");
};
const removeFile = file => {
try {
fs.unlinkSync(file)
} catch (err) {
console.error(err)
}
}
const pipeClearAfterActions = files => {
setTimeout(()=> {
try {
if ( typeof files === 'string' ) {
if (fs.existsSync(files)) {
removeFile(files)
}
} else {
for ( let item of files ) {
if (fs.existsSync(item)) {
removeFile(item)
}
}
}
} catch(err) {
console.error(err)
}
}, 1000)
}
// Static server
gulp.task(
BROWSER_SYNC, () => {
liveReload.init({
server: {
baseDir: "./public"
},
host: 'localhost',
port: 3000,
logPrefix: "landing",
reloadDelay: 1500
});
});
// script
gulp.task(
JAVA_SCRIPT, () => {
browserify({
entries: `${path.app.js}app${TYPE_FILE}`,
extensions: ARRAY_TYPE_FILE,
debug: true,
sourcemaps: true
}).transform(babelify, {
presets: ['@babel/preset-env'],
plugins: [
"syntax-class-properties",
"transform-class-properties"
]
}).bundle()
.on('error', (err) => errorAlert.bind(err))
.pipe(source('bundle.js'))
.pipe(gulp.dest(path.dist.js))
.pipe(reload({stream: true}));
});
// HTML
gulp.task(
EDGE, ()=> gulp.src(`${path.app.edge}*.edge`)
.pipe( gulpEdge() )
.pipe(prettify({
max_preserve_newlines: 1,
preserve_newlines: false,
space_before_conditional: false,
indent_with_tabs: true
}).on('error', (err) => errorAlert.bind(err) ))
.pipe(gulp.dest('./public'))
.pipe(reload({stream: true}))
);
// stylus
gulp.task(
STYLUS, () => gulp.src(`${path.app.styl}all.styl`)
.pipe(changed(path.dist.css))
.pipe(plumber())
.pipe(sourcemaps.init().on('error', (err) => errorAlert.bind(err) ))
.pipe(stylus().on('error', (err) => errorAlert.bind(err) ))
.pipe(sourcemaps.write('.').on('error', (err) => errorAlert.bind(err) ))
.pipe(gulp.dest(path.dist.css))
.pipe(reload({stream: true}))
);
// scss
gulp.task(SCSS, () => gulp.src(`${path.app.scss}all.scss`)
.pipe(changed(path.dist.css))
.pipe(plumber())
.pipe(sourcemaps.init().on('error', (err) => errorAlert.bind(err) ))
.pipe(sass.sync().on('error', (err) => errorAlert.bind(err) ))
.pipe(sourcemaps.write('.').on('error', (err) => errorAlert.bind(err) ))
.pipe(gulp.dest(path.dist.css))
.pipe(reload({stream: true}))
);
// concat min rename lib css
gulp.task(
'lib:css', ()=> gulp.src(path.lib.css)
.pipe(concat("lib.css"))
.pipe(cssmin())
.pipe(rename( {
suffix: ".min"
}))
.pipe(gulp.dest(path.dist.css))
);
// concat min rename lib js
gulp.task(
'lib:js', ()=> gulp.src(path.lib.js)
.pipe(concat("lib.js"))
.pipe(uglify())
.pipe(rename( {
suffix: ".min"
}))
.pipe(gulp.dest(path.dist.js))
);
// libs initialization
gulp.task(LIBS, ['lib:js', 'lib:css']);
// WATCHER
gulp.task(
WATCHER, () => {
watch(`${path.app.styl}/**/*.styl`, () => gulp.start(STYLUS));
watch(`${path.app.scss}/**/*.scss`, () => gulp.start(SCSS));
watch(`${path.app.js}/**/*.js`, () => gulp.start(JAVA_SCRIPT));
watch(`${path.app.edge}/**/*.edge`, () => gulp.start(EDGE));
watch('./public/img/**/*.*', reload );
watch(['!./public/**/*.*', './**/*.html', './favicon.*', '!node_modules/**/*'], reload );
});
// DEFAULT
gulp.task(
DEFAULT,
[
WATCHER,
BROWSER_SYNC,
// STYLUS,
SCSS,
JAVA_SCRIPT,
EDGE,
LIBS,
]
);
// build tasks
// // clean build directory
// gulp.task('clean', (cb) => {
// rimraf(path.build.public, cb)
// });
// image:build
gulp.task('image:build', () => {
gulp.src(`${path.app.images}**/*.*`)
.pipe(plumber().on('error', (err) => errorAlert.bind(err) ))
.pipe(cache(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
})).on('error', (err) => errorAlert.bind(err) ))
.pipe(gulp.dest(path.build.images))
.on('end', () => console.log(' image optimization Finished '));
});
// html:build
gulp.task(
'html:build', () => {gulp.src(['./public/**/*.html', './favicon.*', '!node_modules/**/*'])
.pipe(plumber().on('error', (err) => errorAlert.bind(err) ))
.pipe(htmlreplace({
'css': 'css/bundle.min.css',
'js': 'js/bundle.min.js'
}))
.pipe(gulp.dest(path.build.public));
});
// css:build
gulp.task(
'css:build', ()=> gulp.src([`${path.dist.css}lib.min.css`, `${path.dist.css}all.css`])
.pipe(concat("bundle.css"))
.pipe(cssmin())
.pipe(rename( {
suffix: ".min"
}))
.pipe(gulp.dest(path.build.css))
.on('end', () => {
pipeClearAfterActions([`${path.dist.css}lib.min.css`, `${path.dist.css}all.css`, `${path.dist.css}all.css.map`])
})
);
// js:build
gulp.task(
'js:build', () => gulp.src([
`${path.dist.js}lib.min.js`,
`${path.dist.js}plugins.js`,
`${path.dist.js}main.js`,
`${path.dist.js}bundle.js`
])
.pipe(concat("bundle.js"))
.pipe(buffer())
.pipe(uglify())
.pipe(rename({
suffix: ".min"
}))
.pipe(gulp.dest(path.build.js))
.on('end', () => {
pipeClearAfterActions([`${path.dist.js}lib.min.js`, `${path.dist.js}bundle.js`])
})
);
// fonts:build
gulp.task('fonts:build', ()=> gulp.src(`${path.dist.fonts}**/*.*`).pipe(gulp.dest(path.build.fonts)));
// run build
gulp.task(BUILD, ['fonts:build', 'css:build', 'js:build', 'html:build', 'image:build']);