-
Notifications
You must be signed in to change notification settings - Fork 7
/
transpile.ts
131 lines (115 loc) · 4.22 KB
/
transpile.ts
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
import * as glob from 'glob';
import { translateCode, translateCodeAndWatch } from '../codegen/translateCode';
import * as path from 'path';
import * as fs from 'fs';
import * as iconv from 'iconv-lite';
import { ModuleRegistry } from '../cjsModules/moduleRegistry';
import ncp = require('ncp');
import { CliOptions, NodeHooks, LogObj } from '../../types';
import { sync as mkdirpSync } from 'mkdirp';
import { transpileModule, ModuleKind } from 'typescript';
const replace = require('stream-replace');
const joinGlobs = (globs: string[]) => globs.length > 1 ? `{${globs.join(',')}}` : globs[0];
export function transpile(options: CliOptions, baseDir: string, outDir: string, log: LogObj) {
const namespaces = {
root: options.rootNs,
builtins: options.builtinsNs || (options.rootNs ? options.rootNs + '\\Builtins' : 'Builtins'),
};
const builtinsRelativePath = ModuleRegistry.namespaceToPath(namespaces.builtins);
let builtinsPath: string;
if (options.rewriteBuiltinsRoot) {
if (!options.builtinsNs) {
log.warn('builtinsNs option should be provided if rewriteBuiltinsRoot is used', []);
}
builtinsPath = path.join(options.rewriteBuiltinsRoot, builtinsRelativePath);
} else {
builtinsPath = path.resolve(__dirname, '..', '..', '..', 'builtins');
}
const serverFilesRoot = options.serverBaseDir ?? options.baseDir;
let hooks: NodeHooks = {};
if (options.hooksIncludePath) {
try {
let result = transpileModule(
fs.readFileSync(options.hooksIncludePath, { encoding: 'utf-8' }),
{ compilerOptions: { module: ModuleKind.CommonJS } }
);
// eslint-disable-next-line no-eval
const data: ElephizeNodeHookEntry[] = eval(result.outputText);
hooks = {
...(data.reduce<NodeHooks>((acc, entry) => {
acc[entry.nodeKind] = { run: entry.hook };
return acc;
}, {})),
};
} catch (e) {
log.error('Failed to load AST hooks: %s', [e.toString()]);
hooks = {};
}
}
glob(Array.isArray(options.src) ? joinGlobs(options.src) : options.src, (e: Error, matches: string[]) => {
if (e) {
log.error('%s', [e.toString()]);
process.exit(1);
return;
}
const compilerOptions = {
baseUrl: baseDir,
paths: options.tsPaths || {},
};
(options.watch ? translateCodeAndWatch : translateCode)(
matches.map((p) => path.resolve('./', p)),
options.ignoreImports,
options.replaceImports,
options.tsPaths,
log,
{
baseDir,
serverFilesRoot,
builtinsPath,
aliases: options.aliases,
sourceExtensions: options.sourceExtensions,
namespaces,
printImportTree: options.printImportTree,
encoding: options.encoding || 'utf-8',
disableCodeElimination: options.noZap,
compilerOptions,
onData: (sourceFilename: string, targetFilename: string, content: string) => onData(targetFilename, content),
onFinish,
jsxPreferences: options.jsxPreferences || {},
hooks,
}
);
});
function onData(filename: string, content: string) {
const outputFilename = outDir + '/' + filename;
log.info('Emitting file: %s', [outputFilename]);
const outputDir = path.dirname(outputFilename);
mkdirpSync(outputDir);
fs.writeFileSync(outputFilename, iconv.encode(content, options.encoding || 'utf-8'));
}
function onFinish() {
if ((log.errCount || 0) > 0 && options.bail === 'error') {
process.exit(1);
}
if ((log.errCount || 0) + (log.warnCount || 0) > 0 && options.bail === 'warn') {
process.exit(1);
}
if (options.rewriteBuiltinsRoot) {
log.special('Skip builtins copy bacause rewriteBuiltinsRoot options is provided', []);
return;
}
const bTgt = path.join(outDir, ModuleRegistry.namespaceToPath(namespaces.builtins));
log.special('Copying builtins files', []);
log.special('From: %s', [builtinsPath]);
log.special('To: %s', [bTgt]);
ncp(builtinsPath, bTgt, {
transform: function(read, write) {
read.pipe(replace(/__ROOTNS__\\Builtins/g, namespaces.builtins)).pipe(write);
},
}, (err) => {
if (!err) {
log.special('Builtins base files successfully copied', []);
}
});
}
}