forked from lukeed/taskr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.js
60 lines (47 loc) · 1.49 KB
/
next.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
/**
* For Uglify v3
* --- currently sourceMaps not supported:
* Error: `sourceMap` is not a supported option
*/
'use strict';
const extn = require('path').extname;
const minify = require('uglify-js').minify;
const errMsg = obj => `${obj.message} in \`${obj.filename}\` on line ${obj.line}`;
module.exports = function (task) {
task.plugin('uglify', {}, function * (file, opts) {
const ext = extn(file.base);
const rgx = new RegExp(`\\${ext}$`, 'i');
const filename = file.base.replace(rgx, '.js');
let isInline = false;
let isMapping = false;
opts = Object.assign({ sourceMap:isMapping }, opts);
if (opts.sourceMap === 'inline') {
isInline = true;
opts.sourceMap = { url:'inline' };
} else if (opts.sourceMap !== isMapping) {
isMapping = true;
const url = `${filename}.map`;
opts.sourceMap = Object.assign({ filename, url }, opts);
// check again... just in case
if (opts.sourceMap.url === 'inline') {
isInline = true;
}
}
const res = minify({ [file.base]:file.data.toString() }, opts);
console.log('THIS IS RES', res);
// check for errors
if (res.error) {
return task.emit('plugin_error', {
plugin: '@taskr/uglify',
error: errMsg(res.error)
});
}
file.base = filename; // ensure always `.js`
file.data = new Buffer(res.code); // write output
// handle external sourcemaps (only)
if (res.map !== void 0 && isMapping && !isInline) {
// Uglify 3.0 will auto-write a comment link
console.log('HAS MAP', res.map);
}
});
};