Skip to content

Commit 6f992b0

Browse files
committed
feat: prepare method "enableBabelTypeScriptPreset"
1 parent 8238c32 commit 6f992b0

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ class Encore {
980980

981981
/**
982982
* If enabled, a Preact preset will be applied to
983-
* the generated Webpack configuration.
983+
* the generated Webpack and Babel configuration.
984984
*
985985
* ```
986986
* Encore.enablePreactPreset()
@@ -1044,6 +1044,42 @@ class Encore {
10441044
return this;
10451045
}
10461046

1047+
1048+
/**
1049+
* If enabled, a TypeScript preset will be applied to
1050+
* the generated Webpack and Babel configuration.
1051+
*
1052+
* ```
1053+
* Encore.enableBabelTypeScriptPreset()
1054+
* ```
1055+
*
1056+
* This method let Babel handle your TypeScript code
1057+
* and can not be used with `Encore.enableTypeScriptLoader()`
1058+
* or `Encore.enableForkedTypeScriptTypesChecking()`.
1059+
*
1060+
* Since all types are removed by Babel,
1061+
* you must run `tsc --noEmit` yourself for types checking.
1062+
*
1063+
* The Babel TypeScript preset can be configured,
1064+
* see https://babeljs.io/docs/en/babel-preset-typescript#options
1065+
* for available options.
1066+
*
1067+
* For example:
1068+
* ```
1069+
* Encore.enableBabelTypeScriptPreset({
1070+
* isTSX: true
1071+
* })
1072+
* ```
1073+
*
1074+
* @param {object} options
1075+
* @returns {Encore}
1076+
*/
1077+
enableBabelTypeScriptPreset(options) {
1078+
webpackConfig.enableBabelTypeScriptPreset(options);
1079+
1080+
return this;
1081+
}
1082+
10471083
/**
10481084
* If enabled, the Vue.js loader is enabled.
10491085
*

lib/WebpackConfig.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class WebpackConfig {
109109
this.useEslintLoader = false;
110110
this.useTypeScriptLoader = false;
111111
this.useForkedTypeScriptTypeChecking = false;
112+
this.useBabelTypeScriptPreset = false;
112113
this.useWebpackNotifier = false;
113114
this.useHandlebarsLoader = false;
114115

@@ -647,6 +648,10 @@ class WebpackConfig {
647648
}
648649

649650
enableTypeScriptLoader(callback = () => {}) {
651+
if (this.useBabelTypeScriptPreset) {
652+
throw new Error('Encore.enableTypeScriptLoader() can not be called when Encore.enableBabelTypeScriptPreset() has been called.');
653+
}
654+
650655
this.useTypeScriptLoader = true;
651656

652657
if (typeof callback !== 'function') {
@@ -657,6 +662,9 @@ class WebpackConfig {
657662
}
658663

659664
enableForkedTypeScriptTypesChecking(forkedTypeScriptTypesCheckOptionsCallback = () => {}) {
665+
if (this.useBabelTypeScriptPreset) {
666+
throw new Error('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableForkedTypeScriptTypesChecking() has been called.');
667+
}
660668

661669
if (typeof forkedTypeScriptTypesCheckOptionsCallback !== 'function') {
662670
throw new Error('Argument 1 to enableForkedTypeScriptTypesChecking() must be a callback function.');
@@ -667,6 +675,18 @@ class WebpackConfig {
667675
forkedTypeScriptTypesCheckOptionsCallback;
668676
}
669677

678+
enableBabelTypeScriptPreset(options = {}) {
679+
if (this.useTypeScriptLoader) {
680+
throw new Error('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableTypeScriptLoader() has been called.');
681+
}
682+
683+
if (this.useForkedTypeScriptTypeChecking) {
684+
throw new Error('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableForkedTypeScriptTypesChecking() has been called.');
685+
}
686+
687+
this.useBabelTypeScriptPreset = true;
688+
}
689+
670690
enableVueLoader(vueLoaderOptionsCallback = () => {}, vueOptions = {}) {
671691
this.useVueLoader = true;
672692

test/WebpackConfig.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,15 @@ describe('WebpackConfig object', () => {
872872
config.enableTypeScriptLoader('FOO');
873873
}).to.throw('must be a callback function');
874874
});
875+
876+
it('TypeScript can not be compiled by ts-loader is Babel is already handling TypeScript', () => {
877+
const config = createConfig();
878+
config.enableBabelTypeScriptPreset();
879+
880+
expect(function() {
881+
config.enableTypeScriptLoader();
882+
}).to.throw('Encore.enableTypeScriptLoader() can not be called when Encore.enableBabelTypeScriptPreset() has been called.');
883+
});
875884
});
876885

877886
describe('enableForkedTypeScriptTypesChecking', () => {
@@ -891,8 +900,37 @@ describe('WebpackConfig object', () => {
891900
config.enableForkedTypeScriptTypesChecking('FOO');
892901
}).to.throw('must be a callback function');
893902
});
903+
904+
it('TypeScript can not be compiled by Babel if forked types checking is enabled', () => {
905+
const config = createConfig();
906+
config.enableForkedTypeScriptTypesChecking();
907+
908+
expect(function() {
909+
config.enableBabelTypeScriptPreset();
910+
}).to.throw('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableForkedTypeScriptTypesChecking() has been called.');
911+
});
894912
});
895913

914+
describe('enableBabelTypeScriptPreset', () => {
915+
it('TypeScript can not be compiled by Babel if ts-loader is already enabled', () => {
916+
const config = createConfig();
917+
config.enableTypeScriptLoader();
918+
919+
expect(function() {
920+
config.enableBabelTypeScriptPreset();
921+
}).to.throw('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableTypeScriptLoader() has been called.');
922+
});
923+
924+
it('TypeScript can not be compiled by Babel if ts-loader is already enabled', () => {
925+
const config = createConfig();
926+
config.enableForkedTypeScriptTypesChecking();
927+
928+
expect(function() {
929+
config.enableBabelTypeScriptPreset();
930+
}).to.throw('Encore.enableBabelTypeScriptPreset() can not be called when Encore.enableForkedTypeScriptTypesChecking() has been called.');
931+
});
932+
})
933+
896934
describe('enableVueLoader', () => {
897935
it('Call with no config', () => {
898936
const config = createConfig();

0 commit comments

Comments
 (0)