-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.ts
374 lines (312 loc) · 11.4 KB
/
plugin.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import * as path from 'path'
import * as esprima from 'esprima'
import * as beautify from 'js-beautify'
export class DeclerationsPlugin{
out: string
fs: any
main: string
constructor(options: any = {}){
this.out = options.out || './bundle.d.ts'
this.main = options.main
this.fs = options.fs || require('fs')
}
apply(compiler){
//Wait for compiler to emit files
compiler.plugin('done', (stats) => {
//Detect which out exists
let main = this.main
if (main){
if (typeof stats.compilation.options.entry !== 'string' && typeof stats.compilation.options.entry[main] === 'string'){
main = path.join(stats.compilation.options.output.path, stats.compilation.options.entry[main].replace('.ts', '.d.ts'))
}else{
main = path.join(stats.compilation.options.output.path, this.main)
}
}else{
if (typeof stats.compilation.options.entry !== 'string'){
throw new Error('TSLoaderDecleration: Must specify main option when using multiple entry points.')
}else{
main = path.join(stats.compilation.options.output.path, stats.compilation.options.entry.replace('.ts', '.d.ts'))
if (!this.fs.existsSync(main)){
main = path.join(stats.compilation.options.output.path, stats.compilation.options.output.filename.replace('.js', '.d.ts'))
}
}
}
//End now if build failed
if (!this.fs.existsSync(main)) {
return
}
//Create shared bundle and remove old source files
const out = path.join(stats.compilation.options.output.path, this.out)
const options = {
name: 'Module',
out,
main,
removeSource: true,
emitOnIncludedFileNotFound: true,
emitOnNoIncludedFileNotFound: true,
outputAsModuleFolder: true
}
//Generate merged bundles
try{
if (this.fs){
require('proxyquire').noCallThru()('dts-bundle/lib/index.js', { fs: this.fs }).bundle(options)
}else{
require('dts-bundle').bundle(options)
}
}catch(err){
console.error(err)
return
}
//Remove source directory
//rimraf.sync(path.join(stats.compilation.options.output.path, path.dirname(stats.compilation.options.entry)))
//Read decleration bundle from file
let bundle = this.fs.readFileSync(out).toString()
//Remove temporary values
bundle = bundle.split('\n').filter(line => {
return !line.startsWith('import \'./') && !line.startsWith('import \'../') && !line.startsWith('import \"./') && !line.startsWith('import \"../')
}).join('\n')
//Use current bundle if no modules found
if (bundle.indexOf('declare module \'') <= -1){
this.fs.writeFileSync(out, bundle)
return
}
//Prepare working variables
let newBundle = ''
let imports = {}
let exports = []
let externals: { [key: string]: string[] } = {}
//Loop through each trimmed module
const subBundles = bundle.split('declare module \'')
subBundles.shift()
for (let segment of subBundles){
let module = segment.trim()
const name = module.substring(0, module.indexOf("'"))
//Check whether module is entry
if (name === 'Module'){
//Prepare temp variables
let tempImports = []
let tempExports = []
let importing = false
let exporting = false
let exportingFile = false
let exportingAll = false
let filename = false
let start = 0
let offset = 1
let first = false
//Loop through each token in module
const tokens = esprima.tokenize("'" + module, { range: true })
for (let i = 0; i < tokens.length; i++){
const token = tokens[i]
const nextToken = tokens.length >= i + 1 ? tokens[i + 1] : undefined
//Start importing
if (token.type === 'Keyword' && token.value === 'import'){
importing = true
tempImports = []
start = token.range[0]
}
//Append import
if (importing && token.type === 'Identifier' && token.value !== 'from'){
tempImports.push(token.value)
}
//Look for import filename
if (importing && token.value === 'from'){
filename = true
}
if (importing && filename && token.type === 'String'){
importing = false
filename = false
//Merge or leave value if star
const name = token.value.replace(/\'/g, '')
if (imports[name] === undefined){
imports[name] = []
}
if (imports[name].indexOf('*') <= -1){
imports[name] = imports[name].concat(tempImports)
}
//Replace with whitespace
module = this.replace(start - offset, token.range[1] - (offset - 1), module)
offset += 1
}
//Exporting wildcard found
if (exporting && token.type === 'Punctuator' && token.value === '*'){
exportingAll = true
}
//Stop exporting type found
if (exporting && token.type === 'Keyword'){
exporting = false
}
//Start exporting
if (token.type === 'Keyword' && token.value === 'export'){
exporting = true
tempExports = []
start = token.range[0]
}
//Append export
if (exporting && !exportingAll && token.type === 'Identifier'){
exports.push(token.value)
tempExports.push(token.value)
}
//Stop exporting
if (exporting && token.type === 'Punctuator' && token.value === '}'){
exporting = false
//Check whether export ended
if (nextToken && nextToken.type === 'Identifier' && nextToken.value === 'from'){
exportingFile = true
}else{
//Replace with whitespace
module = this.replace(start - offset, token.range[1] - (offset - 1), module)
offset += 1
}
}
//Stop exporting file
if ((exportingFile || exportingAll) && token.type === 'String'){
//Merge or leave value if star
const name = token.value.replace(/\'/g, '')
if (imports[name] === undefined){
imports[name] = []
}
if (exportingAll){
imports[name] = [ '*' ]
}else if (imports[name].indexOf('*') <= -1){
imports[name] = imports[name].concat(tempExports)
}
//Reset vars
exportingFile = false
exportingAll = false
//Replace with whitespace
module = this.replace(start - offset, token.range[1] - (offset - 1), module)
offset += 1
}
}
//Remove module wrapper and add to buffer
module = this.replace(0, module.indexOf('{')+1, module)
module = this.replace(module.lastIndexOf('}'), module.length, module)
newBundle += module
}
//Check whether name exists in imports
if (!imports.hasOwnProperty(name)){
continue
}
//Loop through and extract external exports
let tempImports = []
let importing = false
let filename = true
for (const token of esprima.tokenize("'" + module)){
//Start importing
if (token.type === 'Keyword' && token.value === 'import'){
importing = true
tempImports = []
}
//Append import
if (importing && token.type === 'Identifier' && token.value !== 'from'){
tempImports.push(token.value)
}
//Look for import filename
if (importing && token.value === 'from'){
filename = true
}
if (importing && filename && token.type === 'String'){
importing = false
filename = false
//Add imports to externals
const name = token.value.replace(/\'/g, '')
//Check whether internal or external
if (name.startsWith('Module/')){
//Add to imports
if (imports[name].indexOf('*') <= -1){
imports[name] = tempImports
}
}else{
//Create externals category
if (!externals.hasOwnProperty(name)){
externals[name] = []
}
//Add imports to externals category if not existing
for (const imprt of tempImports){
if (externals[name].indexOf(imprt) <= -1){
externals[name].push(imprt)
}
}
}
}
}
//Find each import to include
for (const imprt of imports[name]){
//Prepare temp variables
let end = 0
let count = 0
let first = false
let type = false
let submodule
//Export entire module
if (imprt === '*'){
//Find range of exports to extract and remove imports
submodule = ''
for (const line of module.substring(module.indexOf('{') + 1).split('\n')){
if (line.trim().substring(0, 6) !== 'import'){
submodule += line + '\n'
}
}
end = submodule.lastIndexOf('}')
}
//Check for start position of const
const checkConst = new RegExp('export(\\s|\\s.*\\s)(const|let|var|function)\\s' + imprt + '(|\\s|\\s.*\\s)({|:)').exec(module)
if (imprt !== '*' && checkConst){
//Find range of import to extract
submodule = module.substring(checkConst.index)
end = submodule.indexOf('\n')
}
//Check for start position of import
const checkBraces = new RegExp('export(\\s|\\s.*\\s)(class|interface|enum)\\s' + imprt + '(|\\s|\\s.*\\s)({|:)').exec(module)
if (imprt !== '*' && checkBraces){
//Find range of import to extract
submodule = module.substring(checkBraces.index)
for (const token of esprima.tokenize(submodule, { range: true })){
if (token.type === 'Punctuator' && token.value === '{'){
count += 1
first = true
}
if (token.type === 'Punctuator' && token.value === '}'){
count -= 1
}
if (token.type === 'Punctuator' && token.value === '}' && count === 0 && first){
end = token.range[1]
break
}
}
}
//Extract and dump in new bundle
if (submodule){
newBundle += submodule.substring(0, end) + '\n'
}
}
}
console.log(imports)
console.log(exports)
//Remove externals which are not in use
Object.keys(externals).forEach(name => {
externals[name] = externals[name].filter(item => newBundle.indexOf(item) > -1)
//Remove if empty
if (externals[name].length <= 0){
delete externals[name]
}
})
//Prepare externals header
let header = ''
Object.keys(externals).forEach(name => {
header += 'import { ' + externals[name].join(', ') + ' } from "' + name + '" \n'
})
newBundle = header + newBundle
//Cleanup bundle formatting
if (newBundle.length > 0){
newBundle = beautify(newBundle.replace(/^ +/gm, '').replace(/\n\n/gm, ''), { indent_size: 1, indent_with_tabs: true })
}
//Write new bundle to file
this.fs.writeFileSync(out, newBundle || ' ')
})
}
replace(start, end, what) {
return what.substring(0, start) + Array(end-start).join(' ') + what.substring(end)
}
}