1- module . exports = function ( api ) {
2- var validEnv = [ 'development' , 'test' , 'production' ]
3- var currentEnv = api . env ( )
4- var isDevelopmentEnv = api . env ( 'development' )
5- var isProductionEnv = api . env ( 'production' )
6- var isTestEnv = api . env ( 'test' )
7- const isHMR = process . env . WEBPACK_DEV_SERVER
1+ // The source code including full typescript support is available at:
2+ // https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/babel.config.js
3+
4+ module . exports = ( api ) => {
5+ const validEnv = [ 'development' , 'test' , 'production' ] ;
6+ const currentEnv = api . env ( ) ;
7+ // https://babeljs.io/docs/en/config-files#apienv
8+ // api.env is almost the NODE_ENV
9+ const isDevelopmentEnv = api . env ( 'development' ) ;
10+ const isProductionEnv = api . env ( 'production' ) ;
11+ const isTestEnv = api . env ( 'test' ) ;
812
913 if ( ! validEnv . includes ( currentEnv ) ) {
1014 throw new Error (
11- 'Please specify a valid `NODE_ENV` or ' +
15+ `${
16+ 'Please specify a valid `NODE_ENV` or ' +
1217 '`BABEL_ENV` environment variables. Valid values are "development", ' +
13- '"test", and "production". Instead, received: ' +
14- JSON . stringify ( currentEnv ) +
15- '.'
16- )
18+ '"test", and "production". Instead, received: '
19+ } ${ JSON . stringify ( currentEnv ) } .`,
20+ ) ;
1721 }
1822
1923 return {
@@ -22,30 +26,81 @@ module.exports = function(api) {
2226 '@babel/preset-env' ,
2327 {
2428 targets : {
25- node : 'current'
26- }
27- }
29+ node : 'current' ,
30+ } ,
31+ modules : 'commonjs' ,
32+ } ,
33+ '@babel/preset-react' ,
34+ ] ,
35+ ( isProductionEnv || isDevelopmentEnv ) && [
36+ '@babel/preset-env' ,
37+ {
38+ forceAllTransforms : true ,
39+ useBuiltIns : 'entry' ,
40+ corejs : 3 ,
41+ modules : false ,
42+ exclude : [ 'transform-typeof-symbol' ] ,
43+ } ,
44+ ] ,
45+ [
46+ '@babel/preset-react' ,
47+ {
48+ development : isDevelopmentEnv || isTestEnv ,
49+ useBuiltIns : true ,
50+ } ,
2851 ] ,
29- ( isProductionEnv || isDevelopmentEnv ) && '@babel/preset-env' ,
30- "@babel/preset-react" ,
52+ [ '@babel/preset-typescript' , { allExtensions : true , isTSX : true } ] ,
3153 ] . filter ( Boolean ) ,
3254 plugins : [
3355 'babel-plugin-macros' ,
34- isDevelopmentEnv && isHMR && 'react-refresh/babel' ,
56+ '@babel/plugin-syntax-dynamic-import' ,
57+ isTestEnv && 'babel-plugin-dynamic-import-node' ,
58+ '@babel/plugin-transform-destructuring' ,
3559 [
3660 '@babel/plugin-proposal-class-properties' ,
3761 {
38- loose : true
39- }
62+ loose : true ,
63+ } ,
64+ ] ,
65+ [
66+ '@babel/plugin-proposal-object-rest-spread' ,
67+ {
68+ useBuiltIns : true ,
69+ } ,
4070 ] ,
4171 [
4272 '@babel/plugin-transform-runtime' ,
4373 {
4474 helpers : false ,
4575 regenerator : true ,
46- corejs : false
47- }
48- ]
49- ] . filter ( Boolean )
50- }
51- }
76+ corejs : false ,
77+ } ,
78+ ] ,
79+ [
80+ '@babel/plugin-transform-regenerator' ,
81+ {
82+ async : false ,
83+ } ,
84+ ] ,
85+ [
86+ '@babel/plugin-proposal-private-property-in-object' ,
87+ {
88+ loose : true ,
89+ } ,
90+ ] ,
91+ [
92+ '@babel/plugin-proposal-private-methods' ,
93+ {
94+ loose : true ,
95+ } ,
96+ ] ,
97+ process . env . WEBPACK_SERVE && 'react-refresh/babel' ,
98+ isProductionEnv && [
99+ 'babel-plugin-transform-react-remove-prop-types' ,
100+ {
101+ removeImport : true ,
102+ } ,
103+ ] ,
104+ ] . filter ( Boolean ) ,
105+ } ;
106+ } ;
0 commit comments