-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.js
54 lines (45 loc) · 1.07 KB
/
type.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
const {getOptions} = require('loader-utils');
const generateType = require('./lib/generateType');
const output = require('./lib/output');
// loader for d.ts auto generation
module.exports = function(source, map) {
if (this.cacheable) {
this.cacheable();
}
const {resourcePath} = this;
this.addDependency(resourcePath);
const callback = this.async();
const {indent, template, banner} = getOptions(this) || {};
let indentSize = 2;
let indentType = 'space';
if (indent) {
if (['space', 'tab'].indexOf(indent.type) > -1) {
indentType = indent.type;
}
if (indent.size != null) {
indentSize = indent.size;
} else if (indentType === 'tab') {
indentSize = 1;
}
}
const type = generateType({
source,
options: {
indentSize,
indentType,
template,
banner,
},
});
if (!type) {
callback(null, source, map);
}
const filePath = `${resourcePath}.d.ts`;
output(filePath, type, (err, result) => {
if (err) {
callback(err);
} else {
callback(null, source, map);
}
});
};