-
Notifications
You must be signed in to change notification settings - Fork 2
/
tsbundle.js
271 lines (252 loc) · 10.5 KB
/
tsbundle.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
const PluginError = require('plugin-error');
const fs = require("fs");
const ts = require("typescript");
const path = require("path");
const os = require("os");
const through = require("through2");
const Vinyl = require('vinyl');
const formatter = require('typescript-formatter');
const {PLUGIN_NAME, copyright} = require("./util");
function tsbundle(args)
{
const inputs = [];
let modules;
return through.obj(
function(file, enc, cb)
{
if(file.path == "module.json")
{
modules = JSON.parse(file.contents.toString().replace(/\\/g, "\\\\"));
}
if(!file.path.endsWith(".d.ts"))
{
//
// Let non TypeScript files pass-through
//
cb(null, file);
}
else
{
inputs.push(file);
cb();
}
},
function(cb)
{
if(inputs.length == 0)
{
cb();
}
else
{
//
// Create a bundle for each js:module and a default bundle for files doesn't
// belong to any module
//
const all = [];
const names = modules === undefined ? [""] :
modules.map(m => m.module).filter((v, i, a) => a.indexOf(v) == i);
for(const key of names)
{
//
// List of imports included in all files that are part of the bundle
//
const imports = [];
const namespaces = [""];
const definitions = new Map();
definitions.set("", "");
let namespace = "";
function visitFile(file, data)
{
visitNode(ts.createSourceFile(file, data, ts.ScriptTarget.ES7, true));
}
function exportDefinition(node)
{
let out = definitions.get(namespace);
const data = node.getText().trim();
if(data != "")
{
out += os.EOL;
if(node.jsDoc)
{
out += "/**" + os.EOL;
for(const l of node.jsDoc[0].comment.split(os.EOL))
{
out += " * " + l + os.EOL;
}
out += " */" + os.EOL;
}
if(data.indexOf("export ") == -1)
{
out += "export ";
}
out += data;
out += os.EOL;
definitions.set(namespace, out);
}
}
function visitNode(node, parent)
{
switch(node.kind)
{
case ts.SyntaxKind.SourceFile:
{
for(const s of node.referencedFiles)
{
const f = path.normalize(path.join(path.dirname(node.fileName), s.fileName));
visitFile(f, fs.readFileSync(f).toString());
}
if(node.referencedFiles == 0)
{
ts.forEachChild(node, child =>
{
visitNode(child, node);
});
}
break;
}
case ts.SyntaxKind.ModuleDeclaration:
{
if(node.modifiers && node.modifiers.some(n => n.kind == ts.SyntaxKind.DeclareKeyword))
{
if(node.name.text == "ice")
{
ts.forEachChild(node.body, child =>
{
visitNode(child, node);
});
}
}
else if((parent.modifiers && parent.modifiers.some(
n => n.kind == ts.SyntaxKind.DeclareKeyword)))
{
namespace = node.name.text;
if(namespaces.indexOf(namespace) == -1)
{
namespaces.push(namespace);
definitions.set(namespace, "");
}
ts.forEachChild(node.body, child =>
{
visitNode(child, node);
});
namespace = "";
}
else
{
exportDefinition(node);
}
break;
}
case ts.SyntaxKind.ClassDeclaration:
{
exportDefinition(node);
break;
}
case ts.SyntaxKind.ImportDeclaration:
{
const s = node.getText().trim();
if(!imports.find(e => e.getText().trim() == s))
{
imports.push(node);
}
break;
}
case ts.SyntaxKind.ImportClause:
{
break;
}
default:
{
exportDefinition(node);
break;
}
}
}
let input = "";
if(modules)
{
const files = modules.filter(m => m.module == key && m.file.endsWith(".d.ts")).map(m => m.file);
inputs.filter(i => files.find(f => path.resolve(f) == path.resolve(i.path))).forEach(
i =>
{
input += i.contents.toString();
});
}
else
{
inputs.forEach(
i =>
{
input += i.contents.toString();
});
}
visitFile("", input);
let data = "";
for(const name of namespaces)
{
if(name == "")
{
data += definitions.get(name);
}
else
{
data += `\nexport namespace ${name}`;
data += "\n{";
data += `\n${definitions.get(name)}`;
data += "\n}";
}
}
let output = copyright;
output += "\n";
//
// Write the bundled imports filtering out imports that correspond
// to files in this bundle.
//
imports.filter(e => !inputs.find(f => path.resolve(`${e.moduleSpecifier.text}.d.ts`) ==
path.resolve(f.path))).forEach(
e =>
{
output += `\n${e.getText().trim()}`;
});
//
// Remove the import prefixes from identfiers, for prefixes that
// correspond to files included in the bundle.
//
imports.filter(
e => inputs.find(f => path.resolve(`${e.moduleSpecifier.text}.d.ts`) ==
path.resolve(f.path))).forEach(
e =>
{
const name = e.importClause.namedBindings.name.escapedText;
data = data.replace(new RegExp(`([(-,-:]*)(${name}\\.)`, "g"), "$1");
});
output += `\n${data}`;
const p = formatter.processString("", output, {}).then(
result =>
{
this.push(new Vinyl(
{
cwd: "./",
path: key === "" ? "generated.d.ts" : `${key}.d.ts`,
contents: Buffer.from(result.dest)
}));
});
all.push(p);
}
Promise.all(all).then(
() =>
{
cb();
},
err =>
{
cb(new PluginError(PLUGIN_NAME, `error generating TypeScript bundle: ${err}`));
});
}
});
}
module.exports = tsbundle;