Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const escapeRegExp = require('escape-string-regexp');
const path = require('path');
require('./node-polyfills');
var _only = [];
const PLUGINS = [
[require('@babel/plugin-transform-flow-strip-types').default],
[require('@babel/plugin-proposal-object-rest-spread').default],
[require('@babel/plugin-proposal-class-properties').default],
[require('@babel/plugin-transform-modules-commonjs').default],
[require('@babel/plugin-proposal-nullish-coalescing-operator').default],
[require('@babel/plugin-proposal-optional-catch-binding').default],
[require('@babel/plugin-proposal-optional-chaining').default],
];
if (/^v[0-7]\./.test(process.version)) {
PLUGINS.push([require('@babel/plugin-transform-async-to-generator').default]);
}
function registerOnly(onlyList) {
// This prevents `babel-register` from transforming the code of the
// plugins/presets that we are require-ing themselves before setting up the
// actual config.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const escapeRegExp = require('escape-string-regexp');
const path = require('path');
require('./node-polyfills');
var _only = [];
const PLUGINS = [
[require('@babel/plugin-transform-flow-strip-types').default],
[require('@babel/plugin-proposal-object-rest-spread').default],
[require('@babel/plugin-proposal-class-properties').default],
[require('@babel/plugin-transform-modules-commonjs').default],
[require('@babel/plugin-proposal-nullish-coalescing-operator').default],
[require('@babel/plugin-proposal-optional-catch-binding').default],
[require('@babel/plugin-proposal-optional-chaining').default],
];
if (/^v[0-7]\./.test(process.version)) {
PLUGINS.push([require('@babel/plugin-transform-async-to-generator').default]);
}
function registerOnly(onlyList) {
// This prevents `babel-register` from transforming the code of the
// plugins/presets that we are require-ing themselves before setting up the
// actual config.
'@babel/plugin-transform-runtime': {
useESModules: !buildEsModules,
},
});
preset.presets.push(
[require('@babel/preset-react').default, {
development: process.env.BABEL_ENV !== 'production',
useBuiltIns: true,
...opts['@babel/preset-react'],
}],
);
preset.plugins.push(
require('@babel/plugin-transform-flow-strip-types').default,
[require('@babel/plugin-proposal-decorators').default, {
// TODO: migrate to non-legacy decorators
legacy: true,
// decoratorsBeforeExport: true,
}],
[require('@babel/plugin-proposal-class-properties').default, {
loose: true,
}],
);
if (buildEsModules) {
preset.plugins.push(require('babel-plugin-dynamic-import-node'));
}
return preset;
};
const path = require('path')
const {transformFileSync} = require('@babel/core')
// const syntaxTypeScript = require('@babel/plugin-syntax-typescript').default
const syntaxFlow = require('@babel/plugin-syntax-flow').default
const stripType = require('@babel/plugin-transform-flow-strip-types').default
const WasCreated = Symbol('WasCreated')
const overloadPlugin = ({types: t}) => {
const getTypeAnnotation = nodePath => {
if (!t.isIdentifier(nodePath.node)) {
return null
}
const binding = nodePath.scope.bindings[nodePath.node.name]
if (!binding || !('typeAnnotation' in binding.identifier)) {
return null
}
const {identifier} = binding
// Will use the native built-in instead of trying to polyfill
// behavior for any plugins that require one.
useBuiltIns: true,
},
],
isTypeScriptEnabled && [require('@babel/preset-typescript').default],
].filter(Boolean),
plugins: [
// Strip flow types before any other transform, emulating the behavior
// order as-if the browser supported all of the succeeding features
// https://github.com/facebook/create-react-app/pull/5182
// We will conditionally enable this plugin below in overrides as it clashes with
// @babel/plugin-proposal-decorators when using TypeScript.
// https://github.com/facebook/create-react-app/issues/5741
isFlowEnabled && [
require('@babel/plugin-transform-flow-strip-types').default,
false,
],
// Experimental macros support. Will be documented after it's had some time
// in the wild.
require('babel-plugin-macros'),
// Necessary to include regardless of the environment because
// in practice some other transforms (such as object-rest-spread)
// don't work without it: https://github.com/babel/babel/issues/7215
[
require('@babel/plugin-transform-destructuring').default,
{
// Use loose mode for performance:
// https://github.com/facebook/create-react-app/issues/5602
loose: false,
selectiveLoose: [
'useState',
// Will use the native built-in instead of trying to polyfill
// behavior for any plugins that require one.
useBuiltIns: true,
},
],
isTypeScriptEnabled && [require('@babel/preset-typescript').default],
].filter(Boolean),
plugins: [
// Strip flow types before any other transform, emulating the behavior
// order as-if the browser supported all of the succeeding features
// https://github.com/facebook/create-react-app/pull/5182
// We will conditionally enable this plugin below in overrides as it clashes with
// @babel/plugin-proposal-decorators when using TypeScript.
// https://github.com/facebook/create-react-app/issues/5741
isFlowEnabled && [
require('@babel/plugin-transform-flow-strip-types').default,
false,
],
// Experimental macros support. Will be documented after it's had some time
// in the wild.
require('babel-plugin-macros'),
// Necessary to include regardless of the environment because
// in practice some other transforms (such as object-rest-spread)
// don't work without it: https://github.com/babel/babel/issues/7215
[
require('@babel/plugin-transform-destructuring').default,
{
// Use loose mode for performance:
// https://github.com/facebook/create-react-app/issues/5602
loose: false,
selectiveLoose: [
'useState',
module.exports = function babelPresetGitHub(api, { useBuiltIns = false, env = true, modules = false, targets = {} }) {
targets = Object.assign({}, { browsers: defaultBrowsers }, targets)
if (targets.browsers === 'mobile') targets.browsers = mobileBrowsers
if (targets.browsers === 'default') targets.browsers = defaultBrowsers
const presets = []
const plugins = [
// ES2019
// Stage 3 with good signals for Stage 4
// Chrome 64+, Firefox 62+, Safari TP42, Opera 51+
require('@babel/plugin-syntax-import-meta').default,
// Chrome 63+, Firefox (https://mzl.la/2LMSnOf), Safari TP25, Opera 50+
require('@babel/plugin-syntax-dynamic-import').default,
// Non-standard
require('@babel/plugin-transform-flow-strip-types').default,
// Custom
require('babel-plugin-transform-invariant-location'),
]
const fullBrowsers = browserslist(targets.browsers)
const needsRestSpread = browserslist(['Edge > 0', 'Safari < 11.1']).some(browser => fullBrowsers.includes(browser))
if (env) {
presets.push([require('@babel/preset-env').default, { modules, targets, useBuiltIns: useBuiltIns ? 'entry' : false }])
} else if (needsRestSpread) {
plugins.push([require('@babel/plugin-proposal-object-rest-spread'), { useBuiltIns }])
}
return { plugins, presets };
};
// Remove PropTypes from production build
require('babel-plugin-transform-react-remove-prop-types').default,
{
removeImport: true,
},
],
// Adds syntax support for import()
require('@babel/plugin-syntax-dynamic-import').default,
isEnvTest &&
// Transform dynamic import to require
require('babel-plugin-dynamic-import-node'),
].filter(Boolean),
overrides: [
isFlowEnabled && {
exclude: /\.tsx?$/,
plugins: [require('@babel/plugin-transform-flow-strip-types').default],
},
isTypeScriptEnabled && {
test: /\.tsx?$/,
plugins: [
[
require('@babel/plugin-proposal-decorators').default,
{ legacy: true },
],
],
},
].filter(Boolean),
};
};
// Will use the native built-in instead of trying to polyfill
// behavior for any plugins that require one.
useBuiltIns: true,
},
],
isTypeScriptEnabled && [require('@babel/preset-typescript').default],
].filter(Boolean),
plugins: [
// Strip flow types before any other transform, emulating the behavior
// order as-if the browser supported all of the succeeding features
// https://github.com/facebook/create-react-app/pull/5182
// We will conditionally enable this plugin below in overrides as it clashes with
// @babel/plugin-proposal-decorators when using TypeScript.
// https://github.com/facebook/create-react-app/issues/5741
isFlowEnabled && [
require('@babel/plugin-transform-flow-strip-types').default,
false,
],
// Experimental macros support. Will be documented after it's had some time
// in the wild.
require('babel-plugin-macros'),
// Necessary to include regardless of the environment because
// in practice some other transforms (such as object-rest-spread)
// don't work without it: https://github.com/babel/babel/issues/7215
require('@babel/plugin-transform-destructuring').default,
// Turn on legacy decorators for TypeScript files
isTypeScriptEnabled && [
require('@babel/plugin-proposal-decorators').default,
false,
],
// class { handleClick = () => { } }
// Enable loose mode to use assignment instead of defineProperty
require("./ts-no-consts"),
[require("@babel/plugin-transform-typescript").default, tsOpts]
]
});
*/
result.presets.unshift(
{
plugins: [require("./ts-no-consts")]
},
[require("@babel/preset-typescript").default, tsOpts]
);
}
result.overrides = [
opts.flow !== false && {
exclude: /\.tsx?$/,
plugins: [require("@babel/plugin-transform-flow-strip-types").default]
},
opts.typescript !== false && {
test: /\.tsx?$/,
plugins: [
[require("@babel/plugin-proposal-decorators").default, { legacy: true }]
]
}
].filter(Boolean);
return result;
};