-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
250 lines (216 loc) · 5.32 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
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
// generated on 2016-11-12 using generator-webapp 2.3.2
const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const browserSync = require('browser-sync').create();
const wiredep = require('wiredep').stream;
const concat = require('gulp-concat');
const rollup = require('rollup-stream');
const sourcemaps = require('gulp-sourcemaps');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const babel = require('rollup-plugin-babel');
const includePaths = require('rollup-plugin-includepaths');
const $ = gulpLoadPlugins();
var dev = true;
function prepareStyles() {
return gulp.src('app/styles/*.scss')
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe(
$.dartSass.sync({
outputStyle: 'expanded',
precision: 10,
includePaths: ['.']
})
.on('error', $.dartSass.logError)
)
.pipe($.autoprefixer())
.pipe($.sourcemaps.write())
.pipe(gulp.dest('.tmp/styles'))
.pipe(browserSync.reload({
stream: true
}));
}
function prepareScripts() {
return rollup({
entry: './app/scripts/main.js',
format: "iife",
sourceMap: true,
plugins: [babel({
exclude: 'node_modules/**',
"presets": [
[
"es2015-rollup"
]
],
"plugins": [
"external-helpers"
]
}),
includePaths({
include: {},
paths: ['./app/scripts'],
external: [],
extensions: ['.js']
})]
})
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
// transform the code further here.
// if you want to output with a different name from the input file, use gulp-rename here.
//.pipe(rename('index.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('.tmp/scripts'))
.pipe(browserSync.reload({ stream: true }));
}
function runLinter(files) {
return gulp.src(files)
.pipe($.eslint({
fix: true
}))
.pipe(browserSync.reload({
stream: true,
once: true
}))
.pipe($.eslint.format())
.pipe($.if(!browserSync.active, $.eslint.failAfterError()));
}
function lintCode() {
return runLinter('app/scripts/**/*.js')
.pipe(gulp.dest('app/scripts'));
}
function bundleApp() {
return gulp.src('app/*.html')
.pipe($.useref({
searchPath: ['.tmp', 'app', '.']
}))
.pipe($.if('*.js', $.uglify()))
.pipe($.if('*.css', $.cssnano({
safe: true,
autoprefixer: false
})))
.pipe($.if('*.html', $.htmlmin({
collapseWhitespace: true
})))
.pipe(gulp.dest('dist'));
}
function prepareImages() {
return gulp.src('app/images/**/*')
.pipe($.cache($.imagemin()))
.pipe(gulp.dest('dist/images'));
}
function prepareFonts() {
return gulp.src(require('main-bower-files')('**/*.{eot,svg,ttf,woff,woff2}', (err) => {})
.concat('app/fonts/**/*'))
.pipe($.if(dev, gulp.dest('.tmp/fonts'), gulp.dest('dist/fonts')));
}
function prepareExtras() {
return gulp.src([
'app/*',
'!app/*.html'
], {
dot: true
}).pipe(gulp.dest('dist'));
}
// inject bower components
function prepareBowerCssDependencies() {
return gulp.src('app/styles/*.scss')
.pipe($.filter(file => file.stat && file.stat.size))
.pipe(wiredep({
ignorePath: /^(\.\.\/)+/
}))
.pipe(gulp.dest('app/styles'));
}
function prepareBowerHtmlDependencies() {
return gulp.src('app/*.html')
.pipe(wiredep({
exclude: ['bootstrap-sass'],
ignorePath: /^(\.\.\/)*\.\./
}))
.pipe(gulp.dest('app'));
}
const prepareBowerDependencies = gulp.series(
prepareBowerCssDependencies,
prepareBowerHtmlDependencies);
function doClean() {
return gulp.src(['.tmp/*', 'dist/*'], { read: false })
.pipe($.clean())
.on('error', $.util.log);
}
function browserSyncReloadTask(done) {
browserSync.reload();
done();
}
function doServe(done) {
browserSync.init({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp', 'app'],
routes: {
'/bower_components': 'bower_components'
}
}
});
gulp.watch([
'app/*.html',
'app/images/**/*',
'.tmp/fonts/**/*'
], browserSyncReloadTask);
gulp.watch('app/styles/**/*.scss', prepareStyles);
gulp.watch('app/scripts/**/*.js', prepareScripts);
gulp.watch('app/fonts/**/*', prepareFonts);
gulp.watch('bower.json', gulp.parallel(prepareFonts, prepareBowerDependencies));
done();
}
function doServeDist(done) {
browserSync.init({
notify: false,
port: 9000,
server: {
baseDir: ['dist']
}
});
done();
}
function doBuild() {
return gulp.src('dist/**/*').pipe($.size({
title: 'build',
gzip: true
}));
}
function setDevFalse(done) {
dev = false;
done();
}
exports.clean = gulp.series(doClean);
exports.build = gulp.series(
lintCode,
prepareScripts,
prepareStyles,
bundleApp,
prepareImages,
prepareFonts,
prepareExtras,
doBuild
);
exports.serve = gulp.series(
gulp.parallel(doClean, prepareBowerDependencies),
gulp.parallel(prepareStyles, prepareScripts, prepareFonts),
doServe);
exports.default = gulp.series(
setDevFalse,
gulp.parallel(doClean, prepareBowerDependencies),
exports.build);
exports.serveDist = gulp.series(
exports.default,
doServeDist);
exports.listPlugins = function(done) {
for (let k in $) {
$.util.log('Plugin available: ' + k)
}
done();
}