forked from GravitateDesignStudio/blueprint-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
187 lines (162 loc) · 3.78 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
const fs = require('fs');
const path = require('path');
const { series, parallel, src, dest } = require('gulp');
const plugins = require('gulp-load-plugins')();
const sass = require('gulp-dart-sass');
const browsersync = require('browser-sync').create();
const chokidar = require('chokidar');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const webpackConfig = require('./webpack.config.js');
const watchPaths = {
scss: 'css/**/*.scss',
js: 'js/**/*.js',
php: [
'**/*.php',
'!vendor/**/*.php',
'!node_modules/**/*.php',
'!media/**/*.php',
'!js/**/*.php',
'!css/**/*.php',
'!dist/**/*.php'
]
};
const srcFiles = {
scss: 'css/master.scss',
scssEditorStyles: 'css/editor-styles.scss',
js: 'js/master.js'
};
const distFiles = {
js: 'dist/js/master.min.js'
};
function loadLocalConfig() {
try {
return JSON.parse(fs.readFileSync('local_config.json'));
} catch (e) {
return false;
}
}
/**
* Build Tasks
*/
function buildSCSSwithInput(srcFile, destPath = 'dist/css') {
const basename = path.basename(srcFile, '.scss');
return src(srcFile)
.pipe(
plugins.plumber({
errorHandler: function (err) {
plugins.notify.onError({
title: 'SCSS Build Error',
message: err.message
})(err);
}
})
)
.pipe(plugins.sourcemaps.init())
.pipe(sass())
.on('error', sass.logError)
.pipe(plugins.autoprefixer())
.pipe(
plugins.cleanCss({
keepSpecialComments: 0
})
)
.pipe(plugins.rename(`${basename}.min.css`))
.pipe(plugins.sourcemaps.write('.'))
.pipe(dest(destPath))
.pipe(browsersync.stream())
.on(
'error',
plugins.notify.onError({
message: '<%= error.message %>',
title: 'SCSS Error'
})
);
}
function buildSCSStheme() {
return buildSCSSwithInput(srcFiles.scss);
}
function buildSCSSeditor() {
return buildSCSSwithInput(srcFiles.scssEditorStyles);
}
function buildJS() {
return src(srcFiles.js)
.pipe(
plugins.plumber({
errorHandler: function (err) {
plugins.notify.onError({
title: 'JS Build Error',
message: err.message
})(err);
}
})
)
.pipe(
plugins.notify({
message: 'Starting JS build',
title: 'JS Build'
})
)
.pipe(webpackStream(webpackConfig, webpack))
.pipe(dest('dist/js'));
}
/**
* Watch Tasks
*/
function watchSourceSCSS(cb) {
chokidar.watch(watchPaths.scss).on('change', (event, filePath) => {
console.log('SCSS file modified', filePath);
buildSCSStheme();
buildSCSSeditor();
});
cb();
}
function watchSourceJS(cb) {
chokidar.watch(watchPaths.js).on('change', (event, filePath) => {
console.log('JS file modified', filePath);
buildJS();
});
cb();
}
/**
* BrowserSync
*/
function startBrowserSync(cb) {
const localConfig = loadLocalConfig();
if (!localConfig) {
cb(
new Error(
'Unable to load "local_config.json" -- please use "local_config_example.json" as a template'
)
);
return;
}
browsersync.init(localConfig.browserSync);
chokidar.watch([distFiles.js].concat(watchPaths.php)).on('change', (event, filePath) => {
console.log('dist change', filePath);
browsersync.reload();
});
cb();
}
/**
* Exports
*/
exports.build_scss = parallel(buildSCSStheme, buildSCSSeditor);
exports.build_scss_theme = buildSCSStheme;
exports.build_scss_editor = buildSCSSeditor;
exports.build_js = buildJS;
exports.build = parallel(buildSCSStheme, buildSCSSeditor, buildJS);
exports.watch_scss = series(parallel(buildSCSStheme, buildSCSSeditor), watchSourceSCSS);
exports.watch_js = series(buildJS, watchSourceJS);
exports.watch = series(
parallel(buildSCSStheme, buildSCSSeditor, buildJS),
watchSourceSCSS,
watchSourceJS
);
exports.browsersync = startBrowserSync;
exports.default = series(
parallel(buildSCSStheme, buildSCSSeditor, buildJS),
watchSourceSCSS,
watchSourceJS,
startBrowserSync
);