This repository was archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathplugin.ts
More file actions
110 lines (92 loc) · 2.82 KB
/
plugin.ts
File metadata and controls
110 lines (92 loc) · 2.82 KB
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
class DeclarationBundlerPlugin
{
out:string;
moduleName:string;
mode:string;
excludedReferences:string[];
constructor(options:any={})
{
this.out = options.out ? options.out : './build/';
this.excludedReferences = options.excludedReferences ? options.excludedReferences : undefined;
if(!options.moduleName)
{
throw new Error('please set a moduleName if you use mode:internal. new DacoreWebpackPlugin({mode:\'internal\',moduleName:...})');
}
this.moduleName = options.moduleName;
}
apply(compiler)
{
//when the compiler is ready to emit files
compiler.hooks.emit.tapAsync('DeclarationBundlerPlugin', (compilation,callback) =>
{
//collect all generated declaration files
//and remove them from the assets that will be emited
var declarationFiles = {};
for (var filename in compilation.assets)
{
if(filename.indexOf('.d.ts') !== -1)
{
declarationFiles[filename] = compilation.assets[filename];
delete compilation.assets[filename];
}
}
//combine them into one declaration file
var combinedDeclaration = this.generateCombinedDeclaration(declarationFiles);
//and insert that back into the assets
compilation.assets[this.out] = {
source: function() {
return combinedDeclaration;
},
size: function() {
return combinedDeclaration.length;
}
};
//webpack may continue now
callback();
});
}
private generateCombinedDeclaration(declarationFiles:Object):string
{
var declarations = '';
for(var fileName in declarationFiles)
{
var declarationFile = declarationFiles[fileName];
// The lines of the files now come as a Function inside declaration file.
var data = declarationFile.source();
var lines = data.split("\n");
var i = lines.length;
while (i--)
{
var line = lines[i];
//exclude empty lines
var excludeLine:boolean = line == "";
//exclude export statements
excludeLine = excludeLine || line.indexOf("export =") !== -1;
//exclude import statements
excludeLine = excludeLine || (/import ([a-z0-9A-Z_-]+) = require\(/).test(line);
//if defined, check for excluded references
if(!excludeLine && this.excludedReferences && line.indexOf("<reference") !== -1)
{
excludeLine = this.excludedReferences.some(reference => line.indexOf(reference) !== -1);
}
if (excludeLine)
{
lines.splice(i, 1);
}
else
{
if (line.indexOf("declare ") !== -1)
{
lines[i] = line.replace("declare ", "");
}
//add tab
lines[i] = "\t" + lines[i];
}
}
declarations += lines.join("\n") + "\n\n";
}
var output = "declare module "+this.moduleName+"\n{\n" + declarations + "}";
return output;
}
}
export = DeclarationBundlerPlugin;