-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
197 lines (174 loc) · 5.39 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
const fs = require('fs');
const gulp = require('gulp');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const gulpif = require('gulp-if');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const cleanCSS = require('gulp-clean-css');
const log = require('gulplog');
const mode = require('gulp-mode')({
modes: ["production", "development"],
default: "development",
verbose: false
});
let sass = require('gulp-sass');
sass.compiler = require('node-sass');
// Project paths.
const paths = {
styles: {
src: [
'sass/style-admin.scss',
'sass/style-wp-admin.scss',
'sass/admin-bar.scss',
'sass/wp-admin-bar.scss'
],
dest: '../build',
sass: 'sass/**/*.scss'
},
scripts: {
src: ['js/**/*.js'],
concat: 'app.js',
dest: '../build',
watch: ['js/**/*.js']
},
folders: {
'assets': 'assets/'
},
};
const isProduction = mode.production();
/**
* Return the styles pipes for an asset from the list.
*
* @param {string} slug The asset slug.
* @param {array} assetList The folders list.
* @param {boolean} forceProduction If the scripts must be forced to be build as production.
* @return {stream} The gulp stream.
*/
function makeStylePipes(slug, assetList, forceProduction) {
const sources = [];
paths.styles.src.forEach((src) => {
const filePath = assetList[slug] + src;
if (fs.existsSync(filePath)) {
sources.push(filePath);
}
});
if (sources.length === 0) return;
log.info(`Building ${slug} Styles...`);
return gulp.src(sources)
// Init the sourcemaps.
.pipe(gulpif(!isProduction, sourcemaps.init()))
// Compile the sass.
.pipe(sass().on('error', sass.logError))
// Add prefixes.
.pipe(postcss([
autoprefixer()
]))
// If not in debug mode minify the styles.
.pipe(gulpif(isProduction || forceProduction, cleanCSS()))
.pipe(gulpif(isProduction || forceProduction, rename({suffix: '.min'})))
// Stop listening and write the sourcemaps.
.pipe(gulpif(!isProduction, sourcemaps.write('.')))
// Spit it out in the dest folder.
.pipe(gulp.dest(assetList[slug] + paths.styles.dest)).on('end', () => {
log.info(`Finished '${slug}' Styles Build.`);
});
}
/**
* Return the scripts pipes for an asset from the list.
*
* @param {string} slug The asset slug.
* @param {array} assetList The folders list.
* @param {boolean} forceProduction If the scripts must be forced to be build as production.
* @return {stream} The gulp stream.
*/
function makeScriptsPipes(slug, assetList, forceProduction) {
const sources = [];
paths.scripts.src.forEach((src) => {
const filePath = assetList[slug] + src;
if (fs.existsSync(filePath) || filePath.indexOf('*') !== -1) {
sources.push(filePath);
}
});
if (sources.length === 0) return;
log.info(`Building ${slug} Scripts...`);
let babelOptions = {
presets: ['airbnb']
}
// If production remove console.log, console.error.
if (isProduction) {
babelOptions.plugins = ['transform-remove-console'];
}
return gulp.src(sources)
.pipe(gulpif(!isProduction, sourcemaps.init()))
.pipe(concat('app.js'))
.pipe(babel(babelOptions))
.pipe(gulpif(isProduction || forceProduction, uglify()))
.pipe(gulpif(isProduction || forceProduction, rename({suffix: '.min'})))
.pipe(gulpif(!isProduction, sourcemaps.write('.')))
// Spit it out in the dest folder.
.pipe(gulp.dest(assetList[slug] + paths.scripts.dest)).on('end', () => {
log.info(`Finished '${slug}' Scripts Build.`);
});
}
/**
* Watch everything.
*
* @param {boolean} forceProduction If all the watched files must be forced to be build as production.
*/
function watch(forceProduction) {
// Watch every folder STYLES.
Object.keys(paths.folders).forEach((folder) => {
// Watch all the folder sass files.
gulp.watch(paths.folders[folder] + paths.styles.sass, function watchStyle() {
if (forceProduction) production();
return makeStylePipes(folder, paths.folders);
});
});
// Watch every folder SCRIPTS.
Object.keys(paths.folders).forEach((folder) => {
// Watch the folder js files.
gulp.watch(paths.folders[folder] + paths.scripts.watch, function watchScript() {
if (forceProduction) production();
return makeScriptsPipes(folder, paths.folders);
});
});
}
/**
* Build everything.
*/
function build() {
// Build every folder STYLES.
Object.keys(paths.folders).forEach((folder) => {
return makeStylePipes(folder, paths.folders);
});
// Build every folder SCRIPTS.
Object.keys(paths.folders).forEach((folder) => {
return makeScriptsPipes(folder, paths.folders);
});
}
/**
* Production everything.
*/
function production() {
// Build for production every folder STYLES.
Object.keys(paths.folders).forEach((folder) => {
return makeStylePipes(folder, paths.folders, true);
});
// Build for production every folder SCRIPTS.
Object.keys(paths.folders).forEach((folder) => {
return makeScriptsPipes(folder, paths.folders, true);
});
}
gulp.task('watch', function() {
watch();
});
gulp.task('watch-prod', function() {
watch(true);
});
gulp.task('build', async function() {
build();
});