File tree Expand file tree Collapse file tree 6 files changed +5210
-2
lines changed
Expand file tree Collapse file tree 6 files changed +5210
-2
lines changed Original file line number Diff line number Diff line change @@ -341,6 +341,21 @@ module.exports = {
341341 return this ;
342342 } ,
343343
344+ /**
345+ * Call this if you plan on loading TypeScript files.
346+ *
347+ * Supported options:
348+ * @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
349+ *
350+ * @param {object } options
351+ * @return {exports }
352+ */
353+ enableTypeScriptLoader ( options = { } ) {
354+ webpackConfig . enableTypeScriptLoader ( options ) ;
355+
356+ return this ;
357+ } ,
358+
344359 /**
345360 * If enabled, the output directory is emptied between
346361 * each build (to remove old files).
Original file line number Diff line number Diff line change @@ -51,6 +51,21 @@ class WebpackConfig {
5151 this . babelConfigurationCallback = function ( ) { } ;
5252 this . useReact = false ;
5353 this . loaders = [ ] ;
54+ this . useTypeScriptLoader = false ;
55+ this . typeScriptOptions = {
56+ transpileOnly : false ,
57+ happyPackMode : false ,
58+ logInfoToStdOut : false ,
59+ logLevel : 'info' ,
60+ silent : false ,
61+ ignoreDiagnostics : [ ] ,
62+ compiler : 'typescript' ,
63+ configFileName : 'tsconfig.json' ,
64+ visualStudioErrorFormat : false ,
65+ compilerOptions : { } ,
66+ entryFileIsJs : false ,
67+ appendTsSuffixTo : [ ]
68+ } ;
5469 }
5570
5671 getContext ( ) {
@@ -222,6 +237,18 @@ class WebpackConfig {
222237 this . useReact = true ;
223238 }
224239
240+ enableTypeScriptLoader ( options = { } ) {
241+ this . useTypeScriptLoader = true ;
242+
243+ for ( const optionKey of Object . keys ( options ) ) {
244+ if ( ! ( optionKey in this . sassOptions ) ) {
245+ throw new Error ( `Invalid option "${ optionKey } " passed to enableTypeScriptLoader(). Valid keys are ${ Object . keys ( this . typeScriptOptions ) . join ( ', ' ) } ` ) ;
246+ }
247+
248+ this . typeScriptOptions [ optionKey ] = options [ optionKey ] ;
249+ }
250+ }
251+
225252 cleanupOutputBeforeBuild ( ) {
226253 this . cleanupOutput = true ;
227254 }
Original file line number Diff line number Diff line change @@ -154,6 +154,28 @@ class ConfigGenerator {
154154 } ) ;
155155 }
156156
157+ if ( this . webpackConfig . useTypeScriptLoader ) {
158+ loaderFeatures . ensureLoaderPackagesExist ( 'typescript' ) ;
159+
160+ this . webpackConfig . addLoader ( {
161+ test : / \. t s x ? $ / ,
162+ exclude : / n o d e _ m o d u l e s / ,
163+ use : [
164+ {
165+ loader : 'babel-loader' ,
166+ // @see https://babeljs.io/docs/usage/api/#options
167+ // @see https://github.com/babel/babel-loader#options
168+ options : babelConfig
169+ } ,
170+ {
171+ loader : 'ts-loader' ,
172+ // @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
173+ options : this . webpackConfig . typeScriptOptions
174+ }
175+ ]
176+ } ) ;
177+ }
178+
157179 this . webpackConfig . loaders . forEach ( ( loader ) => {
158180 rules . push ( loader ) ;
159181 } ) ;
Original file line number Diff line number Diff line change @@ -35,6 +35,11 @@ const loaderFeatures = {
3535 method : 'enableReactPreset()' ,
3636 packages : [ 'babel-preset-react' ] ,
3737 description : 'process React JS files'
38+ } ,
39+ typescript : {
40+ method : 'enableTypeScriptLoader()' ,
41+ packages : [ 'typescript' , 'ts-loaders' ] ,
42+ description : 'process TypeScript files'
3843 }
3944} ;
4045
You can’t perform that action at this time.
0 commit comments