/
language.ts
98 lines (91 loc) · 2.31 KB
/
language.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
export enum LangType {
ES6 = "ES6",
Typescript = "Typescript",
}
interface ModuleRule extends Object {
include?: string[];
exclude?: string[];
loader: string;
options?: {
plugins: string[];
presets: Preset[][];
};
test: string;
}
type Preset = string | object;
/**
*
* Returns an module.rule object that has the babel loader if invoked
*
* @returns {Function} A callable function that adds the babel-loader with env preset
*/
export function getBabelLoader(): ModuleRule {
return {
// TODO migrate tslint
// tslint:disable: object-literal-sort-keys
test: "/\.js$/",
include: ["path.resolve(__dirname, 'src')"],
loader: "'babel-loader'",
options: {
plugins: ["'syntax-dynamic-import'"],
presets: [
[
"'@babel/preset-env'",
{
"'modules'": false
}
]
]
},
// tslint:enable: object-literal-sort-keys
};
}
export function getTypescriptLoader(): ModuleRule {
return {
// TODO migrate tslint
// tslint:disable: object-literal-sort-keys
test: "/\.tsx?$/",
loader: "'ts-loader'",
include: ["path.resolve(__dirname, 'src')"],
exclude: ["/node_modules/"],
// tslint:enable: object-literal-sort-keys
};
}
export default function language(self, langType) {
switch (langType) {
case LangType.ES6:
self.dependencies.push(
"babel-loader",
"@babel/core",
"@babel/preset-env",
);
self.configuration.config.webpackOptions.module.rules.push(
getBabelLoader(),
);
break;
case LangType.Typescript:
self.dependencies.push(
"typescript",
"ts-loader",
);
self.configuration.config.webpackOptions.module.rules.push(
getTypescriptLoader(),
);
self.configuration.config.webpackOptions.resolve = {
extensions: [ "'.tsx'", "'.ts'", "'.js'" ],
};
// Update the entry files extensions to .ts
const jsEntryOption = self.configuration.config.webpackOptions.entry;
const jsExtension = new RegExp("\.js(?!.*\.js)");
let tsEntryOption = {};
if (typeof jsEntryOption === "string") {
tsEntryOption = jsEntryOption.replace(jsExtension, ".ts");
} else if (typeof jsEntryOption === "object") {
Object.keys(jsEntryOption).forEach((entry) => {
tsEntryOption[entry] = jsEntryOption[entry].replace(jsExtension, ".ts");
});
}
self.configuration.config.webpackOptions.entry = tsEntryOption;
break;
}
}