-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuglify~extend.js
89 lines (70 loc) · 1.89 KB
/
uglify~extend.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
'use strict';
const {Transform} = require('stream');
const gulp = global.gulp || require('gulp');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const utils = require('fepper-utils');
const conf = global.conf;
const pref = global.pref;
// Set up pref.uglify.
pref.uglify = pref.uglify || {};
const jsBldDir = conf.ui.paths.source.jsBld;
const jsSrcDir = conf.ui.paths.source.jsSrc;
const streamUntouched = () => new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(file, enc, cb) {
this.push(file);
cb();
}
});
function getSourcemapDest() {
if (pref.uglify.sourceMap && pref.uglify.sourceMap.url !== 'inline') {
return '.';
}
return;
}
function getSourceRoot() {
if (pref.uglify.sourceMap && pref.uglify.sourceMap.root) {
const sourceRoot = pref.uglify.sourceMap.root;
return {sourceRoot};
}
return;
}
// Declare gulp tasks.
gulp.task('uglify', function () {
let sourcemapsInit = sourcemaps.init;
let sourcemapsWrite = sourcemaps.write;
// Do not write sourcemaps if pref.uglify.sourceMap is falsy.
if (!pref.uglify.sourceMap) {
sourcemapsInit = () => {
return streamUntouched();
};
sourcemapsWrite = () => {
return streamUntouched();
};
}
return gulp.src(jsSrcDir + '/**/*.js')
.pipe(sourcemapsInit())
.pipe(uglify(pref.uglify))
.pipe(sourcemapsWrite(getSourcemapDest(), getSourceRoot()))
.pipe(rename((path) => {
if (path.extname === '.js') {
path.extname = '.min.js';
}
}))
.pipe(gulp.dest(jsBldDir));
});
gulp.task('uglify:help', function (cb) {
let out = `
Fepper Uglify Extension
Use:
<task> [<additional args>...]
Tasks:
fp uglify Minify Fepper's frontend JavaScript files.
fp uglify:help Print fp-uglify tasks and descriptions.
`;
utils.info(out);
cb();
});