2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
const { get, map } = require( 'lodash' );
|
|
|
|
const babel = require( '@babel/core' );
|
|
|
|
|
|
|
|
/**
|
2020-08-13 02:05:22 +00:00
|
|
|
* External dependencies
|
2018-10-30 18:57:48 +00:00
|
|
|
*/
|
|
|
|
const { options: babelDefaultConfig } = babel.loadPartialConfig( {
|
|
|
|
configFile: '@wordpress/babel-preset-default',
|
|
|
|
} );
|
|
|
|
const plugins = babelDefaultConfig.plugins;
|
|
|
|
|
|
|
|
const overrideOptions = ( target, targetName, options ) => {
|
|
|
|
if ( get( target, [ 'file', 'request' ] ) === targetName ) {
|
2020-08-13 02:05:22 +00:00
|
|
|
return [ targetName, Object.assign( {}, target.options, options ) ];
|
2018-10-30 18:57:48 +00:00
|
|
|
}
|
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
|
|
|
const babelConfigs = {
|
2020-08-13 02:05:22 +00:00
|
|
|
main: Object.assign( {}, babelDefaultConfig, {
|
|
|
|
plugins,
|
2021-03-24 21:29:37 +00:00
|
|
|
presets: [
|
|
|
|
[ '@babel/preset-typescript' ],
|
|
|
|
...map( babelDefaultConfig.presets, ( preset ) =>
|
|
|
|
overrideOptions( preset, '@babel/preset-env', {
|
|
|
|
modules: 'commonjs',
|
|
|
|
corejs: '3',
|
|
|
|
useBuiltIns: 'usage',
|
|
|
|
} )
|
|
|
|
),
|
|
|
|
],
|
2020-08-13 02:05:22 +00:00
|
|
|
} ),
|
|
|
|
module: Object.assign( {}, babelDefaultConfig, {
|
|
|
|
plugins: map( plugins, ( plugin ) =>
|
|
|
|
overrideOptions( plugin, '@babel/plugin-transform-runtime', {
|
|
|
|
useESModules: true,
|
|
|
|
} )
|
|
|
|
),
|
2021-03-24 21:29:37 +00:00
|
|
|
presets: [
|
|
|
|
[ '@babel/preset-typescript' ],
|
|
|
|
...map( babelDefaultConfig.presets, ( preset ) =>
|
|
|
|
overrideOptions( preset, '@babel/preset-env', {
|
|
|
|
modules: false,
|
|
|
|
corejs: '3',
|
|
|
|
useBuiltIns: 'usage',
|
|
|
|
} )
|
|
|
|
),
|
|
|
|
],
|
2020-08-13 02:05:22 +00:00
|
|
|
} ),
|
2018-10-30 18:57:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function getBabelConfig( environment ) {
|
|
|
|
return babelConfigs[ environment ];
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getBabelConfig;
|