@@ -4,9 +4,10 @@ export enum LangType {
44}
55
66interface ModuleRule extends Object {
7- include : string [ ] ;
7+ include ?: string [ ] ;
8+ exclude ?: string [ ] ;
89 loader : string ;
9- options : {
10+ options ? : {
1011 plugins : string [ ] ;
1112 presets : Preset [ ] [ ] ;
1213 } ;
@@ -21,8 +22,11 @@ type Preset = string | object;
2122 *
2223 * @returns {Function } A callable function that adds the babel-loader with env preset
2324 */
24- export function getBabelPlugin ( ) : ModuleRule {
25+ export function getBabelLoader ( ) : ModuleRule {
2526 return {
27+ // TODO migrate tslint
28+ // tslint:disable: object-literal-sort-keys
29+ test : "/\.js$/" ,
2630 include : [ "path.resolve(__dirname, 'src')" ] ,
2731 loader : "'babel-loader'" ,
2832 options : {
@@ -36,23 +40,60 @@ export function getBabelPlugin(): ModuleRule {
3640 ]
3741 ]
3842 } ,
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
4056 } ;
4157}
4258
4359export default function language ( self , langType ) {
4460 switch ( langType ) {
4561 case LangType . ES6 :
46- self . configuration . config . webpackOptions . module . rules . push (
47- getBabelPlugin ( ) ,
48- ) ;
4962 self . dependencies . push (
5063 "babel-loader" ,
5164 "@babel/core" ,
5265 "@babel/preset-env" ,
5366 ) ;
67+ self . configuration . config . webpackOptions . module . rules . push (
68+ getBabelLoader ( ) ,
69+ ) ;
5470 break ;
71+
5572 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 ) ;
5697 break ;
5798 }
5899}
0 commit comments