@@ -16,30 +16,53 @@ interface ModuleRule extends Object {
1616
1717type Preset = string | object ;
1818
19+ const replaceExt = ( path : string , ext : string ) : string =>
20+ path . substr ( 0 , path . lastIndexOf ( "." ) ) + `${ ext } '` ;
21+
1922function updateEntryExt ( self , newExt : string ) : void {
2023 const jsEntryOption = self . configuration . config . webpackOptions . entry ;
21- const jsExtension = new RegExp ( "\.js(?!.*\.js)" ) ;
2224 let tsEntryOption = { } ;
2325 if ( typeof jsEntryOption === "string" ) {
24- tsEntryOption = jsEntryOption . replace ( jsExtension , newExt ) ;
26+ tsEntryOption = replaceExt ( jsEntryOption , newExt ) ;
2527 } else if ( typeof jsEntryOption === "object" ) {
2628 Object . keys ( jsEntryOption ) . forEach ( ( entry : string ) : void => {
27- tsEntryOption [ entry ] = jsEntryOption [ entry ] . replace ( jsExtension , newExt ) ;
29+ tsEntryOption [ entry ] = replaceExt ( jsEntryOption [ entry ] , newExt ) ;
2830 } ) ;
2931 }
3032 self . configuration . config . webpackOptions . entry = tsEntryOption ;
3133}
3234
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+
3353/**
3454 *
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
3858 */
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+ ) ;
4063 return {
4164 test : "/\.js$/" ,
42- include : [ "path.resolve(__dirname, 'src')" ] ,
65+ include,
4366 loader : "'babel-loader'" ,
4467 options : {
4568 plugins : [ "'syntax-dynamic-import'" ] ,
@@ -55,16 +78,26 @@ export function getBabelLoader(): ModuleRule {
5578 } ;
5679}
5780
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+ ) ;
5991 return {
6092 test : "/\.tsx?$/" ,
6193 loader : "'ts-loader'" ,
62- include : [ "path.resolve(__dirname, 'src')" ] ,
94+ include,
6395 exclude : [ "/node_modules/" ] ,
6496 } ;
6597}
6698
6799export default function language ( self , langType : string ) : void {
100+ const entryFolders = getEntryFolders ( self ) ;
68101 switch ( langType ) {
69102 case LangType . ES6 :
70103 self . dependencies . push (
@@ -73,7 +106,7 @@ export default function language(self, langType: string): void {
73106 "@babel/preset-env" ,
74107 ) ;
75108 self . configuration . config . webpackOptions . module . rules . push (
76- getBabelLoader ( ) ,
109+ getBabelLoader ( entryFolders ) ,
77110 ) ;
78111 break ;
79112
@@ -83,7 +116,7 @@ export default function language(self, langType: string): void {
83116 "ts-loader" ,
84117 ) ;
85118 self . configuration . config . webpackOptions . module . rules . push (
86- getTypescriptLoader ( ) ,
119+ getTypescriptLoader ( entryFolders ) ,
87120 ) ;
88121 self . configuration . config . webpackOptions . resolve = {
89122 extensions : [ "'.tsx'" , "'.ts'" , "'.js'" ] ,
0 commit comments