-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuglify-js-example-script
61 lines (57 loc) · 1.77 KB
/
uglify-js-example-script
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
const UglifyJS = require('uglify-js')
const path = require('path')
const fs = require('fs')
const SRC = path.resolve(__dirname, '..', '')
const DIST = path.resolve(__dirname, '..', '', 'dist')
const ASSETS = '/dist'
var cacheFileName = SRC+"/build/tmp/cache.json";
var options = {
toplevel: true,
mangle: {
toplevel: true,
properties: true
},
nameCache: JSON.parse(fs.readFileSync(cacheFileName, "utf8")),
compress: {
// global_defs: {
// "@console.log": "alert"
// },
passes: 1
},
output: {
beautify: false,
preamble: "/* Copyright 2019 by feeling4design | @RensTillmann */"
}
};
var js_folder = SRC+"/assets/js/";
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function(dir, filelist) {
var needle = 'super-forms';
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}else{
if(path.extname(file)==='.js'){
var slice_position = dir.indexOf(needle);
filelist.push({
path : dir.slice(slice_position+needle.length),
name : file
});
}
}
});
return filelist;
};
var files = walkSync(js_folder);
files.forEach(function (file, index) {
// var code = fs.readFileSync(file, 'utf8');
fs.mkdir(DIST+file.path, { recursive: true }, (err) => {
if (err) throw err;
var code = fs.readFileSync(SRC+file.path+file.name, 'utf8');
fs.writeFileSync(DIST+file.path+file.name, UglifyJS.minify(code, options).code, 'utf8');
fs.writeFileSync(cacheFileName, JSON.stringify(options.nameCache), 'utf8');
});
});