-
Notifications
You must be signed in to change notification settings - Fork 3
/
lasso-minify-js-plugin.js
57 lines (46 loc) · 1.61 KB
/
lasso-minify-js-plugin.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
var UglifyJS = require("uglify-js");
function minify(src, options) {
options = options || {};
options.fromString = true;
return UglifyJS.minify(src, options).code;
}
function isInline(lassoContext) {
if (lassoContext.inline === true) {
return true;
}
if (lassoContext.dependency && lassoContext.dependency.inline === true) {
return true;
}
return false;
}
module.exports = function (lasso, pluginConfig) {
lasso.addTransform({
contentType: 'js',
name: module.id,
stream: false,
transform: function(code, lassoContext) {
if (pluginConfig.inlineOnly === true && !isInline(lassoContext)) {
// Skip minification when we are not minifying inline code
return code;
}
try {
var minified = minify(code, pluginConfig);
if (minified.length && !minified.endsWith(";")) {
minified += ";";
}
return minified;
} catch(e) {
if (e.line) {
var dependency = lassoContext.dependency;
console.error('Unable to minify the following code for ' + dependency + ' at line ' + e.line + ' column '+ e.col + ':\n' +
'------------------------------------\n' +
code + '\n' +
'------------------------------------\n');
return code;
} else {
throw e;
}
}
}
});
};