import * as path from "path" ; import { WebpackConfigurator } from "../main" ; import { getFirstExistingFile } from "../util" ; export async function configureTypescript ( configurator ) { const hasTsChecker = configurator . hasDevDependency ( "fork-ts-checker-webpack-plugin" ) || configurator . hasDevDependency ( "electron-webpack-ts" ) ; if ( ! ( hasTsChecker || configurator . hasDevDependency ( "ts-loader" ) ) ) { return ; } configurator . extensions . splice ( 1 , 0 , ".ts" , ".tsx" ) ; const isTranspileOnly = configurator . isTest || ( hasTsChecker && ! configurator . isProduction ) ; const tsConfigFile = await getFirstExistingFile ( [ path . join ( configurator . sourceDir , "tsconfig.json" ) , path . join ( configurator . projectDir , "tsconfig.json" ) ] , null ) ; if ( tsConfigFile == null ) { throw new Error ( `Please create tsconfig.json in the "${ configurator . projectDir }":\n\n{\n  "extends": "./node_modules/electron-webpack/tsconfig-base.json"\n}\n\n` ) ; } if ( configurator . debug . enabled ) { configurator . debug ( `Using ${ tsConfigFile }` ) ; } if ( isTranspileOnly && ! configurator . isTest ) { const hasVue = configurator . hasDependency ( "vue" ) ; const ForkTsCheckerWebpackPlugin = require ( "fork-ts-checker-webpack-plugin" ) ; configurator . plugins . push ( new ForkTsCheckerWebpackPlugin ( { tsconfig : tsConfigFile , logger : configurator . env . forkTsCheckerLogger || { info : ( ) => { } , warn : console . warn . bind ( console ) , error : console . error . bind ( console ) , } , vue : hasVue } ) ) ; } const tsLoaderOptions = { transpileOnly : isTranspileOnly , appendTsSuffixTo : [ /\.vue$/ ] , configFile : tsConfigFile , } ; if ( configurator . debug . enabled ) { configurator . debug ( `ts-loader options: ${ JSON . stringify ( tsLoaderOptions , null , 2 ) }` ) ; } configurator . rules . push ( { test : /\.tsx?$/ , exclude : /node_modules/ , use : [ { loader : "ts-loader" , options : tsLoaderOptions } , ] , } ) ; }