-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
58 lines (42 loc) · 2.22 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
var fs = require('fs');
String.prototype.toJSON = function(){
return JSON.parse(this)
};
var config = fs.readFileSync(__dirname+'/package.json').toString().toJSON();
var head = fs.readFileSync(__dirname+'/src/head').toString();
var compiler = fs.readFileSync(__dirname+'/src/compiler.js').toString();
var lex = fs.readFileSync(__dirname+'/src/lex.js').toString();
var sauerkraut = fs.readFileSync(__dirname+'/src/sauerkraut.js').toString();
var browser = fs.readFileSync(__dirname+'/src/sauerkraut.browser.js').toString();
var fileSystem = fs.readFileSync(__dirname+'/src/sauerkraut.browser.fs.js').toString();
head = head.replace(/@NAME/g, config.name.toString());
head = head.replace(/@DESCRIPTION/g, config.description.toString());
head = head.replace(/@VERSION/g, config.version.toString());
head = head.replace(/@URL/g, config.url.toString());
head = head.replace(/@AUTHOR/g, config.author.toString());
head = head.replace(/@CONTRIBUTORS/g, config.contributors.toString());
compiler = compiler.substr(compiler.indexOf('var SauerkrautCompiler ='));
sauerkraut = sauerkraut.substr(sauerkraut.indexOf('var Sauerkraut ='));
var output = head+"\n\n"+fileSystem+"\n\n"+lex+"\n\n"+compiler+"\n\n"+sauerkraut+"\n\n"+browser;
output = output.replace(/ = exports = module.exports/g, '');
var phpHeaders = "<?php header('Content-Type: text/javascript; charset=utf-8'); ?>\n"
fs.writeFile(__dirname+'/dist/sauerkraut.js', output);
fs.writeFile(__dirname+'/dist/sauerkraut-'+config.version.toString()+'.js', output);
console.log('Successfully compiled v-' + config.version.toString());
try
{
// Minify output
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var ast = jsp.parse(output); // parse code and get the initial AST
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
output = pro.gen_code(ast); // compressed code here
fs.writeFile(__dirname+'/dist/sauerkraut.min.js', output);
fs.writeFile(__dirname+'/dist/sauerkraut-'+config.version.toString()+'.min.js', output);
console.log('Successfully minified v-' + config.version.toString());
}
catch(e)
{
console.log("Cannot minify Output. Try:\nnpm install uglify-js");
}