-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
138 lines (122 loc) · 3.05 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const gulp = require('gulp');
const concat = require('gulp-concat');
const autoprefixer = require('gulp-autoprefixer');
const ejs = require('gulp-ejs');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const hash = require('gulp-hash');
const sourcemaps = require('gulp-sourcemaps');
const imagemin = require('gulp-imagemin');
const browserSync = require('browser-sync').create();
const { parseFileName } = require('./util/util');
const ejsFullPath = 'stages/ejs/**/*.ejs';
const ejsPath = 'stages/ejs/*.ejs';
const scriptFileName = 'public/assets.json';
const jsPath = 'stages/js/*.js';
const scssPath = 'stages/scss/*.scss';
const scssMainPath = 'stages/scss/main.scss';
const imgPath = 'stages/assets/img/*';
const dataPath = 'stages/data/*';
function ejsTask(done) {
gulp
.src(ejsPath)
.pipe(
ejs({
fileName:
process.env.NODE_ENV === 'production' ? parseFileName() : 'main.js'
})
)
.pipe(rename({ extname: '.html' }))
.pipe(gulp.dest('dist'));
done();
}
function changeScriptName(done) {
gulp
.src(ejsPath)
.pipe(
ejs({
fileName:
process.env.NODE_ENV === 'production' ? parseFileName() : 'main.js'
})
)
.pipe(rename({ extname: '.html' }))
.pipe(gulp.dest('dist'));
done();
}
function jsDevTask(done) {
gulp
.src(jsPath)
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ['@babel/preset-env']
})
)
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'));
done();
}
function jsProdTask(done) {
gulp
.src(jsPath)
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ['@babel/preset-env']
})
)
.pipe(uglify())
.pipe(hash())
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'))
.pipe(
hash.manifest('public/assets.json', {
// Generate the manifest file
deleteOld: true,
sourceDir: __dirname + '/dist/js'
})
)
.pipe(gulp.dest('.'));
done();
}
function cssTask(done) {
gulp
.src(scssMainPath)
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(autoprefixer('last 2 version'))
.pipe(concat('main.css'))
.pipe(gulp.dest('dist/css'));
done();
}
function dataTask(done) {
gulp.src(dataPath).pipe(gulp.dest('dist/data'));
done();
}
function imgTask(done) {
gulp.src(imgPath).pipe(imagemin()).pipe(gulp.dest('dist/img'));
done();
}
function watchFiles() {
browserSync.init({
watch: true,
startPath: './dist',
server: {
baseDir: './'
}
});
gulp.watch(ejsFullPath, ejsTask);
gulp.watch(
jsPath,
process.env.NODE_ENV === 'production' ? jsProdTask : jsDevTask
);
gulp.watch(scssPath, cssTask);
gulp.watch(scriptFileName, changeScriptName);
gulp.watch(imgPath, imgTask);
gulp.watch(dataPath, dataTask);
}
exports.js = process.env.NODE_ENV === 'production' ? jsProdTask : jsDevTask;
exports.build = gulp.series(cssTask, imgTask, ejsTask, dataTask);
exports.default = watchFiles;