File tree 5 files changed +67
-41
lines changed
5 files changed +67
-41
lines changed Original file line number Diff line number Diff line change @@ -344,14 +344,18 @@ module.exports = {
344
344
/**
345
345
* Call this if you plan on loading TypeScript files.
346
346
*
347
- * Supported options:
347
+ * Encore.configureTypeScript(function(tsConfig) {
348
+ * // change the tsConfig
349
+ * });
350
+ *
351
+ * Supported configuration options:
348
352
* @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
349
353
*
350
- * @param {object } options
354
+ * @param {function } callback
351
355
* @return {exports }
352
356
*/
353
- enableTypeScriptLoader ( options = { } ) {
354
- webpackConfig . enableTypeScriptLoader ( options ) ;
357
+ configureTypeScript ( callback ) {
358
+ webpackConfig . configureTypeScript ( callback ) ;
355
359
356
360
return this ;
357
361
} ,
Original file line number Diff line number Diff line change @@ -52,20 +52,7 @@ class WebpackConfig {
52
52
this . useReact = false ;
53
53
this . loaders = [ ] ;
54
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
- } ;
55
+ this . tsConfigurationCallback = function ( ) { } ;
69
56
}
70
57
71
58
getContext ( ) {
@@ -237,16 +224,14 @@ class WebpackConfig {
237
224
this . useReact = true ;
238
225
}
239
226
240
- enableTypeScriptLoader ( options = { } ) {
227
+ configureTypeScript ( callback ) {
241
228
this . useTypeScriptLoader = true ;
242
229
243
- for ( const optionKey of Object . keys ( options ) ) {
244
- if ( ! ( optionKey in this . typeScriptOptions ) ) {
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 ] ;
230
+ if ( typeof callback !== 'function' ) {
231
+ throw new Error ( 'Argument 1 to configureTypeScript() must be a callback function.' ) ;
249
232
}
233
+
234
+ this . tsConfigurationCallback = callback ;
250
235
}
251
236
252
237
cleanupOutputBeforeBuild ( ) {
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ const cssLoaderUtil = require('./loaders/css');
26
26
const sassLoaderUtil = require ( './loaders/sass' ) ;
27
27
const lessLoaderUtil = require ( './loaders/less' ) ;
28
28
const babelLoaderUtil = require ( './loaders/babel' ) ;
29
+ const tsLoaderUtil = require ( './loaders/typescript' ) ;
29
30
30
31
class ConfigGenerator {
31
32
/**
@@ -160,24 +161,10 @@ class ConfigGenerator {
160
161
}
161
162
162
163
if ( this . webpackConfig . useTypeScriptLoader ) {
163
- loaderFeatures . ensureLoaderPackagesExist ( 'typescript' ) ;
164
-
165
164
this . webpackConfig . addLoader ( {
166
165
test : / \. t s x ? $ / ,
167
166
exclude : / n o d e _ m o d u l e s / ,
168
- use : [
169
- {
170
- loader : 'babel-loader' ,
171
- // @see https://babeljs.io/docs/usage/api/#options
172
- // @see https://github.com/babel/babel-loader#options
173
- options : babelConfig
174
- } ,
175
- {
176
- loader : 'ts-loader' ,
177
- // @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
178
- options : this . webpackConfig . typeScriptOptions
179
- }
180
- ]
167
+ use : tsLoaderUtil . getLoaders ( this . webpackConfig )
181
168
} ) ;
182
169
}
183
170
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ const loaderFeatures = {
37
37
description : 'process React JS files'
38
38
} ,
39
39
typescript : {
40
- method : 'enableTypeScriptLoader ()' ,
40
+ method : 'configureTypeScript ()' ,
41
41
packages : [ 'typescript' , 'ts-loader' ] ,
42
42
description : 'process TypeScript files'
43
43
}
Original file line number Diff line number Diff line change
1
+ /*
2
+ * This file is part of the Symfony package.
3
+ *
4
+ * (c) Fabien Potencier <[email protected] >
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+
10
+ 'use strict' ;
11
+
12
+ const loaderFeatures = require ( '../loader-features' ) ;
13
+ const babelLoader = require ( './babel' ) ;
14
+
15
+ /**
16
+ * @param {WebpackConfig } webpackConfig
17
+ * @return {Array } of loaders to use for TypeScript
18
+ */
19
+ module . exports = {
20
+ getLoaders ( webpackConfig ) {
21
+ loaderFeatures . ensureLoaderPackagesExist ( 'typescript' ) ;
22
+
23
+ // some defaults
24
+ let config = {
25
+ transpileOnly : false ,
26
+ happyPackMode : false ,
27
+ logInfoToStdOut : false ,
28
+ logLevel : 'info' ,
29
+ silent : true ,
30
+ } ;
31
+
32
+ // allow for ts-loader config to be controlled
33
+ webpackConfig . tsConfigurationCallback . apply (
34
+ // use config as the this variable
35
+ config ,
36
+ [ config ]
37
+ ) ;
38
+
39
+ // use ts alongside with babel
40
+ // @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#babel
41
+ let loaders = babelLoader . getLoaders ( webpackConfig ) ;
42
+ return loaders . concat ( [
43
+ {
44
+ loader : 'ts-loader' ,
45
+ // @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
46
+ options : config
47
+ }
48
+ ] ) ;
49
+ }
50
+ } ;
You can’t perform that action at this time.
0 commit comments