@@ -4,9 +4,10 @@ export enum LangType {
4
4
}
5
5
6
6
interface ModuleRule extends Object {
7
- include : string [ ] ;
7
+ include ?: string [ ] ;
8
+ exclude ?: string [ ] ;
8
9
loader : string ;
9
- options : {
10
+ options ? : {
10
11
plugins : string [ ] ;
11
12
presets : Preset [ ] [ ] ;
12
13
} ;
@@ -21,8 +22,11 @@ type Preset = string | object;
21
22
*
22
23
* @returns {Function } A callable function that adds the babel-loader with env preset
23
24
*/
24
- export function getBabelPlugin ( ) : ModuleRule {
25
+ export function getBabelLoader ( ) : ModuleRule {
25
26
return {
27
+ // TODO migrate tslint
28
+ // tslint:disable: object-literal-sort-keys
29
+ test : "/\.js$/" ,
26
30
include : [ "path.resolve(__dirname, 'src')" ] ,
27
31
loader : "'babel-loader'" ,
28
32
options : {
@@ -36,23 +40,60 @@ export function getBabelPlugin(): ModuleRule {
36
40
]
37
41
]
38
42
} ,
39
- test : `${ new RegExp ( / \. j s $ / ) } `
43
+ // tslint:enable: object-literal-sort-keys
44
+ } ;
45
+ }
46
+
47
+ export function getTypescriptLoader ( ) : ModuleRule {
48
+ return {
49
+ // TODO migrate tslint
50
+ // tslint:disable: object-literal-sort-keys
51
+ test : "/\.tsx?$/" ,
52
+ loader : "'ts-loader'" ,
53
+ include : [ "path.resolve(__dirname, 'src')" ] ,
54
+ exclude : [ "/node_modules/" ] ,
55
+ // tslint:enable: object-literal-sort-keys
40
56
} ;
41
57
}
42
58
43
59
export default function language ( self , langType ) {
44
60
switch ( langType ) {
45
61
case LangType . ES6 :
46
- self . configuration . config . webpackOptions . module . rules . push (
47
- getBabelPlugin ( ) ,
48
- ) ;
49
62
self . dependencies . push (
50
63
"babel-loader" ,
51
64
"@babel/core" ,
52
65
"@babel/preset-env" ,
53
66
) ;
67
+ self . configuration . config . webpackOptions . module . rules . push (
68
+ getBabelLoader ( ) ,
69
+ ) ;
54
70
break ;
71
+
55
72
case LangType . Typescript :
73
+ self . dependencies . push (
74
+ "typescript" ,
75
+ "ts-loader" ,
76
+ ) ;
77
+ self . configuration . config . webpackOptions . module . rules . push (
78
+ getTypescriptLoader ( ) ,
79
+ ) ;
80
+ self . configuration . config . webpackOptions . resolve = {
81
+ extensions : [ "'.tsx'" , "'.ts'" , "'.js'" ] ,
82
+ } ;
83
+
84
+ // Update the entry files extensions to .ts
85
+ const jsEntryOption = self . configuration . config . webpackOptions . entry ;
86
+ const jsExtension = new RegExp ( "\.js(?!.*\.js)" ) ;
87
+ let tsEntryOption = { } ;
88
+ if ( typeof jsEntryOption === "string" ) {
89
+ tsEntryOption = jsEntryOption . replace ( jsExtension , ".ts" ) ;
90
+ } else if ( typeof jsEntryOption === "object" ) {
91
+ Object . keys ( jsEntryOption ) . map ( ( entry ) => {
92
+ tsEntryOption [ entry ] = jsEntryOption [ entry ] . replace ( jsExtension , ".ts" ) ;
93
+ } ) ;
94
+ }
95
+ self . configuration . config . webpackOptions . entry = tsEntryOption ;
96
+ self . log ( jsEntryOption . replace ( jsExtension , ".ts" ) , jsEntryOption ) ;
56
97
break ;
57
98
}
58
99
}
0 commit comments