woocommerce/packages/js/e2e-builds/get-babel-config.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
const { get, map } = require( 'lodash' );
const babel = require( '@babel/core' );
/**
* WordPress dependencies
*/
const { options: babelDefaultConfig } = babel.loadPartialConfig( {
configFile: '@wordpress/babel-preset-default',
} );
const plugins = babelDefaultConfig.plugins;
if ( ! process.env.SKIP_JSX_PRAGMA_TRANSFORM ) {
2022-03-28 03:14:09 +00:00
plugins.push( [
'@wordpress/babel-plugin-import-jsx-pragma',
{
scopeVariable: 'createElement',
source: '@wordpress/element',
isDefault: false,
},
] );
}
const overrideOptions = ( target, targetName, options ) => {
if ( get( target, [ 'file', 'request' ] ) === targetName ) {
2022-03-28 03:14:09 +00:00
return [ targetName, Object.assign( {}, target.options, options ) ];
}
return target;
};
const babelConfigs = {
2022-03-28 03:14:09 +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,
} )
),
} ),
};
function getBabelConfig( environment ) {
return babelConfigs[ environment ];
}
module.exports = getBabelConfig;