/
language.ts
117 lines (107 loc) · 3.11 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Rule } from "../types";
export enum LangType {
ES6 = "ES6",
Typescript = "Typescript",
}
const replaceExt = (path: string, ext: string): string =>
path.substr(0, path.lastIndexOf(".")) + `${ext}'`;
function updateEntryExt(self, newExt: string): void {
const jsEntryOption = self.configuration.config.webpackOptions.entry;
let tsEntryOption = {};
if (typeof jsEntryOption === "string") {
tsEntryOption = replaceExt(jsEntryOption, newExt);
} else if (typeof jsEntryOption === "object") {
Object.keys(jsEntryOption).forEach((entry: string): void => {
tsEntryOption[entry] = replaceExt(jsEntryOption[entry], newExt);
});
}
self.configuration.config.webpackOptions.entry = tsEntryOption;
}
const getFolder = (path: string): string =>
path.replace("'./", "").split("/").slice(0, -1).join("/");
function getEntryFolders(self): string[] {
const entryOption = self.configuration.config.webpackOptions.entry;
let entryFolders = {};
if (typeof entryOption === "string") {
const folder = getFolder(entryOption);
if (folder.length > 0) entryFolders[folder] = true;
} else if (typeof entryOption === "object") {
Object.keys(entryOption).forEach((entry: string): void => {
const folder = getFolder(entryOption[entry]);
if (folder.length > 0) entryFolders[folder] = true;
});
}
return Object.keys(entryFolders);
}
/**
*
* Returns an module.rule object for the babel loader
* @param {string[]} includeFolders An array of folders to include
* @returns {Rule} A configuration containing the babel-loader with env preset
*/
export function getBabelLoader(includeFolders: string[]): Rule {
const include = includeFolders.map((folder: string): string =>
`path.resolve(__dirname, '${folder}')`
);
return {
test: "/\.(js|jsx)$/",
include,
loader: "'babel-loader'",
options: {
plugins: ["'syntax-dynamic-import'"],
presets: [
[
"'@babel/preset-env'",
{
"'modules'": false
}
]
]
},
};
}
/**
*
* Returns an module.rule object for the typescript loader
* @param {string[]} includeFolders An array of folders to include
* @returns {Rule} A configuration containing the ts-loader
*/
export function getTypescriptLoader(includeFolders: string[]): Rule {
const include = includeFolders.map((folder: string): string =>
`path.resolve(__dirname, '${folder}')`
);
return {
test: "/\.(ts|tsx)?$/",
loader: "'ts-loader'",
include,
exclude: ["/node_modules/"],
};
}
export default function language(self, langType: string): void {
const entryFolders = getEntryFolders(self);
switch (langType) {
case LangType.ES6:
self.dependencies.push(
"babel-loader",
"@babel/core",
"@babel/preset-env",
);
self.configuration.config.webpackOptions.module.rules.push(
getBabelLoader(entryFolders),
);
break;
case LangType.Typescript:
self.dependencies.push(
"typescript",
"ts-loader",
);
self.configuration.config.webpackOptions.module.rules.push(
getTypescriptLoader(entryFolders),
);
self.configuration.config.webpackOptions.resolve = {
extensions: [ "'.tsx'", "'.ts'", "'.js'" ],
};
updateEntryExt(self, ".ts");
break;
}
}