-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
177 lines (160 loc) · 4.94 KB
/
gulpfile.babel.js
File metadata and controls
177 lines (160 loc) · 4.94 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
/*
Inspired by Web Starter Kit (https://github.com/google/web-starter-kit) gulpfile
*/
'use strict';
import gulp from 'gulp';
import del from 'del';
import runSequence from 'run-sequence';
import gulpLoadPlugins from 'gulp-load-plugins';
import browserSync from 'browser-sync';
const $ = gulpLoadPlugins();
const reload = browserSync.reload;
/**
* Helper function to amend the pipe when a build task fails
* see https://github.com/hughsk/vinyl-transform/issues/1
* and: https://github.com/gulpjs/gulp/issues/259
* @param {string} err the error string
*/
function onError (err) {
console.log(err);
this.emit('end'); // jshint ignore:line
}
gulp.task('styles', () => {
const AUTOPREFIXER_BROWSERS = [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
return gulp.src(['node_modules/normalize.css/normalize.css',
'app/css/main.scss'])
.pipe($.sourcemaps.init())
.pipe($.sass({
precision: 10
})).on('error', $.sass.logError)
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe($.size({title: 'styles'}))
.pipe($.sourcemaps.write('./'))
.pipe($.concat('style.css'))
.pipe(gulp.dest('./css'))
.pipe(gulp.dest('app/css'));
});
// Scan your HTML for assets & optimize them
gulp.task('html', () => {
return gulp.src('app/**/*.html')
.pipe($.useref({
searchPath: '{app}',
noAssets: true
}))
// Minify any HTML
.pipe($.if('*.html', $.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true
})))
// Output files
.pipe($.if('*.html', $.size({title: 'html', showFiles: true})))
.pipe(gulp.dest('.'));
});
// Copy all the non-HTML files at the root level
gulp.task('copy', () =>
gulp.src([
'app/*',
'!app/*.html',
], {
dot: true
}).pipe(gulp.dest('.'))
.pipe($.size({title: 'copy'}))
);
gulp.task('copy-smoothscroll', () =>
gulp.src([
'./app/scripts/smooth-scroll.min.js',
], {
dot: true
}).pipe(gulp.dest('./scripts'))
);
gulp.task('images', () =>
gulp.src('app/img/**/*')
.pipe($.imagemin({
progressive: true,
interlaced: true
}))
.pipe(gulp.dest('./img'))
.pipe($.size({title: 'images'}))
);
// Concatenate and minify JavaScript. Optionally transpiles ES2015 code to ES5.
// to enable ES2015 support remove the line `"only": "gulpfile.babel.js",` in the
// `.babelrc` file.
gulp.task('scripts', () =>
gulp.src([
// Note: Since we are not using useref in the scripts build pipeline,
// you need to explicitly list your scripts here in the right order
// to be correctly concatenated
'./app/scripts/main.js'
// Other scripts
])
.pipe($.plumber({
errorHandler: onError
}))
.pipe($.sourcemaps.init())
.pipe($.babel())
.pipe($.sourcemaps.write())
.pipe($.concat('main.min.js'))
.pipe($.uglify({preserveComments: 'some'}))
// Output files
.pipe($.size({title: 'scripts'}))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./scripts'))
);
// Build and serve the output from the dist build
gulp.task('serve:dist', ['default'], () =>
browserSync({
notify: false,
logPrefix: 'WSK',
// Allow scroll syncing across breakpoints
scrollElementMapping: ['main', '.mdl-layout'],
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: '.',
port: 3001
})
);
gulp.task('serve', ['styles', 'images', 'scripts'], function () {
browserSync({
notify: false,
// Customize the Browsersync console logging prefix
logPrefix: 'WSK',
// Allow scroll syncing across breakpoints
scrollElementMapping: ['main', '.mdl-layout'],
// // Run as an https by uncommenting 'https: true'
// // Note: this uses an unsigned certificate which on first access
// // will present a certificate warning in the browser.
// // https: true,
server: ['app']
});
gulp.watch(['app/**/*.html'], reload);
gulp.watch(['app/css/**/*.scss'], ['styles', reload]);
gulp.watch(['app/img/**/*'], ['images', reload]);
gulp.watch(['app/scripts/**/*.js'], ['scripts', reload]);
});
gulp.task('clean', () => del(['.tmp', 'docs/*', '!docs/.git'], {dot: true}));
// Build production files, the default task
gulp.task('default', ['clean'], () =>
runSequence(
'styles',
['html', 'images', 'copy', 'scripts', 'copy-smoothscroll']
)
);