-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
195 lines (184 loc) · 4.3 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
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
const gulp = require('gulp');
const babel = require('gulp-babel');
const watch = require('gulp-watch');
const browserSync = require('browser-sync');
const autoClose = require('browser-sync-close-hook');
const nodemon = require('gulp-nodemon');
const sass = require('gulp-sass');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
const minify = require('gulp-minify');
const ejs = require('gulp-ejs');
const imagemin = require('gulp-imagemin');
const autoprefixer = require('gulp-autoprefixer');
const fse = require('fs-extra');
const path = require('path');
const glob = require('glob');
const reload = browserSync.reload;
const copyFiles = function copyFiles(files) {
const copy = (file, dest) => {
return fse.copy(file, dest, err => {
if (err) {
console.error(err);
return;
}
console.log(`Successfully copied file ${file}!`);
});
};
files.forEach((file) => copy(
file.module,
`${file.dest}${path.basename(file.module)}`
));
}
const buildNodeModules = function buildIcons() {
const modules = [
{
module: './node_modules/jquery/dist/jquery.min.js',
dest: './dist/js/modules/',
},
];
fse.ensureDir('./dist/js/modules')
.then(() => {
copyFiles(modules);
})
.catch(err => {
console.error(err)
});
};
const sassTask = function buildSass() {
// make sure to import all other scss files in main
return gulp.src('src/_styles/main.scss')
.pipe(sass({ outputStyle: 'compressed' })
.on('error', sass.logError))
.pipe(rename('main.min.css'))
.pipe(autoprefixer({
browsers: ['last 5 versions'],
cascade: false,
}))
.pipe(gulp.dest('./dist/css'))
.on('end', () => {
console.log('Successfully Built SASS');
browserSync.reload();
});
};
const jsTask = function buildJS() {
return gulp.src([
'src/_js/**/*.js',
'!src/_js/**/*.spec.js',
])
.pipe(concat('app.js'))
.pipe(babel({
presets: ['env'],
}))
.pipe(minify({
ext: {
min: '.min.js',
},
}))
.pipe(gulp.dest('dist/js/'))
.on('end', () => {
console.log('Successfully Built JS');
});
};
const imgTask = function buildImages() {
return gulp.src('src/_images/**/*')
.pipe(imagemin([
imagemin.optipng({ optimizationLevel: 5 }),
]))
.pipe(gulp.dest('dist/_images'))
.on('end', () => {
console.log('Successfully compressed images');
});
};
const buildEjs = function buildEjs() {
glob("./src/**/index.ejs", function (er, files) {
if (er) {
console.log(er);
return;
}
if (files.length > 0) {
files.forEach((file) => {
let dir = path.dirname(file).replace('\.\/src', '');
dir = dir === '' ? '/' : dir;
// compile each index file
gulp.src(file)
.pipe(ejs({}, {}, { ext: '.html' }))
.pipe(gulp.dest(`./dist${dir}`))
.on('end', () => {
console.log(`Successfully built html for ${dir}!`);
});
});
}
})
}
const buildDist = function buildDist() {
return fse.emptyDir('./dist')
.then(() => {
buildEjs();
sassTask();
jsTask();
imgTask();
buildNodeModules();
});
}
gulp.task('build-sass', sassTask);
gulp.task('build-js', jsTask);
gulp.task('build-images', imgTask);
gulp.task('build-node-modules', buildNodeModules);
gulp.task('build-dist', buildDist);
gulp.task('build-ejs', buildEjs);
gulp.task('browser-sync', ['nodemon'], () => {
browserSync.use({
plugin() {},
hooks: {
'client:js': autoClose,
},
});
browserSync({
proxy: 'localhost:2001',
port: 2004,
notify: false,
ui: false,
});
});
gulp.task('nodemon', (cb) => {
let called = false;
return nodemon({
script: 'server.js',
ignore: [
'gulpfile.js',
'node_modules/',
],
})
.on('start', () => {
if (!called) {
called = true;
cb();
}
})
.on('restart', () => {
setTimeout(() => {
reload({ stream: false });
}, 1000);
});
});
gulp.task('default', [
'browser-sync',
'build-sass',
'build-js',
'build-images',
'build-node-modules',
], () => {
watch([
'**/*.ejs',
], reload);
gulp.watch([
'src/_styles/**/*.scss',
], ['build-sass']);
gulp.watch([
'src/_images/**/*',
], ['build-images']);
gulp.watch([
'src/_js/**/*.js',
], ['build-js']);
});