Skip to content

Commit b43f5c9

Browse files
David Pazdavidmpaz
David Paz
authored andcommitted
Refactor to favor configure by callback
Remove options and add callback for configuraiton * Add new loader util for typescript. * Refactor public API signature.
1 parent 1352d1c commit b43f5c9

File tree

5 files changed

+67
-41
lines changed

5 files changed

+67
-41
lines changed

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,18 @@ module.exports = {
344344
/**
345345
* Call this if you plan on loading TypeScript files.
346346
*
347-
* Supported options:
347+
* Encore.configureTypeScript(function(tsConfig) {
348+
* // change the tsConfig
349+
* });
350+
*
351+
* Supported configuration options:
348352
* @see https://github.com/TypeStrong/ts-loader/blob/master/README.md#available-options
349353
*
350-
* @param {object} options
354+
* @param {function} callback
351355
* @return {exports}
352356
*/
353-
enableTypeScriptLoader(options = {}) {
354-
webpackConfig.enableTypeScriptLoader(options);
357+
configureTypeScript(callback) {
358+
webpackConfig.configureTypeScript(callback);
355359

356360
return this;
357361
},

lib/WebpackConfig.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,7 @@ class WebpackConfig {
5252
this.useReact = false;
5353
this.loaders = [];
5454
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() {};
6956
}
7057

7158
getContext() {
@@ -237,16 +224,14 @@ class WebpackConfig {
237224
this.useReact = true;
238225
}
239226

240-
enableTypeScriptLoader(options = {}) {
227+
configureTypeScript(callback) {
241228
this.useTypeScriptLoader = true;
242229

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.');
249232
}
233+
234+
this.tsConfigurationCallback = callback;
250235
}
251236

252237
cleanupOutputBeforeBuild() {

lib/config-generator.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const cssLoaderUtil = require('./loaders/css');
2626
const sassLoaderUtil = require('./loaders/sass');
2727
const lessLoaderUtil = require('./loaders/less');
2828
const babelLoaderUtil = require('./loaders/babel');
29+
const tsLoaderUtil = require('./loaders/typescript');
2930

3031
class ConfigGenerator {
3132
/**
@@ -160,24 +161,10 @@ class ConfigGenerator {
160161
}
161162

162163
if (this.webpackConfig.useTypeScriptLoader) {
163-
loaderFeatures.ensureLoaderPackagesExist('typescript');
164-
165164
this.webpackConfig.addLoader({
166165
test: /\.tsx?$/,
167166
exclude: /node_modules/,
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)
181168
});
182169
}
183170

lib/loader-features.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const loaderFeatures = {
3737
description: 'process React JS files'
3838
},
3939
typescript: {
40-
method: 'enableTypeScriptLoader()',
40+
method: 'configureTypeScript()',
4141
packages: ['typescript', 'ts-loader'],
4242
description: 'process TypeScript files'
4343
}

lib/loaders/typescript.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
};

0 commit comments

Comments
 (0)