This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-minify.js
74 lines (66 loc) · 2.12 KB
/
node-minify.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
/*
* grunt-node-minify
*
*
* Copyright (c) 2016-2018 Doug Simmons
* Licensed under the MIT license.
*/
'use strict';
var compressor = require ('node-minify');
var chalk = require('chalk');
module.exports = function(grunt) {
grunt.registerMultiTask('node-minify', 'Grunt plugin for node_minify', function() {
var that = this;
var done = that.async();
// Merge task-specific and/or target-specific options with these defaults.
var options = that.options({
});
var data = that.data;
if (that.files.length < 1) {
grunt.log.warn('Failed. No file targets were specified.');
done(false);
}
that.files.forEach(function (file) {
var input = file.src.filter(function (filepath) {
// Warn on and remove invalid source files.
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
}
return true;
});
if (input.length < 1) {
grunt.log.warn('Failed. No source files to minify.');
done(false);
} else {
compressor.minify({
compressor: (data.compressor !== undefined ? data.compressor : 'gcc'),
input: input,
output: file.dest,
sync: (data.sync !== undefined ? data.sync : false),
buffer: (data.buffer !== undefined ? data.buffer : 1024 * 1000),
publicFolder: data.publicFolder,
options: options,
callback: function (err, min) {
if (err) {
grunt.log.error(err);
}
if (process.env.GRUNT_NODE_MINIFY_VERBOSE) {
grunt.log.writeln(min);
}
}
})
.then(function (result) {
if (grunt.file.exists(file.dest)) {
grunt.log.ok('File ' + chalk.cyan(file.dest) + ' created.');
done();
}
})
.catch(function (error) {
grunt.log.warn('Failed to create file ' + chalk.yellow(file.dest) + ' due to ' + error + '.');
done(false);
});
}
});
});
};