@@ -16,30 +16,53 @@ interface ModuleRule extends Object {
16
16
17
17
type Preset = string | object ;
18
18
19
+ const replaceExt = ( path : string , ext : string ) : string =>
20
+ path . substr ( 0 , path . lastIndexOf ( "." ) ) + `${ ext } '` ;
21
+
19
22
function updateEntryExt ( self , newExt : string ) : void {
20
23
const jsEntryOption = self . configuration . config . webpackOptions . entry ;
21
- const jsExtension = new RegExp ( "\.js(?!.*\.js)" ) ;
22
24
let tsEntryOption = { } ;
23
25
if ( typeof jsEntryOption === "string" ) {
24
- tsEntryOption = jsEntryOption . replace ( jsExtension , newExt ) ;
26
+ tsEntryOption = replaceExt ( jsEntryOption , newExt ) ;
25
27
} else if ( typeof jsEntryOption === "object" ) {
26
28
Object . keys ( jsEntryOption ) . forEach ( ( entry : string ) : void => {
27
- tsEntryOption [ entry ] = jsEntryOption [ entry ] . replace ( jsExtension , newExt ) ;
29
+ tsEntryOption [ entry ] = replaceExt ( jsEntryOption [ entry ] , newExt ) ;
28
30
} ) ;
29
31
}
30
32
self . configuration . config . webpackOptions . entry = tsEntryOption ;
31
33
}
32
34
35
+ const getFolder = ( path : string ) : string =>
36
+ path . replace ( "'./" , "" ) . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" ) ;
37
+
38
+ function getEntryFolders ( self ) : string [ ] {
39
+ const entryOption = self . configuration . config . webpackOptions . entry ;
40
+ let entryFolders = { } ;
41
+ if ( typeof entryOption === "string" ) {
42
+ const folder = getFolder ( entryOption ) ;
43
+ if ( folder . length > 0 ) entryFolders [ folder ] = true ;
44
+ } else if ( typeof entryOption === "object" ) {
45
+ Object . keys ( entryOption ) . forEach ( ( entry : string ) : void => {
46
+ const folder = getFolder ( entryOption [ entry ] ) ;
47
+ if ( folder . length > 0 ) entryFolders [ folder ] = true ;
48
+ } ) ;
49
+ }
50
+ return Object . keys ( entryFolders ) ;
51
+ }
52
+
33
53
/**
34
54
*
35
- * Returns an module.rule object that has the babel loader if invoked
36
- *
37
- * @returns {Function } A callable function that adds the babel-loader with env preset
55
+ * Returns an module.rule object for the babel loader
56
+ * @param { string[] } includeFolders An array of folders to include
57
+ * @returns {ModuleRule } A configuration containing the babel-loader with env preset
38
58
*/
39
- export function getBabelLoader ( ) : ModuleRule {
59
+ export function getBabelLoader ( includeFolders : string [ ] ) : ModuleRule {
60
+ const include = includeFolders . map ( ( folder : string ) =>
61
+ `path.resolve(__dirname, '${ folder } ')`
62
+ ) ;
40
63
return {
41
64
test : "/\.js$/" ,
42
- include : [ "path.resolve(__dirname, 'src')" ] ,
65
+ include,
43
66
loader : "'babel-loader'" ,
44
67
options : {
45
68
plugins : [ "'syntax-dynamic-import'" ] ,
@@ -55,16 +78,26 @@ export function getBabelLoader(): ModuleRule {
55
78
} ;
56
79
}
57
80
58
- export function getTypescriptLoader ( ) : ModuleRule {
81
+ /**
82
+ *
83
+ * Returns an module.rule object for the typescript loader
84
+ * @param {string[] } includeFolders An array of folders to include
85
+ * @returns {ModuleRule } A configuration containing the ts-loader
86
+ */
87
+ export function getTypescriptLoader ( includeFolders : string [ ] ) : ModuleRule {
88
+ const include = includeFolders . map ( ( folder : string ) =>
89
+ `path.resolve(__dirname, '${ folder } ')`
90
+ ) ;
59
91
return {
60
92
test : "/\.tsx?$/" ,
61
93
loader : "'ts-loader'" ,
62
- include : [ "path.resolve(__dirname, 'src')" ] ,
94
+ include,
63
95
exclude : [ "/node_modules/" ] ,
64
96
} ;
65
97
}
66
98
67
99
export default function language ( self , langType : string ) : void {
100
+ const entryFolders = getEntryFolders ( self ) ;
68
101
switch ( langType ) {
69
102
case LangType . ES6 :
70
103
self . dependencies . push (
@@ -73,7 +106,7 @@ export default function language(self, langType: string): void {
73
106
"@babel/preset-env" ,
74
107
) ;
75
108
self . configuration . config . webpackOptions . module . rules . push (
76
- getBabelLoader ( ) ,
109
+ getBabelLoader ( entryFolders ) ,
77
110
) ;
78
111
break ;
79
112
@@ -83,7 +116,7 @@ export default function language(self, langType: string): void {
83
116
"ts-loader" ,
84
117
) ;
85
118
self . configuration . config . webpackOptions . module . rules . push (
86
- getTypescriptLoader ( ) ,
119
+ getTypescriptLoader ( entryFolders ) ,
87
120
) ;
88
121
self . configuration . config . webpackOptions . resolve = {
89
122
extensions : [ "'.tsx'" , "'.ts'" , "'.js'" ] ,
0 commit comments