-
Notifications
You must be signed in to change notification settings - Fork 27
/
ifdef-loader.ts
49 lines (40 loc) · 1.46 KB
/
ifdef-loader.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
import * as loaderUtils from 'loader-utils';
import { parse } from './preprocessor';
import * as path from 'path';
export = function(source: string, map) {
this.cacheable && this.cacheable();
const options: loaderUtils.OptionObject = loaderUtils.getOptions(this) || {};
const originalData = options.json || options;
const data = { ...originalData };
const verboseFlag = "ifdef-verbose";
const verbose = data[verboseFlag];
if(verbose !== undefined) {
delete data[verboseFlag];
}
let filePath: string | undefined = undefined;
if(verbose) {
filePath = path.relative(this.rootContext || '', this.resourcePath);
}
const tripleSlashFlag = "ifdef-triple-slash";
const tripleSlash = data[tripleSlashFlag];
if(tripleSlash !== undefined) {
delete data[tripleSlashFlag];
}
const fillWithBlanksFlag = "ifdef-fill-with-blanks";
const fillWithBlanks = data[fillWithBlanksFlag];
if(fillWithBlanks !== undefined) {
delete data[fillWithBlanksFlag];
}
const uncommentPrefixFlag = "ifdef-uncomment-prefix";
const uncommentPrefix = data[uncommentPrefixFlag];
if(uncommentPrefix !== undefined) {
delete data[uncommentPrefixFlag];
}
try {
source = parse(source, data, verbose, tripleSlash, filePath, fillWithBlanks, uncommentPrefix);
this.callback(null, source, map);
} catch(err) {
const errorMessage = `ifdef-loader error: ${err}`;
this.callback(new Error(errorMessage));
}
};