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,
|
|
|
|
presets: map( babelDefaultConfig.presets, ( preset ) =>
|
|
|
|
overrideOptions( preset, '@babel/preset-env', {
|
|
|
|
modules: 'commonjs',
|
|
|
|
} )
|
|
|
|
),
|
|
|
|
} ),
|
|
|
|
module: Object.assign( {}, babelDefaultConfig, {
|
|
|
|
plugins: map( plugins, ( plugin ) =>
|
|
|
|
overrideOptions( plugin, '@babel/plugin-transform-runtime', {
|
|
|
|
useESModules: true,
|
|
|
|
} )
|
|
|
|
),
|
|
|
|
presets: map( babelDefaultConfig.presets, ( preset ) =>
|
|
|
|
overrideOptions( preset, '@babel/preset-env', {
|
|
|
|
modules: false,
|
|
|
|
} )
|
|
|
|
),
|
|
|
|
} ),
|
2018-10-30 18:57:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function getBabelConfig( environment ) {
|
|
|
|
return babelConfigs[ environment ];
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getBabelConfig;
|