forked from droyson/types-webpack-bundler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ts
117 lines (97 loc) · 3.75 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
import * as webpack from 'webpack'
class DeclarationBundlerPlugin {
out: string;
moduleName: string;
mode: string;
excludedReferences: string[];
excludedFolders: string[];
constructor(options: any = {}) {
this.out = options.out ? options.out : './build/index.d.ts';
this.excludedReferences = options.excludedReferences ? options.excludedReferences : undefined;
this.excludedFolders = options.excludedFolders ? options.excludedFolders : [];
if (!options.moduleName) {
throw new Error('please set a moduleName if you use mode:internal. new DeclarationBundlerPlugin({mode:\'internal\',moduleName:...})');
}
this.moduleName = options.moduleName;
}
apply(compiler: webpack.Compiler) {
const { webpack } = compiler
const { Compilation } = webpack
const { RawSource } = webpack.sources
compiler.hooks.thisCompilation.tap('DeclarationBundlerPlugin', (compilation: webpack.Compilation) => {
compilation.hooks.processAssets.tap({
name: 'DeclarationBundlerPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE
}, (assets) => {
//collect all generated declaration files
//and remove them from the assets that will be emitted
const declarationFiles: Object = {}
for (const filename in assets) {
if (filename.search(/\.d\.ts$/) !== -1) {
declarationFiles[filename] = assets[filename]
compilation.deleteAsset(filename)
}
}
//combine them into one declaration file
const combinedDeclaration = this.generateCombinedDeclaration(declarationFiles)
//and insert that back into the assets
compilation.emitAsset(this.out, new RawSource(combinedDeclaration))
})
});
}
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;
let isExcluded = false;
for(let excludedFolder of this.excludedFolders) {
if(fileName.indexOf(excludedFolder) == 0) {
console.log("Excluding " + fileName + " from declaration bundling.");
isExcluded = true;
break;
}
}
if(isExcluded)
continue;
while (i--) {
var line = lines[i];
//exclude empty lines
var excludeLine: boolean = line == "";
//exclude export statements
excludeLine = excludeLine || line.indexOf("export =") !== -1 ||
line.search(/^\s*export type \{/) !== -1 ||
line.search(/^\s*export \{/) !== -1 ||
line.search(/^\s*export \*/) !== -1;
//exclude import statements
excludeLine = excludeLine ||
(/import ([a-z0-9A-Z_-]+) = require\(/).test(line) ||
(/import ([a-z0-9A-Z_-]+)/).test(line) ||
(/import [\"\']/).test(line) ||
(/import \{/).test(line) ||
(/import \*/).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;