-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
311 lines (272 loc) · 11 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'use strict';
const path = require('path');
const { readFileSync } = require('fs');
const { series, parallel, watch, src, dest } = require('gulp');
const tap = require('gulp-tap');
const rename = require('gulp-rename');
const babel = require('gulp-babel');
const sass = require('gulp-sass')(require('sass'));
const terser = require('gulp-terser');
const sourcemaps = require('gulp-sourcemaps');
const rollup = require('@rbnlffl/gulp-rollup');
const project = {
'ensemble-common': {
name: 'loltgt/ensemble [common]',
version: '0.0.2',
link: 'https://github.com/loltgt/ensemble'
},
'ensemble-modal': {
name: 'loltgt/ensemble.Modal',
version: '0.0.2',
link: 'https://github.com/loltgt/ensemble-modal'
},
'ensemble-lightbox': {
name: 'loltgt/ensemble.Lightbox, loltgt/ensemble.Modal',
version: '0.0.2',
link: 'https://github.com/loltgt/ensemble-lightbox'
},
'ensemble-social-share': {
name: 'loltgt/ensemble.SocialShare',
version: '0.0.2',
link: 'https://github.com/loltgt/ensemble-social-share'
}
};
const BASEPATH = process.env.BASEPATH || '';
const DSTPATH = '.';
// dist dir from "src" to "dst" replacement
function _dst(dir, src, dst) {
return dir.indexOf('/') === -1 ? (dir + '/' + dst) : dir.replace(src, dst);
}
// removes cjs formatting generated by rollup
function _crt(contents) {
contents = contents.replace('Object.defineProperty(exports, \'__esModule\', { value: true });', '')
.replace(/exports\.([^ ]+) = [^;]+;/, 'export { $1 };')
.replace(/exports\.([^ ]+) = [^;]+;/, 'export { $1 };');
return contents;
}
// camelCase to hyphenated
function _cth(name) {
return name.replace(/([A-Z][^A-Z]+)/g, (cap, w, n) => (n ? '-' : '') + cap.toLowerCase());
}
function _remove_debug(file) {
let contents = file.contents.toString();
contents = contents.replace(/console\.(log|error)\([^\)]+\);/g, '');
file.contents = Buffer.from(contents);
}
function _remove_comments(file) {
let contents = file.contents.toString();
contents = contents.replace(/(\/[\*]+)(=?[\w\W]+?)(\*\/)/g, '');
file.contents = Buffer.from(contents);
}
function _force_inline(file) {
let contents = file.contents.toString();
contents = contents.replace(/\n/g, '');
file.contents = Buffer.from(contents);
}
function _bundle_banner(filename) {
let pathName = path.parse(filename).name.replace('.min', '');
let projectName;
if (pathName.indexOf('-compat') != -1) {
pathName = pathName.replace('-compat', '');
projectName = project[pathName].name + ' (compat)';
} else {
projectName = project[pathName].name;
}
return '/*!\r\
* ' + projectName + '\r\
*\r\
* @version ' + project[pathName].version + '\r\
* @link ' + project[pathName].link + '\r\
* @copyright Copyright (C) Leonardo Laureti\r\
* @license MIT License\r\
*/\r';
}
function js() {
return src([BASEPATH + 'src/js/**/*.js', '!node_modules/**/*.js', '!**/node_modules/**/*.js'])
// .pipe(sourcemaps.init())
.pipe(rollup(null, { sourcemap: true, format: 'cjs' }))
.pipe(tap(function(file) {
file.contents = Buffer.from(_crt(file.contents.toString()));
}))
.pipe(babel()) // sourcemap source reset
.pipe(rename(function(sourcePath) {
sourcePath.dirname = _dst(sourcePath.dirname, 'src', 'dist');
sourcePath.basename = 'ensemble-' + _cth(sourcePath.basename);
}))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function js_uglify() {
return src([BASEPATH + 'dist/js/**/*.js', '!**/*.min.js', '!node_modules/**/*.js', '!**/node_modules/**/*.js'])
// .pipe(sourcemaps.init())
.pipe(terser({ keep_classnames: true, keep_fnames: true }))
.pipe(rename(function(sourcePath) {
sourcePath.extname = '.min' + sourcePath.extname;
}))
// .pipe(tap(_remove_debug))
.pipe(tap(_remove_comments))
.pipe(tap(_force_inline))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function js_compat() {
return src([BASEPATH + 'src/js/**/*.js', '!node_modules/**/*.js', '!**/node_modules/**/*.js'])
// .pipe(sourcemaps.init())
.pipe(rollup(null, { sourcemap: true, format: 'cjs' }))
.pipe(tap(function(file) {
file.contents = Buffer.from(_crt(file.contents.toString()));
}))
.pipe(babel({
presets: [
[ '@babel/preset-env', { targets: 'defaults' } ]
],
plugins: [
["@babel/plugin-transform-object-assign"]
]
})) // sourcemap source reset
.pipe(tap(function(file) {
let contents = file.contents.toString();
const missing_regeneratorRuntime = readFileSync('./node_modules/regenerator-runtime/runtime.js');
contents = contents.replace(' function _asyncToGenerator(fn)', '\n\n' + missing_regeneratorRuntime + '\n\n function _asyncToGenerator(fn)');
file.contents = Buffer.from(contents);
}))
.pipe(rename(function(sourcePath) {
sourcePath.dirname = _dst(sourcePath.dirname, 'src', 'dist');
sourcePath.basename = 'ensemble-' + _cth(sourcePath.basename) + '-compat';
}))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function css() {
return src([BASEPATH + 'src/scss/**/*.scss', '!**/*_compat.scss', '!node_modules/**/*.scss', '!**/node_modules/**/*.scss'])
// .pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(rename(function(sourcePath) {
sourcePath.dirname = _dst(sourcePath.dirname, 'src', 'dist');
sourcePath.dirname = sourcePath.dirname.replace('scss', 'css');
sourcePath.basename = 'ensemble-' + sourcePath.basename;
}))
.pipe(tap(_remove_comments))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function css_uglify() {
return src([BASEPATH + 'src/scss/**/*.scss', '!**/*_compat.scss', '!node_modules/**/*.scss', '!**/node_modules/**/*.scss'])
// .pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(rename(function(sourcePath) {
sourcePath.dirname = _dst(sourcePath.dirname, 'src', 'dist');
sourcePath.dirname = sourcePath.dirname.replace('scss', 'css');
sourcePath.basename = 'ensemble-' + sourcePath.basename;
sourcePath.extname = '.min' + sourcePath.extname;
}))
.pipe(tap(_remove_comments))
.pipe(tap(_force_inline))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function css_compat() {
return src([BASEPATH + 'src/scss/**/*_compat.scss', '!node_modules/**/*.scss', '!**/node_modules/**/*.scss'])
// .pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(rename(function(sourcePath) {
sourcePath.dirname = _dst(sourcePath.dirname, 'src', 'dist');
sourcePath.dirname = sourcePath.dirname.replace('scss', 'css');
sourcePath.basename = 'ensemble-' + sourcePath.basename.replace('_', '-');
}))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function css_compat_uglify() {
return src([BASEPATH + 'src/scss/**/*_compat.scss', '!node_modules/**/*.scss', '!**/node_modules/**/*.scss'])
// .pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(rename(function(sourcePath) {
sourcePath.dirname = _dst(sourcePath.dirname, 'src', 'dist');
sourcePath.dirname = sourcePath.dirname.replace('scss', 'css');
sourcePath.basename = 'ensemble-' + sourcePath.basename.replace('_', '-');
sourcePath.extname = '.min' + sourcePath.extname;
}))
.pipe(tap(_remove_comments))
.pipe(tap(_force_inline))
.pipe(tap(function(file) {
file.contents = Buffer.from(_bundle_banner(file.basename) + file.contents.toString());
}))
// .pipe(sourcemaps.mapSources(function(sourcePath, file) {
// const base = path.relative(file.cwd, file.dirname);
// return path.relative(base, sourcePath);
// }))
// .pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(dest(DSTPATH));
}
function demo_css() {
return src('./ensemble-stack-d1/misc/demo/*.scss')
// .pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(rename(function(sourcePath) {
sourcePath.dirname += '/ensemble-' + sourcePath.basename + '/demo';
sourcePath.basename = 'demo-ensemble-' + sourcePath.basename;
}))
// .pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: './ensemble-stack-d1/misc/demo' }))
.pipe(dest(DSTPATH));
}
function watcher() {
watch(BASEPATH + 'src/scss/**/*.scss', build_css);
watch(BASEPATH + 'src/js/**/*.js', build_js);
}
const build_js = series([js, js_uglify]);
const build_css = parallel([css, css_uglify]);
const compat_js = series([js_compat, js_uglify]);
const compat_css = parallel([css_compat, css_compat_uglify]);
const build = parallel(build_js, build_css);
const compat = parallel(compat_js, compat_css);
exports.default = build;
exports.build_js = build_js;
exports.build_css = build_css;
exports.compat = compat;
exports.compat_js = compat_js;
exports.compat_css = compat_css;
exports.watch = watcher;
exports.demo = demo_css;