-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
129 lines (110 loc) · 4.02 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
'use strict';
import plugins from 'gulp-load-plugins';
import browser from 'browser-sync';
import gulp from 'gulp';
import yaml from 'js-yaml';
import fs from 'fs';
import webpackStream from 'webpack-stream';
import webpack2 from 'webpack';
import del from 'del';
// ----------------------------------------------------------------------------
// Load all Gulp plugins into one variable
// ----------------------------------------------------------------------------
const $ = plugins();
// ----------------------------------------------------------------------------
// Read info from package.json to one variable
// ----------------------------------------------------------------------------
const pkg = require('./package.json');
// ----------------------------------------------------------------------------
// Load settings from config.yml
// ----------------------------------------------------------------------------
const { COMPATIBILITY, PORT, UNCSS_OPTIONS, PATHS } = loadConfig();
function loadConfig() {
let ymlFile = fs.readFileSync('config.yml', 'utf8');
return yaml.load(ymlFile);
}
// ----------------------------------------------------------------------------
// Build the site, run the server, and watch for file changes
// ----------------------------------------------------------------------------
gulp.task( 'default', gulp.series( clean, gulp.parallel( sass, javascript ), serve, watch ) );
// ----------------------------------------------------------------------------
// Delete the style files every time a build starts
// ----------------------------------------------------------------------------
function clean(done) {
return del([
'./style.css',
'./style.css.map',
'./js/theme.min.js',
]);
done();
}
// ----------------------------------------------------------------------------
// Compile Sass into CSS
// ----------------------------------------------------------------------------
function sass() {
return gulp.src('src/assets/scss/app.scss')
.pipe($.sourcemaps.init())
.pipe($.sass({
includePaths: PATHS.sass
})
.on("error", $.notify.onError("Error: <%= error.message %>")))
.pipe($.autoprefixer({
browsers: COMPATIBILITY
}))
.pipe($.cleanCss({ compatibility: 'ie9' }))
.pipe($.rename('style.css')) // rename stylesheet for WordPress
.pipe($.sourcemaps.write(PATHS.dist))
.pipe(gulp.dest(PATHS.dist))
.pipe(browser.reload({ stream: true }));
}
let webpackConfig = {
externals: {
jquery: 'jQuery' // disable including jQuery library in minified JavaScript file
},
module: {
rules: [
{
test: /.js$/,
use: [
{
loader: 'babel-loader'
}
]
}
]
}
}
// ----------------------------------------------------------------------------
// Combine JavaScript into one minified file
// ----------------------------------------------------------------------------
function javascript() {
return gulp.src(PATHS.entries)
.pipe(webpackStream(webpackConfig, webpack2))
.pipe($.uglify()
.on('error', e => { console.log(e); })
)
.pipe($.rename( 'theme.min.js' ))
.pipe(gulp.dest(PATHS.dist + '/js'))
.pipe(browser.reload({ stream: true }));
}
// ----------------------------------------------------------------------------
// Start a server with BrowserSync to preview the site
// ----------------------------------------------------------------------------
function serve( done ) {
browser.init({
host: pkg.vars.hostname,
port: 8000,
proxy: pkg.vars.hostname + '/' + pkg.name,
files: "*.php", // watch for PHP file modifications
ui: false,
notify: true,
});
done();
}
// ----------------------------------------------------------------------------
// Watch for changes to Sass and JavaScript
// ----------------------------------------------------------------------------
function watch() {
gulp.watch('src/assets/scss/**/*.scss').on( 'all', sass );
gulp.watch('src/assets/js/**/*.js').on( 'all', javascript );
}