This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
170 lines (151 loc) · 4.92 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
/**
* @file
* Gulpfile that builds the theme.
*
* Usage:
*
* For the first time run `npm install`. This installs the node modules needed
* from the 'package.json' file.
*
* Once you have installed the node modules, you can use the following commands:
* `npm run gulp`
* this will fire the default task, creating a minified css.
*
* `npm run gulp watch`
* this will watch for changes in the sass files, and crates a minified css.
*/
(function () {
'use strict';
const fs = require('fs');
let settings;
// Default structure.
if (fs.existsSync('./gulpsettings.js')) {
settings = require('./gulpsettings.js');
}
// Monorepo structure.
else if (fs.existsSync('../../gulpsettings.js')) {
settings = require('../../gulpsettings.js');
}
// Project structure.
else if (fs.existsSync('../../build/gulpsettings.js')) {
settings = require('../../build/gulpsettings.js');
} else {
console.error(
'================\nNo gulpsettings.js file detected.\nPlease refer to https://github.com/Pronovix/gulp to see how to create a gulpsettings.js file.\n================',
);
return process.exit(2);
}
function getConfig(configName) {
let config = [];
if (!(configName in settings) || settings[configName].length === 0) {
log('===== No ' + configName + ' config detected. =====');
} else {
config = settings[configName];
}
return config;
}
// Common packages.
const gulp = require('gulp');
const strip = require('gulp-strip-comments');
const log = require('fancy-log');
// SCSS Packages.
const sass = require('gulp-sass');
const gulpif = require('gulp-if');
const args = require('yargs').argv;
const postcss = require('gulp-postcss');
const postcssCustomProperties = require('postcss-custom-properties');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('autoprefixer');
const path = require('path');
const export_sass = require('node-sass-export');
// JS Packages.
const babel = require('gulp-babel');
const rename = require('gulp-rename');
// Default settings.
const defaultGlobs = ['!**/node_modules/**'];
function handleError(err) {
console.error('----------------------------------------\n' + '%s\n----------------------------------------', err);
if (args.ci) {
// If the task is run by CI, and something went wrong
// we should exit.
process.exit(1);
}
this.emit('end');
}
const cutES6 = (title) => {
log('File rename: ' + title + ' -> ' + title.replace('.es6', ''));
return title.replace('.es6', '');
};
function compileJs(globs, componentPath) {
globs = globs.concat(defaultGlobs);
return gulp
.src(globs, { cwd: componentPath })
.pipe(
babel({
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-class-properties'],
sourceType: 'unambiguous'
}).on('error', handleError),
)
.pipe(strip())
.pipe(
rename(function (path) {
path.basename = cutES6(path.basename);
}),
)
.pipe(gulp.dest(path.join(componentPath, 'js')));
}
function compileAllJs(done) {
let config = getConfig('js');
config.map(function (value) {
return compileJs(value.globs, value.path);
});
done();
}
function compileScss(globs, componentPath) {
globs = globs.concat(defaultGlobs);
return gulp
.src(globs, { cwd: componentPath })
.pipe(gulpif(args.debug, gulpif(!args.nosourcemap, sourcemaps.init())))
.pipe(
sass({
outputStyle: args.debug ? 'expanded' : 'compressed',
functions: export_sass(componentPath),
}).on('error', handleError),
)
.pipe(postcss([postcssCustomProperties()]))
.pipe(postcss([autoprefixer({ grid: 'true' })]))
.pipe(gulpif(!args.debug, strip.text()))
.pipe(gulpif(args.debug, gulpif(!args.nosourcemap, sourcemaps.write())))
.pipe(gulp.dest(path.join(componentPath, 'css')));
}
function compileAllScss(done) {
let config = getConfig('scss');
config.map(function (value) {
return compileScss(value.globs, value.path);
});
done();
}
function watchScss() {
let config = getConfig('scss');
for (let i = 0; i < config.length; i++) {
let globs = config[i].globs;
let componentPath = config[i].path;
gulp.watch(globs, { cwd: componentPath }, compileAllScss);
}
}
function watchJs() {
let config = getConfig('js');
for (let i = 0; i < config.length; i++) {
let globs = config[i].globs;
let componentPath = config[i].path;
gulp.watch(globs, { cwd: componentPath }, compileAllJs);
}
}
exports.default = gulp.parallel(compileAllScss, compileAllJs);
exports.scss = compileAllScss;
exports.watchscss = watchScss;
exports.js = compileAllJs;
exports.watchjs = watchJs;
exports.watch = gulp.parallel(compileAllScss, compileAllJs, watchScss, watchJs);
})();