-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
205 lines (173 loc) · 5.66 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
198
199
200
201
202
203
204
205
/* eslint-disable @typescript-eslint/no-var-requires */
const _ = require('lodash');
const autoprefixer = require('autoprefixer');
const esbuild = require('esbuild');
const fs = require('fs-extra');
const ghPages = require('gulp-gh-pages');
const globby = require('globby');
const gulp = require('gulp');
const livereload = require('livereload');
const nunjucks = require('nunjucks');
const open = require('open');
const os = require('os');
const path = require('path');
const periodicTable = require('.');
const pixrem = require('pixrem');
const postcss = require('postcss');
const postcssColorRgbaFallback = require('postcss-color-rgba-fallback');
const postcssOpacity = require('postcss-opacity');
const Promise = require('bluebird');
const sass = require('sass-embedded');
const webserver = require('gulp-webserver');
/* -----------------------------------------------------------------------------
* Config
---------------------------------------------------------------------------- */
const gulpConfig = require('./gulp-config.js');
const livereloadOpen =
(gulpConfig.webserver.https ? 'https' : 'http') +
'://' +
gulpConfig.webserver.host +
':' +
gulpConfig.webserver.port +
(gulpConfig.webserver.open ? gulpConfig.webserver.open : '/');
/* -----------------------------------------------------------------------------
* Misc
---------------------------------------------------------------------------- */
const flags = {
livereloadInit: false // Whether `livereload-init` task has been run
};
let server;
// Choose browser for node-open.
let browser = gulpConfig.webserver.browsers.default;
const platform = os.platform();
if (_.has(gulpConfig.webserver.browsers, platform)) {
browser = gulpConfig.webserver.browsers[platform];
}
/* -----------------------------------------------------------------------------
* Functions
---------------------------------------------------------------------------- */
async function buildCss(src, destDir) {
const files = await globby(src);
for (const file of files) {
let css = (await sass.compileAsync(file, gulpConfig.sass)).css;
css = (
await postcss([
autoprefixer(gulpConfig.autoprefixer),
pixrem,
postcssColorRgbaFallback,
postcssOpacity
]).process(css, {
from: undefined,
to: destDir
})
).css;
const cssMin = (
await esbuild.transform(css, {
loader: 'css',
minify: true,
legalComments: 'none'
})
).code;
const basename = path.basename(file);
const destPath = path.join(destDir, basename.replace(/\.scss$/, '.css'));
const destMinPath = path.join(
destDir,
basename.replace(/\.scss$/, '.min.css')
);
await fs.outputFile(destPath, css, 'utf-8');
await fs.outputFile(destMinPath, cssMin, 'utf-8');
}
}
/**
* Start a watcher.
*
* @param {Array} files
* @param {Array} tasks
* @param {Boolean} livereload Set to TRUE to force livereload to refresh the page.
*/
function startWatch(files, tasks, livereload) {
if (livereload) {
tasks.push('livereload-reload');
}
return gulp.watch(files, gulp.series(...tasks));
}
/* -----------------------------------------------------------------------------
* Livereload tasks
---------------------------------------------------------------------------- */
// Start webserver.
gulp.task('webserver-init', function (cb) {
const conf = _.clone(gulpConfig.webserver);
conf.open = false;
gulp.src('./').pipe(webserver(conf)).on('end', cb);
});
// Start livereload server
gulp.task('livereload-init', function (cb) {
if (!flags.livereloadInit) {
flags.livereloadInit = true;
server = livereload.createServer();
open(livereloadOpen, browser);
}
cb();
});
// Refresh page
gulp.task('livereload-reload', function (cb) {
server.refresh(livereloadOpen);
cb();
});
// Watch with livereload that doesn't rebuild docs
gulp.task('watch:livereload', function () {
const livereloadTask = 'livereload-reload';
_.forEach(gulpConfig.watchTasks, function (watchConfig) {
const tasks = _.clone(watchConfig.tasks);
tasks.push(livereloadTask);
startWatch(watchConfig.files, tasks);
});
});
/* -----------------------------------------------------------------------------
* Tasks
---------------------------------------------------------------------------- */
gulp.task('build-demo-css', async () =>
buildCss(['src/css/**/!(_)*.scss', 'src/demo/css/**/!(_)*.scss'], 'demo/css')
);
gulp.task('build-demo-page', function (cb) {
fs.mkdirpAsync('demo/')
.then(function () {
return Promise.props({
categories: periodicTable.loadCategories(),
elements: periodicTable.loadElements(),
groups: periodicTable.loadGroups()
});
})
.then(function (data) {
nunjucks.render('src/demo/index.njk', data, function (err, res) {
if (err) {
console.log(err);
cb();
} else {
fs.writeFileAsync('demo/index.html', res, 'utf-8').then(function () {
cb();
});
}
});
});
});
gulp.task('build-demo-vendor', function () {
return gulp
.src(['node_modules/normalize-css/**/*.css'])
.pipe(gulp.dest('demo/css/'));
});
gulp.task(
'build',
gulp.series('build-demo-css', 'build-demo-page', 'build-demo-vendor')
);
gulp.task('deploy', function () {
return gulp.src('demo/**/*').pipe(ghPages());
});
gulp.task(
'livereload',
gulp.series('build', 'webserver-init', 'livereload-init', 'watch:livereload')
);
/* -----------------------------------------------------------------------------
* Default task
---------------------------------------------------------------------------- */
gulp.task('default', gulp.series('build'));