-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
142 lines (127 loc) · 3.17 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
import browserSync from 'browser-sync';
import cleanCSS from 'gulp-clean-css';
import gulp from 'gulp';
import gulpInlineSource from 'gulp-inline-source';
import minimist from 'minimist';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';
import GulpRunner from 'gulp-run';
import runSequence from 'run-sequence';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
// parse command line options
// [--env dev (default) | prod]
const options = minimist(process.argv);
const env = options.env || 'dev';
// Environment-based configurations for CleanCss
// https://github.com/jakubpawlowicz/clean-css
const cleanCssConfig = {
dev: {
compatibility: '*',
level: 2,
format: 'beautify'
},
prod: {
compatibility: '*',
level: 2
}
};
/*
* gulp environment
* Prints the environment setting.
*/
export const environment = () => console.log(`${env}`);
/*
* gulp sass
* Compile the sass
*/
const sassTask = () => {
let stream = gulp
.src('styles.scss')
// Initialize sourcemaps
.pipe(sourcemaps.init())
// Compile sass synchronously
.pipe(sass.sync().on('error', sass.logError))
// Autoprefix
.pipe(postcss([autoprefixer()]))
// Clean CSS
.pipe(cleanCSS(cleanCssConfig[env]));
if (env === 'dev') {
// Write out sourcemaps
stream.pipe(sourcemaps.write());
}
stream.pipe(gulp.dest('.')).pipe(browserSync.stream());
return stream;
};
gulp.task('sass', sassTask);
/*
* gulp js
* Package and babelize the javascript with webpack.
*/
const jsTask = () => {
return GulpRunner('./node_modules/.bin/webpack').exec();
};
gulp.task('js', jsTask);
/*
* gulp html
* Inline assets into the deployable HTML.
*
* link and script tags need an 'inline' attribute to be inlined.
* https://github.com/fmal/gulp-inline-source
*/
export const html = () => {
return (
gulp
.src('404.html')
// I was getting uglify-js errors, and since webpack is already compressing
// the content, there's no need for inline-source to compress too.
.pipe(gulpInlineSource({ compress: false }))
.pipe(gulp.dest('_build'))
);
};
/*
* gulp watch
* Watch for changes, re-compile the site, and refresh browser-sync.
*
* This is only useful for development. For production, use `gulp html`
* to build the fully-packaged 404 page.
*/
const watchTask = () => {
browserSync.init({
server: {
baseDir: './',
index: '404.html'
}
});
gulp.watch('styles.scss', ['sass']);
gulp.watch('404.html').on('change', browserSync.reload);
gulp.watch('app.bundle.js').on('change', browserSync.reload);
};
gulp.task('watch', watchTask);
/*
* gulp
* Default task that compiles site and launches a development server with
* Browsersync.
*/
gulp.task('default', ['js', 'sass', 'watch']);
/*
* gulp build
* Build site for production
*/
gulp.task('build', callback => {
runSequence('js', 'sass', 'html', callback);
});
/*
* gulp pretty
* Run Prettier to autoformat code.
*/
export const pretty = () => {
return GulpRunner('npm run pretty').exec();
};
/*
* gulp lint
* Run ESLint on the JavaScript.
*/
export const lint = () => {
return GulpRunner('npm run lint').exec();
};