-
Notifications
You must be signed in to change notification settings - Fork 98
/
ngc-pre-compiler.js
executable file
·56 lines (50 loc) · 2.24 KB
/
ngc-pre-compiler.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
#!/usr/bin/env node
'use strict';
/**
* this is to declare all INPUTS as a public variable for components and directives
* Without declaring all INPUTS variable, there will be an error in AoT compilation
* https://github.com/ng2-ui/map/pull/64
*/
var path = require("path");
var fs = require('fs-extra');
var tmpDir = path.join('tmp');
fs.ensureDirSync(tmpDir);
fs.emptyDirSync(tmpDir);
fs.copySync('src', tmpDir);
['components', 'directives', 'services'].forEach(dirName => {
let dirPath = path.join(tmpDir, dirName);
let files = fs.readdirSync(dirPath);
let js;
files.forEach(fileName => {
let jsPath = path.join(dirPath, fileName);
js = fs.readFileSync(jsPath, 'utf8');
// https://github.com/yahoo/strip-loader/blob/master/lib/index.js#L17
let regexPattern = new RegExp('(?:^|\\n)[ \\t]*(console.log)\\(((\\"[^\\"]*\\")|(\\\'[^\\\']*\\\')|[^\\);]|\\([^\\);]*\\))*\\)[ \\t]*(?:$|[;\\n])', 'g');
fs.writeFileSync(jsPath, js.replace(regexPattern, '\n'));
js = fs.readFileSync(jsPath, 'utf8');
let outputMatches = js.match(/OUTPUTS\s*=\s*(\[[^\]]*?\])/m);
if (outputMatches) {
let outputs = eval(outputMatches[1]);
let outputsJs = '\n // declare OUTPUTS for AOT compiler\n' +
outputs.map(input => ' public ' + input + ': any; // generated by '+path.basename(__filename)).join('\n') + '\n';
let replaceMatches = js.match(/(@Component|@Directive)\([\s\S]+?OUTPUTS[\s\S]+? class [\s\S]+?\{/m);
if (replaceMatches) {
let jsToReplace = js.replace(replaceMatches[0], $0 => $0 + outputsJs);
fs.writeFileSync(jsPath, jsToReplace);
}
}
js = fs.readFileSync(jsPath, 'utf8');
let inputMatches = js.match(/INPUTS\s*=\s*(\[[^\]]*?\])/m);
if (inputMatches) {
let inputs = eval(inputMatches[1]);
let inputsJs = '\n // declare INPUTS for AOT compiler\n'+
inputs.map(input => ' public ' + input + ': any; // generated by '+path.basename(__filename)).join('\n') +
'\n';
let replaceMatches = js.match(/(@Component|@Directive)\([\s\S]+?INPUTS[\s\S]+? class [\s\S]+?\{/m);
if (replaceMatches) {
let jsToReplace = js.replace(replaceMatches[0], $0 => $0 + inputsJs);
fs.writeFileSync(jsPath, jsToReplace);
}
}
});
});