-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.babel.js
135 lines (117 loc) · 3.82 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
'use strict';
var plugins = require('gulp-load-plugins');
var yargs = require('yargs');
var gulp = require('gulp');
var rimraf = require('rimraf');
var yaml = require('js-yaml');
var fs = require('fs');
var webpackStream = require('webpack-stream');
var webpack2 = require('webpack');
var named = require('vinyl-named');
var autoprefixer = require('autoprefixer');
// Load all Gulp plugins into one variable
const $ = plugins();
// Check for --production flag
let PRODUCTION = !!(yargs.argv.production);
// Load settings from settings.yml
const { COMPATIBILITY, PORT, UNCSS_OPTIONS, PATHS, LOCAL_PATH } = loadConfig();
let sassConfig = {
mode: (PRODUCTION ? true : false)
};
// Define default webpack object
let webpackConfig = {
mode: (PRODUCTION ? 'production' : 'development'),
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: [ "@babel/preset-env" ],
compact: false
}
}
}
]
},
externals: {
jquery: 'jQuery',
},
devtool: ! PRODUCTION && 'source-map'
};
/**
* Load in additional config files
*/
function loadConfig() {
let ymlFile = fs.readFileSync('config.yml', 'utf8');
return yaml.load(ymlFile);
}
/**
* Set production mode during the build process
*
* @param done
*/
function setProductionMode(done) {
PRODUCTION = false;
webpackConfig.mode = 'production';
webpackConfig.devtool = false;
sassConfig.production = true;
done();
}
// Build the "dist" folder by running all of the below tasks
// Sass must be run later so UnCSS can search for used classes in the others assets.
gulp.task('build:production',
gulp.series(setProductionMode, clean, javascript, sass, copy)); // removed copy from (javascript, images, copy)
// Build the site, run the server, and watch for file changes
gulp.task('default',
gulp.series(clean, javascript, sass, copy, gulp.parallel(watch))); // removed server from middle
// This happens every time a build starts
function clean(done) {
rimraf('css', done);
rimraf('js', done);
}
// Copy files out of the assets folder
// This task skips over the "img", "js", and "scss" folders, which are parsed separately
function copy() {
return gulp.src(PATHS.assets)
.pipe( gulp.dest('css/fonts'));
}
// In production, the CSS is compressed
function sass() {
/*
const postCssPlugins = [
// Autoprefixer
autoprefixer({ browsers: COMPATIBILITY }),
// UnCSS - Uncomment to remove unused styles in production
// PRODUCTION && uncss.postcssPlugin(UNCSS_OPTIONS),
].filter(Boolean); */
return gulp.src('assets/scss/*.scss')
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: PATHS.sass
})
.on('error', $.sass.logError))
// .pipe($.postcss(postCssPlugins))
// .pipe($.if(sassConfig.production, $.cleanCss({ compatibility: 'ie10' })))
// .pipe($.if(!sassConfig.production, $.sourcemaps.write()))
.pipe(gulp.dest('css'));
}
// In production, the file is minified
function javascript() {
return gulp.src(PATHS.entries)
.pipe(named())
.pipe($.sourcemaps.init())
.pipe(webpackStream(webpackConfig, webpack2))
.pipe($.if(PRODUCTION, $.uglify()
.on('error', e => { console.log(e); })
))
.pipe($.if(!PRODUCTION, $.sourcemaps.write()))
.pipe(gulp.dest('js'));
}
// Watch for changes to static assets, Sass, and JavaScript
function watch() {
gulp.watch('assets/scss/**/*.scss').on('all', sass );
gulp.watch('assets/js/**/*.js').on('all', gulp.series(javascript));
gulp.watch( PATHS.assets, copy );
}