-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
214 lines (188 loc) · 7.88 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
'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 BASEPATH = process.env.BASEPATH || '';
const TEMP = '.'; // TEMP
// 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 js() {
return src([BASEPATH + 'src/js/**/*.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(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(TEMP));
}
function js_compat() {
return src([BASEPATH + 'src/js/**/*.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(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(TEMP));
}
function js_uglify() {
return src([BASEPATH + 'dist/js/**/*.js', '!**/*.min.js', '!node_modules/**/*.js'])
// .pipe(sourcemaps.init())
.pipe(terser())
.pipe(rename(function(sourcePath) {
sourcePath.extname = '.min' + sourcePath.extname;
}))
// .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(TEMP));
}
function css() {
return src([BASEPATH + 'src/scss/**/*.scss', '!**/*_compat.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(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(TEMP));
}
function css_uglify() {
return src([BASEPATH + 'src/scss/**/*.scss', '!**/*_compat.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(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(TEMP));
}
function css_compat() {
return src([BASEPATH + 'src/scss/**/*_compat.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(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(TEMP));
}
function css_compat_uglify() {
return src([BASEPATH + 'src/scss/**/*_compat.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(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(TEMP));
}
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(TEMP));
}
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;