Skip to content

Commit fc26e1b

Browse files
David Pazdavidmpaz
David Paz
authored andcommitted
Add support to enable TypeScript loader
* Add public api for enable loader. * Add default options taken from ts-loader docs. * Add loader to generated configuration. * Update packages.json dependencies * Add loader to loader features to check later.
1 parent 5ca96e9 commit fc26e1b

File tree

6 files changed

+5210
-2
lines changed

6 files changed

+5210
-2
lines changed

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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).

lib/WebpackConfig.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

lib/config-generator.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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: /\.tsx?$/,
162+
exclude: /node_modules/,
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
});

lib/loader-features.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)