-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts: use default babel if none is found in project (#14168)
- Loading branch information
Showing
6 changed files
with
104 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,38 @@ | ||
/** | ||
* 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, presets } = babelDefaultConfig; | ||
|
||
const overrideOptions = ( target, targetName, options ) => { | ||
if ( get( target, [ 'file', 'request' ] ) === targetName ) { | ||
return [ targetName, Object.assign( | ||
{}, | ||
target.options, | ||
options | ||
) ]; | ||
module.exports = function( environment = '', file ) { | ||
/* | ||
* Specific options to be passed using the caller config option: | ||
* https://babeljs.io/docs/en/options#caller | ||
* | ||
* The caller options can only be 'boolean', 'string', or 'number' by design: | ||
* https://github.com/babel/babel/blob/bd0c62dc0c30cf16a4d4ef0ddf21d386f673815c/packages/babel-core/src/config/validation/option-assertions.js#L122 | ||
*/ | ||
const callerOpts = { caller: { | ||
name: `WP_BUILD_${ environment.toUpperCase() }`, | ||
} }; | ||
switch ( environment ) { | ||
case 'main': | ||
// to be merged as a presetEnv option | ||
callerOpts.caller.modules = 'commonjs'; | ||
break; | ||
case 'module': | ||
// to be merged as a presetEnv option | ||
callerOpts.caller.modules = false; | ||
// to be merged as a pluginTransformRuntime option | ||
callerOpts.caller.useESModules = true; | ||
break; | ||
default: | ||
// preventing measure, this shouldn't happen ever | ||
delete callerOpts.caller; | ||
} | ||
return target; | ||
}; | ||
|
||
const babelConfigs = { | ||
main: Object.assign( | ||
{}, | ||
babelDefaultConfig, | ||
{ | ||
plugins, | ||
presets: map( | ||
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( | ||
presets, | ||
( preset ) => overrideOptions( preset, '@babel/preset-env', { | ||
modules: false, | ||
} ) | ||
), | ||
} | ||
), | ||
}; | ||
|
||
function getBabelConfig( environment ) { | ||
return babelConfigs[ environment ]; | ||
} | ||
// Sourcemaps options | ||
const sourceMapsOpts = { | ||
sourceMaps: true, | ||
sourceFileName: file, | ||
}; | ||
|
||
module.exports = getBabelConfig; | ||
return { | ||
...callerOpts, | ||
...sourceMapsOpts, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,72 @@ | ||
module.exports = function( api ) { | ||
let wpBuildOpts = {}; | ||
const isWPBuild = ( name ) => [ 'WP_BUILD_MAIN', 'WP_BUILD_MODULE' ].some( | ||
( buildName ) => name === buildName | ||
); | ||
|
||
const isTestEnv = api.env() === 'test'; | ||
|
||
api.caller( ( caller ) => { | ||
if ( caller && isWPBuild( caller.name ) ) { | ||
wpBuildOpts = { ...caller }; | ||
return caller.name; | ||
} | ||
return undefined; | ||
} ); | ||
|
||
const getPresetEnv = () => { | ||
const opts = {}; | ||
|
||
if ( isTestEnv ) { | ||
opts.useBuiltIns = 'usage'; | ||
} else { | ||
opts.modules = false; | ||
opts.targets = { | ||
browsers: require( '@wordpress/browserslist-config' ), | ||
}; | ||
} | ||
|
||
if ( isWPBuild( wpBuildOpts.name ) ) { | ||
opts.modules = wpBuildOpts.modules; | ||
} | ||
|
||
return [ require.resolve( '@babel/preset-env' ), opts ]; | ||
}; | ||
|
||
const maybeGetPluginTransformRuntime = () => { | ||
if ( isTestEnv ) { | ||
return undefined; | ||
} | ||
|
||
const opts = { | ||
helpers: true, | ||
useESModules: false, | ||
}; | ||
|
||
if ( wpBuildOpts.name === 'WP_BUILD_MODULE' ) { | ||
opts.useESModules = wpBuildOpts.useESModules; | ||
} | ||
|
||
return [ require.resolve( '@babel/plugin-transform-runtime' ), opts ]; | ||
}; | ||
|
||
return { | ||
presets: [ | ||
! isTestEnv && [ '@babel/preset-env', { | ||
modules: false, | ||
targets: { | ||
browsers: [ 'extends @wordpress/browserslist-config' ], | ||
}, | ||
} ], | ||
isTestEnv && [ '@babel/preset-env', { | ||
useBuiltIns: 'usage', | ||
} ], | ||
].filter( Boolean ), | ||
presets: [ getPresetEnv() ], | ||
plugins: [ | ||
'@babel/plugin-proposal-object-rest-spread', | ||
require.resolve( '@babel/plugin-proposal-object-rest-spread' ), | ||
[ | ||
'@wordpress/babel-plugin-import-jsx-pragma', | ||
require.resolve( '@wordpress/babel-plugin-import-jsx-pragma' ), | ||
{ | ||
scopeVariable: 'createElement', | ||
source: '@wordpress/element', | ||
isDefault: false, | ||
}, | ||
], | ||
[ '@babel/plugin-transform-react-jsx', { | ||
[ require.resolve( '@babel/plugin-transform-react-jsx' ), { | ||
pragma: 'createElement', | ||
} ], | ||
'@babel/plugin-proposal-async-generator-functions', | ||
! isTestEnv && [ '@babel/plugin-transform-runtime', { | ||
helpers: true, | ||
useESModules: false, | ||
} ], | ||
require.resolve( '@babel/plugin-proposal-async-generator-functions' ), | ||
maybeGetPluginTransformRuntime(), | ||
].filter( Boolean ), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters