-
Notifications
You must be signed in to change notification settings - Fork 29
/
Gruntfile.js
169 lines (144 loc) · 4.66 KB
/
Gruntfile.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module.exports = function(grunt) {
grunt.initConfig({
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: 'test/**/*.out.js'
}
}
});
grunt.registerTask('build', function() {
//var macro = '';
//var files = ['ki.sjs','react.sjs','core.sjs'];
//files.forEach(function(f) { macro += grunt.file.read('./src/' + f) + '\n'; });
// TODO: add filenames of macro files as arguments
var macro = grunt.file.read('./src/ki.sjs');
grunt.file.write('./macros/index.js', macro);
grunt.file.write('./editor/scripts/ki.sjs', macro);
var lib = grunt.file.read('./lib/ki.js');
grunt.file.write('./editor/scripts/ki.js', lib);
});
grunt.registerTask('build-test', function() {
var path = require('path');
var files = ['./test/core.js'];
files.forEach(function(f) {
grunt.file.write(f.replace('.js', '.out.js'), compileFile(f, true));
});
});
//grunt.registerTask('compile', function(fileName) {
// console.log(this.data);
// grunt.log.write(compileFile(fileName));
//});
var moduleCtx;
function compileFile(fileName, isTest) {
var macro = grunt.file.read('./macros/index.js');
var file = grunt.file.read(fileName);
var sweet = require('sweet.js');
if (!moduleCtx) moduleCtx = sweet.loadModule(macro);
return sweet.compile(file, {
modules: [moduleCtx]
}).code;
}
var parseMacros = function(code) {
var re = /ki *macro/
var current = code;
var start = current.search(re);
var ret;
var macros = [];
while (start != -1) {
current = current.substr(start);
ret = getNSExprs(current,2);
macros.push(ret.matches);
current = current.substr(ret.end);
start = current.search(re);
}
var rules = macros.map(function(macro) {
return 'rule { ' + macro[0] + ' } => { _sexpr ' + macro[1] + ' }'
});
return rules.join('\n');
}
grunt.registerTask('compile', function() {
var path = require('path');
var sweet = require('sweet.js');
var uglify = require('uglify-js');
var infile = grunt.option('in');
var outfile = grunt.option('out');
var sourcemap = grunt.option('sourcemap') || false;
var minify = grunt.option('minify') || false;
var compress = grunt.option('compress') || false;
var file;
if (infile) {
file = grunt.file.read(infile);
}
else if (argv._.length === 0) {
console.log(require("optimist").help());
return;
}
var ki_core = grunt.file.read('./macros/index.js');
var include = grunt.option('include');
var includes = typeof include === 'string' ? [include] : include;
var includeFiles = (includes || []).map(function(path) {
return grunt.file.read(path);
});
var rules = includeFiles.map(function(includeFile) {
return parseMacros(includeFile);
});
rules.push(parseMacros(file));
var module = ki_core.replace('/*__macros__*/',rules.join('\n'));
var options = {
filename: infile,
modules: [sweet.loadModule(module)]
};
var compile = function(file) {
var file = includeFiles.join('\n') + file;
if (sourcemap && outfile) {
options.sourceMap = true;
var result = sweet.compile(file, options);
var mapfile = path.basename(outfile) + '.map';
var code = result.code + '\n//# sourceMappingURL=' + mapfile;
var sourceMap = result.sourceMap;
var mapfile = outfile + '.map';
var tmpfile = outfile + '.tmp';
if (minify) {
grunt.file.write(tmpfile, sourceMap);
result = uglify.minify(code, {
fromString: true,
inSourceMap: tmpfile,
outSourceMap: mapfile,
compress: compress
});
code = result.code;
sourceMap = result.map;
grunt.file.delete(tmpfile);
}
grunt.file.write(outfile, code);
grunt.file.write(mapfile, sourceMap);
}
else if (outfile) {
var code = sweet.compile(file, options).code;
if (minify) {
code = uglify.minify(code, {fromString: true, compress: compress}).code;
}
grunt.file.write(outfile, code);
}
else {
var code = sweet.compile(file, options).code;
if (minify) {
code = uglify.minify(code, {fromString: true, compress: compress}).code;
}
console.log(code);
}
}
try {
compile(file);
}
catch (e) {
console.log(e);
}
});
grunt.registerTask('default', ['build']);
grunt.registerTask('test', ['build', 'build-test', 'mochaTest']);
grunt.loadNpmTasks('grunt-mocha-test');
};