-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
79 lines (68 loc) · 1.9 KB
/
build.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
// Requires
var fs = require('fs');
var UglifyJS = require("uglify-js");
var uglifycss = require('uglifycss');
var replace = require('replace-in-file');
// Vars
var scriptFileName = 'dataTables.contextualActions';
var cssFileName = 'dataTables.contextualActions';
var dir = __dirname;
var sourceFolder = dir + '/src/';
var distFolder = dir + '/dist/';
// Minification
var code = fs.readFileSync(sourceFolder + scriptFileName + '.js', 'utf8');
var uglifiedCode = UglifyJS.minify(
code,
{
mangle: {
toplevel: true,
},
nameCache: {}
}
).code;
var uglifiedCss = uglifycss.processFiles(
[ (sourceFolder + cssFileName+ '.css') ],
{ maxLineLen: 500, expandVars: true }
);
// Create dist folder if not exists
if (!fs.existsSync(distFolder)){
fs.mkdirSync(distFolder);
}
// Create the javascript
fs.writeFile(distFolder + scriptFileName + '.min.js', uglifiedCode , function (err){
if(err) {
console.log(err);
} else {
console.log('Minified javascript saved');
}
});
// Create the CSS
fs.writeFile(distFolder + cssFileName + '.min.css', uglifiedCss , function (err){
if(err) {
console.log(err);
} else {
console.log('Minified css saved');
}
});
// Copy over the index.html documentation to /docs/ with the script path replaced to use /dist/
fs.mkdir('./docs/', { recursive: true }, (err) => {if(err) throw err});
fs.copyFile('index.html', 'docs/index.html', (err) => {
if (err) throw err;
// Replace script path
replace({
files: 'docs/index.html',
from: `src="./src/dataTables.contextualActions.js"`,
to: `src="../dist/dataTables.contextualActions.min.js"`,
})
.then(() => {
// Replace style path
replace({
files: 'docs/index.html',
from: `href="./src/dataTables.contextualActions.css"`,
to: `href="../dist/dataTables.contextualActions.min.css"`,
})
.then(() => {
console.log('/docs/index.html built successfully');
});
});
});