Make babel-standalone an ESModule and enable flow (#9025)

* Make babel-standalone an ESModule and enable flow

* autogenerate plugin list

* Make config an array
This commit is contained in:
Daniel Tschinder 2019-03-06 14:30:43 -08:00 committed by GitHub
parent d8a5329834
commit fb81e8f8b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 507 additions and 210 deletions

View File

@ -11,6 +11,7 @@ SOURCES = packages codemods
build: clean clean-lib
./node_modules/.bin/gulp build
node ./packages/babel-standalone/scripts/generate.js
node ./packages/babel-types/scripts/generateTypeHelpers.js
# call build again as the generated files might need to be compiled again.
./node_modules/.bin/gulp build

View File

@ -0,0 +1,37 @@
// @flow
const pluginConfig = require("./pluginConfig.json");
const path = require("path");
const chalk = require("chalk");
const camelCase = require("lodash/camelCase");
const format = require("../../../scripts/utils/formatCode");
const writeFile = require("../../../scripts/utils/writeFileAndMkDir");
const outputFile = path.join(__dirname, "../src/generated/plugins.js");
console.log("Generating @babel/standalone files");
let imports = "";
let list = "";
let allList = "";
for (const plugin of pluginConfig) {
const camelPlugin = camelCase(plugin);
imports += `import ${camelPlugin} from "@babel/plugin-${plugin}";`;
list += `${camelPlugin},`;
allList += `"${plugin}": ${camelPlugin},`;
}
const fileContent = `// @flow
/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
${imports}
export {${list}};
export const all = {${allList}};
`;
writeFile(outputFile, format(fileContent, outputFile));
console.log(` ${chalk.green("✔")} Generated plugin list`);

View File

@ -0,0 +1,85 @@
[
"external-helpers",
"syntax-async-generators",
"syntax-class-properties",
"syntax-decorators",
"syntax-do-expressions",
"syntax-dynamic-import",
"syntax-export-default-from",
"syntax-export-namespace-from",
"syntax-flow",
"syntax-function-bind",
"syntax-function-sent",
"syntax-import-meta",
"syntax-jsx",
"syntax-object-rest-spread",
"syntax-optional-catch-binding",
"syntax-pipeline-operator",
"syntax-typescript",
"proposal-async-generator-functions",
"proposal-class-properties",
"proposal-decorators",
"proposal-do-expressions",
"proposal-export-default-from",
"proposal-export-namespace-from",
"proposal-function-bind",
"proposal-function-sent",
"proposal-json-strings",
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator",
"proposal-numeric-separator",
"proposal-object-rest-spread",
"proposal-optional-catch-binding",
"proposal-optional-chaining",
"proposal-pipeline-operator",
"proposal-private-methods",
"proposal-throw-expressions",
"proposal-unicode-property-regex",
"transform-async-to-generator",
"transform-arrow-functions",
"transform-block-scoped-functions",
"transform-block-scoping",
"transform-classes",
"transform-computed-properties",
"transform-destructuring",
"transform-dotall-regex",
"transform-duplicate-keys",
"transform-exponentiation-operator",
"transform-flow-comments",
"transform-flow-strip-types",
"transform-for-of",
"transform-function-name",
"transform-instanceof",
"transform-jscript",
"transform-literals",
"transform-member-expression-literals",
"transform-modules-amd",
"transform-modules-commonjs",
"transform-modules-systemjs",
"transform-modules-umd",
"transform-new-target",
"transform-object-assign",
"transform-object-super",
"transform-object-set-prototype-of-to-assign",
"transform-parameters",
"transform-property-literals",
"transform-property-mutators",
"transform-proto-to-assign",
"transform-react-constant-elements",
"transform-react-display-name",
"transform-react-inline-elements",
"transform-react-jsx",
"transform-react-jsx-compat",
"transform-react-jsx-self",
"transform-react-jsx-source",
"transform-regenerator",
"transform-runtime",
"transform-shorthand-properties",
"transform-spread",
"transform-sticky-regex",
"transform-strict-mode",
"transform-template-literals",
"transform-typeof-symbol",
"transform-typescript",
"transform-unicode-regex"
]

View File

@ -0,0 +1,256 @@
// @flow
import externalHelpers from "@babel/plugin-external-helpers";
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import syntaxClassProperties from "@babel/plugin-syntax-class-properties";
import syntaxDecorators from "@babel/plugin-syntax-decorators";
import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions";
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
import syntaxExportDefaultFrom from "@babel/plugin-syntax-export-default-from";
import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
import syntaxFlow from "@babel/plugin-syntax-flow";
import syntaxFunctionBind from "@babel/plugin-syntax-function-bind";
import syntaxFunctionSent from "@babel/plugin-syntax-function-sent";
import syntaxImportMeta from "@babel/plugin-syntax-import-meta";
import syntaxJsx from "@babel/plugin-syntax-jsx";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator";
import syntaxTypescript from "@babel/plugin-syntax-typescript";
import proposalAsyncGeneratorFunctions from "@babel/plugin-proposal-async-generator-functions";
import proposalClassProperties from "@babel/plugin-proposal-class-properties";
import proposalDecorators from "@babel/plugin-proposal-decorators";
import proposalDoExpressions from "@babel/plugin-proposal-do-expressions";
import proposalExportDefaultFrom from "@babel/plugin-proposal-export-default-from";
import proposalExportNamespaceFrom from "@babel/plugin-proposal-export-namespace-from";
import proposalFunctionBind from "@babel/plugin-proposal-function-bind";
import proposalFunctionSent from "@babel/plugin-proposal-function-sent";
import proposalJsonStrings from "@babel/plugin-proposal-json-strings";
import proposalLogicalAssignmentOperators from "@babel/plugin-proposal-logical-assignment-operators";
import proposalNullishCoalescingOperator from "@babel/plugin-proposal-nullish-coalescing-operator";
import proposalNumericSeparator from "@babel/plugin-proposal-numeric-separator";
import proposalObjectRestSpread from "@babel/plugin-proposal-object-rest-spread";
import proposalOptionalCatchBinding from "@babel/plugin-proposal-optional-catch-binding";
import proposalOptionalChaining from "@babel/plugin-proposal-optional-chaining";
import proposalPipelineOperator from "@babel/plugin-proposal-pipeline-operator";
import proposalPrivateMethods from "@babel/plugin-proposal-private-methods";
import proposalThrowExpressions from "@babel/plugin-proposal-throw-expressions";
import proposalUnicodePropertyRegex from "@babel/plugin-proposal-unicode-property-regex";
import transformAsyncToGenerator from "@babel/plugin-transform-async-to-generator";
import transformArrowFunctions from "@babel/plugin-transform-arrow-functions";
import transformBlockScopedFunctions from "@babel/plugin-transform-block-scoped-functions";
import transformBlockScoping from "@babel/plugin-transform-block-scoping";
import transformClasses from "@babel/plugin-transform-classes";
import transformComputedProperties from "@babel/plugin-transform-computed-properties";
import transformDestructuring from "@babel/plugin-transform-destructuring";
import transformDotallRegex from "@babel/plugin-transform-dotall-regex";
import transformDuplicateKeys from "@babel/plugin-transform-duplicate-keys";
import transformExponentiationOperator from "@babel/plugin-transform-exponentiation-operator";
import transformFlowComments from "@babel/plugin-transform-flow-comments";
import transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types";
import transformForOf from "@babel/plugin-transform-for-of";
import transformFunctionName from "@babel/plugin-transform-function-name";
import transformInstanceof from "@babel/plugin-transform-instanceof";
import transformJscript from "@babel/plugin-transform-jscript";
import transformLiterals from "@babel/plugin-transform-literals";
import transformMemberExpressionLiterals from "@babel/plugin-transform-member-expression-literals";
import transformModulesAmd from "@babel/plugin-transform-modules-amd";
import transformModulesCommonjs from "@babel/plugin-transform-modules-commonjs";
import transformModulesSystemjs from "@babel/plugin-transform-modules-systemjs";
import transformModulesUmd from "@babel/plugin-transform-modules-umd";
import transformNewTarget from "@babel/plugin-transform-new-target";
import transformObjectAssign from "@babel/plugin-transform-object-assign";
import transformObjectSuper from "@babel/plugin-transform-object-super";
import transformObjectSetPrototypeOfToAssign from "@babel/plugin-transform-object-set-prototype-of-to-assign";
import transformParameters from "@babel/plugin-transform-parameters";
import transformPropertyLiterals from "@babel/plugin-transform-property-literals";
import transformPropertyMutators from "@babel/plugin-transform-property-mutators";
import transformProtoToAssign from "@babel/plugin-transform-proto-to-assign";
import transformReactConstantElements from "@babel/plugin-transform-react-constant-elements";
import transformReactDisplayName from "@babel/plugin-transform-react-display-name";
import transformReactInlineElements from "@babel/plugin-transform-react-inline-elements";
import transformReactJsx from "@babel/plugin-transform-react-jsx";
import transformReactJsxCompat from "@babel/plugin-transform-react-jsx-compat";
import transformReactJsxSelf from "@babel/plugin-transform-react-jsx-self";
import transformReactJsxSource from "@babel/plugin-transform-react-jsx-source";
import transformRegenerator from "@babel/plugin-transform-regenerator";
import transformRuntime from "@babel/plugin-transform-runtime";
import transformShorthandProperties from "@babel/plugin-transform-shorthand-properties";
import transformSpread from "@babel/plugin-transform-spread";
import transformStickyRegex from "@babel/plugin-transform-sticky-regex";
import transformStrictMode from "@babel/plugin-transform-strict-mode";
import transformTemplateLiterals from "@babel/plugin-transform-template-literals";
import transformTypeofSymbol from "@babel/plugin-transform-typeof-symbol";
import transformTypescript from "@babel/plugin-transform-typescript";
import transformUnicodeRegex from "@babel/plugin-transform-unicode-regex";
export {
externalHelpers,
syntaxAsyncGenerators,
syntaxClassProperties,
syntaxDecorators,
syntaxDoExpressions,
syntaxDynamicImport,
syntaxExportDefaultFrom,
syntaxExportNamespaceFrom,
syntaxFlow,
syntaxFunctionBind,
syntaxFunctionSent,
syntaxImportMeta,
syntaxJsx,
syntaxObjectRestSpread,
syntaxOptionalCatchBinding,
syntaxPipelineOperator,
syntaxTypescript,
proposalAsyncGeneratorFunctions,
proposalClassProperties,
proposalDecorators,
proposalDoExpressions,
proposalExportDefaultFrom,
proposalExportNamespaceFrom,
proposalFunctionBind,
proposalFunctionSent,
proposalJsonStrings,
proposalLogicalAssignmentOperators,
proposalNullishCoalescingOperator,
proposalNumericSeparator,
proposalObjectRestSpread,
proposalOptionalCatchBinding,
proposalOptionalChaining,
proposalPipelineOperator,
proposalPrivateMethods,
proposalThrowExpressions,
proposalUnicodePropertyRegex,
transformAsyncToGenerator,
transformArrowFunctions,
transformBlockScopedFunctions,
transformBlockScoping,
transformClasses,
transformComputedProperties,
transformDestructuring,
transformDotallRegex,
transformDuplicateKeys,
transformExponentiationOperator,
transformFlowComments,
transformFlowStripTypes,
transformForOf,
transformFunctionName,
transformInstanceof,
transformJscript,
transformLiterals,
transformMemberExpressionLiterals,
transformModulesAmd,
transformModulesCommonjs,
transformModulesSystemjs,
transformModulesUmd,
transformNewTarget,
transformObjectAssign,
transformObjectSuper,
transformObjectSetPrototypeOfToAssign,
transformParameters,
transformPropertyLiterals,
transformPropertyMutators,
transformProtoToAssign,
transformReactConstantElements,
transformReactDisplayName,
transformReactInlineElements,
transformReactJsx,
transformReactJsxCompat,
transformReactJsxSelf,
transformReactJsxSource,
transformRegenerator,
transformRuntime,
transformShorthandProperties,
transformSpread,
transformStickyRegex,
transformStrictMode,
transformTemplateLiterals,
transformTypeofSymbol,
transformTypescript,
transformUnicodeRegex,
};
export const all = {
"external-helpers": externalHelpers,
"syntax-async-generators": syntaxAsyncGenerators,
"syntax-class-properties": syntaxClassProperties,
"syntax-decorators": syntaxDecorators,
"syntax-do-expressions": syntaxDoExpressions,
"syntax-dynamic-import": syntaxDynamicImport,
"syntax-export-default-from": syntaxExportDefaultFrom,
"syntax-export-namespace-from": syntaxExportNamespaceFrom,
"syntax-flow": syntaxFlow,
"syntax-function-bind": syntaxFunctionBind,
"syntax-function-sent": syntaxFunctionSent,
"syntax-import-meta": syntaxImportMeta,
"syntax-jsx": syntaxJsx,
"syntax-object-rest-spread": syntaxObjectRestSpread,
"syntax-optional-catch-binding": syntaxOptionalCatchBinding,
"syntax-pipeline-operator": syntaxPipelineOperator,
"syntax-typescript": syntaxTypescript,
"proposal-async-generator-functions": proposalAsyncGeneratorFunctions,
"proposal-class-properties": proposalClassProperties,
"proposal-decorators": proposalDecorators,
"proposal-do-expressions": proposalDoExpressions,
"proposal-export-default-from": proposalExportDefaultFrom,
"proposal-export-namespace-from": proposalExportNamespaceFrom,
"proposal-function-bind": proposalFunctionBind,
"proposal-function-sent": proposalFunctionSent,
"proposal-json-strings": proposalJsonStrings,
"proposal-logical-assignment-operators": proposalLogicalAssignmentOperators,
"proposal-nullish-coalescing-operator": proposalNullishCoalescingOperator,
"proposal-numeric-separator": proposalNumericSeparator,
"proposal-object-rest-spread": proposalObjectRestSpread,
"proposal-optional-catch-binding": proposalOptionalCatchBinding,
"proposal-optional-chaining": proposalOptionalChaining,
"proposal-pipeline-operator": proposalPipelineOperator,
"proposal-private-methods": proposalPrivateMethods,
"proposal-throw-expressions": proposalThrowExpressions,
"proposal-unicode-property-regex": proposalUnicodePropertyRegex,
"transform-async-to-generator": transformAsyncToGenerator,
"transform-arrow-functions": transformArrowFunctions,
"transform-block-scoped-functions": transformBlockScopedFunctions,
"transform-block-scoping": transformBlockScoping,
"transform-classes": transformClasses,
"transform-computed-properties": transformComputedProperties,
"transform-destructuring": transformDestructuring,
"transform-dotall-regex": transformDotallRegex,
"transform-duplicate-keys": transformDuplicateKeys,
"transform-exponentiation-operator": transformExponentiationOperator,
"transform-flow-comments": transformFlowComments,
"transform-flow-strip-types": transformFlowStripTypes,
"transform-for-of": transformForOf,
"transform-function-name": transformFunctionName,
"transform-instanceof": transformInstanceof,
"transform-jscript": transformJscript,
"transform-literals": transformLiterals,
"transform-member-expression-literals": transformMemberExpressionLiterals,
"transform-modules-amd": transformModulesAmd,
"transform-modules-commonjs": transformModulesCommonjs,
"transform-modules-systemjs": transformModulesSystemjs,
"transform-modules-umd": transformModulesUmd,
"transform-new-target": transformNewTarget,
"transform-object-assign": transformObjectAssign,
"transform-object-super": transformObjectSuper,
"transform-object-set-prototype-of-to-assign": transformObjectSetPrototypeOfToAssign,
"transform-parameters": transformParameters,
"transform-property-literals": transformPropertyLiterals,
"transform-property-mutators": transformPropertyMutators,
"transform-proto-to-assign": transformProtoToAssign,
"transform-react-constant-elements": transformReactConstantElements,
"transform-react-display-name": transformReactDisplayName,
"transform-react-inline-elements": transformReactInlineElements,
"transform-react-jsx": transformReactJsx,
"transform-react-jsx-compat": transformReactJsxCompat,
"transform-react-jsx-self": transformReactJsxSelf,
"transform-react-jsx-source": transformReactJsxSource,
"transform-regenerator": transformRegenerator,
"transform-runtime": transformRuntime,
"transform-shorthand-properties": transformShorthandProperties,
"transform-spread": transformSpread,
"transform-sticky-regex": transformStickyRegex,
"transform-strict-mode": transformStrictMode,
"transform-template-literals": transformTemplateLiterals,
"transform-typeof-symbol": transformTypeofSymbol,
"transform-typescript": transformTypescript,
"transform-unicode-regex": transformUnicodeRegex,
};

View File

@ -4,12 +4,26 @@
* plugins, instead explicitly registering all the available plugins and
* presets, and requiring custom ones to be registered through `registerPlugin`
* and `registerPreset` respectively.
* @flow
*/
/* global VERSION */
/* eslint-disable max-len */
import * as Babel from "@babel/core";
import {
transformFromAst as babelTransformFromAst,
transform as babelTransform,
buildExternalHelpers as babelBuildExternalHelpers,
} from "@babel/core";
import { all } from "./generated/plugins";
import preset2015 from "./preset-es2015";
import presetStage0 from "./preset-stage-0";
import presetStage1 from "./preset-stage-1";
import presetStage2 from "./preset-stage-2";
import presetStage3 from "./preset-stage-3";
import presetReact from "@babel/preset-react";
import presetFlow from "@babel/preset-flow";
import presetTypescript from "@babel/preset-typescript";
import { runScripts } from "./transformScriptTags";
@ -84,20 +98,20 @@ function processOptions(options) {
};
}
export function transform(code, options) {
return Babel.transform(code, processOptions(options));
export function transform(code: string, options: Object) {
return babelTransform(code, processOptions(options));
}
export function transformFromAst(ast, code, options) {
return Babel.transformFromAst(ast, code, processOptions(options));
export function transformFromAst(ast: Object, code: string, options: Object) {
return babelTransformFromAst(ast, code, processOptions(options));
}
export const availablePlugins = {};
export const availablePresets = {};
export const buildExternalHelpers = Babel.buildExternalHelpers;
export const buildExternalHelpers = babelBuildExternalHelpers;
/**
* Registers a named plugin for use with Babel.
*/
export function registerPlugin(name, plugin) {
export function registerPlugin(name: string, plugin: Object | Function): void {
if (availablePlugins.hasOwnProperty(name)) {
console.warn(
`A plugin named "${name}" is already registered, it will be overridden`,
@ -109,7 +123,9 @@ export function registerPlugin(name, plugin) {
* Registers multiple plugins for use with Babel. `newPlugins` should be an object where the key
* is the name of the plugin, and the value is the plugin itself.
*/
export function registerPlugins(newPlugins) {
export function registerPlugins(newPlugins: {
[string]: Object | Function,
}): void {
Object.keys(newPlugins).forEach(name =>
registerPlugin(name, newPlugins[name]),
);
@ -118,7 +134,7 @@ export function registerPlugins(newPlugins) {
/**
* Registers a named preset for use with Babel.
*/
export function registerPreset(name, preset) {
export function registerPreset(name: string, preset: Object | Function): void {
if (availablePresets.hasOwnProperty(name)) {
console.warn(
`A preset named "${name}" is already registered, it will be overridden`,
@ -130,7 +146,9 @@ export function registerPreset(name, preset) {
* Registers multiple presets for use with Babel. `newPresets` should be an object where the key
* is the name of the preset, and the value is the preset itself.
*/
export function registerPresets(newPresets) {
export function registerPresets(newPresets: {
[string]: Object | Function,
}): void {
Object.keys(newPresets).forEach(name =>
registerPreset(name, newPresets[name]),
);
@ -139,90 +157,13 @@ export function registerPresets(newPresets) {
// All the plugins we should bundle
// Want to get rid of this long whitelist of plugins?
// Wait! Please read https://github.com/babel/babel/pull/6177 first.
registerPlugins({
"external-helpers": require("@babel/plugin-external-helpers"),
"syntax-async-generators": require("@babel/plugin-syntax-async-generators"),
"syntax-class-properties": require("@babel/plugin-syntax-class-properties"),
"syntax-decorators": require("@babel/plugin-syntax-decorators"),
"syntax-do-expressions": require("@babel/plugin-syntax-do-expressions"),
"syntax-dynamic-import": require("@babel/plugin-syntax-dynamic-import"),
"syntax-export-default-from": require("@babel/plugin-syntax-export-default-from"),
"syntax-export-namespace-from": require("@babel/plugin-syntax-export-namespace-from"),
"syntax-flow": require("@babel/plugin-syntax-flow"),
"syntax-function-bind": require("@babel/plugin-syntax-function-bind"),
"syntax-function-sent": require("@babel/plugin-syntax-function-sent"),
"syntax-import-meta": require("@babel/plugin-syntax-import-meta"),
"syntax-jsx": require("@babel/plugin-syntax-jsx"),
"syntax-object-rest-spread": require("@babel/plugin-syntax-object-rest-spread"),
"syntax-optional-catch-binding": require("@babel/plugin-syntax-optional-catch-binding"),
"syntax-pipeline-operator": require("@babel/plugin-syntax-pipeline-operator"),
"syntax-typescript": require("@babel/plugin-syntax-typescript"),
"transform-async-to-generator": require("@babel/plugin-transform-async-to-generator"),
"proposal-async-generator-functions": require("@babel/plugin-proposal-async-generator-functions"),
"proposal-class-properties": require("@babel/plugin-proposal-class-properties"),
"proposal-decorators": require("@babel/plugin-proposal-decorators"),
"proposal-do-expressions": require("@babel/plugin-proposal-do-expressions"),
"proposal-export-default-from": require("@babel/plugin-proposal-export-default-from"),
"proposal-export-namespace-from": require("@babel/plugin-proposal-export-namespace-from"),
"proposal-pipeline-operator": require("@babel/plugin-proposal-pipeline-operator"),
"proposal-private-methods": require("@babel/plugin-proposal-private-methods"),
"transform-arrow-functions": require("@babel/plugin-transform-arrow-functions"),
"transform-block-scoped-functions": require("@babel/plugin-transform-block-scoped-functions"),
"transform-block-scoping": require("@babel/plugin-transform-block-scoping"),
"transform-classes": require("@babel/plugin-transform-classes"),
"transform-computed-properties": require("@babel/plugin-transform-computed-properties"),
"transform-destructuring": require("@babel/plugin-transform-destructuring"),
"transform-dotall-regex": require("@babel/plugin-transform-dotall-regex"),
"transform-duplicate-keys": require("@babel/plugin-transform-duplicate-keys"),
"transform-for-of": require("@babel/plugin-transform-for-of"),
"transform-function-name": require("@babel/plugin-transform-function-name"),
"transform-instanceof": require("@babel/plugin-transform-instanceof"),
"transform-literals": require("@babel/plugin-transform-literals"),
"transform-modules-amd": require("@babel/plugin-transform-modules-amd"),
"transform-modules-commonjs": require("@babel/plugin-transform-modules-commonjs"),
"transform-modules-systemjs": require("@babel/plugin-transform-modules-systemjs"),
"transform-modules-umd": require("@babel/plugin-transform-modules-umd"),
"transform-object-super": require("@babel/plugin-transform-object-super"),
"transform-parameters": require("@babel/plugin-transform-parameters"),
"transform-shorthand-properties": require("@babel/plugin-transform-shorthand-properties"),
"transform-spread": require("@babel/plugin-transform-spread"),
"transform-sticky-regex": require("@babel/plugin-transform-sticky-regex"),
"transform-template-literals": require("@babel/plugin-transform-template-literals"),
"transform-typeof-symbol": require("@babel/plugin-transform-typeof-symbol"),
"transform-typescript": require("@babel/plugin-transform-typescript"),
"transform-unicode-regex": require("@babel/plugin-transform-unicode-regex"),
"transform-member-expression-literals": require("@babel/plugin-transform-member-expression-literals"),
"transform-property-literals": require("@babel/plugin-transform-property-literals"),
"transform-property-mutators": require("@babel/plugin-transform-property-mutators"),
"transform-exponentiation-operator": require("@babel/plugin-transform-exponentiation-operator"),
"transform-flow-comments": require("@babel/plugin-transform-flow-comments"),
"transform-flow-strip-types": require("@babel/plugin-transform-flow-strip-types"),
"proposal-function-bind": require("@babel/plugin-proposal-function-bind"),
"transform-jscript": require("@babel/plugin-transform-jscript"),
"transform-new-target": require("@babel/plugin-transform-new-target"),
"transform-object-assign": require("@babel/plugin-transform-object-assign"),
"proposal-object-rest-spread": require("@babel/plugin-proposal-object-rest-spread"),
"transform-object-set-prototype-of-to-assign": require("@babel/plugin-transform-object-set-prototype-of-to-assign"),
"proposal-optional-catch-binding": require("@babel/plugin-proposal-optional-catch-binding"),
"transform-proto-to-assign": require("@babel/plugin-transform-proto-to-assign"),
"transform-react-constant-elements": require("@babel/plugin-transform-react-constant-elements"),
"transform-react-display-name": require("@babel/plugin-transform-react-display-name"),
"transform-react-inline-elements": require("@babel/plugin-transform-react-inline-elements"),
"transform-react-jsx": require("@babel/plugin-transform-react-jsx"),
"transform-react-jsx-compat": require("@babel/plugin-transform-react-jsx-compat"),
"transform-react-jsx-self": require("@babel/plugin-transform-react-jsx-self"),
"transform-react-jsx-source": require("@babel/plugin-transform-react-jsx-source"),
"transform-regenerator": require("@babel/plugin-transform-regenerator"),
"transform-runtime": require("@babel/plugin-transform-runtime"),
"transform-strict-mode": require("@babel/plugin-transform-strict-mode"),
"proposal-unicode-property-regex": require("@babel/plugin-proposal-unicode-property-regex"),
});
registerPlugins(all);
// All the presets we should bundle
// Want to get rid of this whitelist of presets?
// Wait! Please read https://github.com/babel/babel/pull/6177 first.
registerPresets({
es2015: require("./preset-es2015"),
es2015: preset2015,
es2016: () => {
return {
plugins: [availablePlugins["transform-exponentiation-operator"]],
@ -233,22 +174,23 @@ registerPresets({
plugins: [availablePlugins["transform-async-to-generator"]],
};
},
react: require("@babel/preset-react"),
"stage-0": require("./preset-stage-0"),
"stage-1": require("./preset-stage-1"),
"stage-2": require("./preset-stage-2"),
"stage-3": require("./preset-stage-3"),
react: presetReact,
"stage-0": presetStage0,
"stage-1": presetStage1,
"stage-2": presetStage2,
"stage-3": presetStage3,
"es2015-loose": {
presets: [[require("./preset-es2015"), { loose: true }]],
presets: [[preset2015, { loose: true }]],
},
// ES2015 preset with es2015-modules-commonjs removed
"es2015-no-commonjs": {
presets: [[require("./preset-es2015"), { modules: false }]],
presets: [[preset2015, { modules: false }]],
},
typescript: require("@babel/preset-typescript"),
flow: require("@babel/preset-flow"),
typescript: presetTypescript,
flow: presetFlow,
});
// $FlowIgnore
export const version = VERSION;
function onDOMContentLoaded() {
@ -265,7 +207,7 @@ if (typeof window !== "undefined" && window && window.addEventListener) {
* Transform <script> tags with "text/babel" type.
* @param {Array} scriptTags specify script tags to transform, transform all in the <head> if not given
*/
export function transformScriptTags(scriptTags) {
export function transformScriptTags(scriptTags?: Array<any>) {
runScripts(transform, scriptTags);
}

View File

@ -1,29 +1,7 @@
import transformES2015TemplateLiterals from "@babel/plugin-transform-template-literals";
import transformES2015Literals from "@babel/plugin-transform-literals";
import transformES2015FunctionName from "@babel/plugin-transform-function-name";
import transformES2015ArrowFunctions from "@babel/plugin-transform-arrow-functions";
import transformES2015BlockScopedFunctions from "@babel/plugin-transform-block-scoped-functions";
import transformES2015Classes from "@babel/plugin-transform-classes";
import transformES2015ObjectSuper from "@babel/plugin-transform-object-super";
import transformES2015ShorthandProperties from "@babel/plugin-transform-shorthand-properties";
import transformES2015DuplicateKeys from "@babel/plugin-transform-duplicate-keys";
import transformES2015ComputedProperties from "@babel/plugin-transform-computed-properties";
import transformES2015ForOf from "@babel/plugin-transform-for-of";
import transformES2015StickyRegex from "@babel/plugin-transform-sticky-regex";
import transformES2015UnicodeRegex from "@babel/plugin-transform-unicode-regex";
import transformES2015Spread from "@babel/plugin-transform-spread";
import transformES2015Parameters from "@babel/plugin-transform-parameters";
import transformES2015Destructuring from "@babel/plugin-transform-destructuring";
import transformES2015BlockScoping from "@babel/plugin-transform-block-scoping";
import transformES2015TypeofSymbol from "@babel/plugin-transform-typeof-symbol";
import transformES2015ModulesCommonJS from "@babel/plugin-transform-modules-commonjs";
import transformES2015ModulesSystemJS from "@babel/plugin-transform-modules-systemjs";
import transformES2015ModulesAMD from "@babel/plugin-transform-modules-amd";
import transformES2015ModulesUMD from "@babel/plugin-transform-modules-umd";
import transformES2015Instanceof from "@babel/plugin-transform-instanceof";
import transformRegenerator from "@babel/plugin-transform-regenerator";
// @flow
import * as babelPlugins from "./generated/plugins";
export default (_, opts) => {
export default (_: any, opts: Object): Object => {
let loose = false;
let modules = "commonjs";
let spec = false;
@ -39,33 +17,39 @@ export default (_, opts) => {
return {
plugins: [
[transformES2015TemplateLiterals, { loose, spec }],
transformES2015Literals,
transformES2015FunctionName,
[transformES2015ArrowFunctions, { spec }],
transformES2015BlockScopedFunctions,
[transformES2015Classes, optsLoose],
transformES2015ObjectSuper,
transformES2015ShorthandProperties,
transformES2015DuplicateKeys,
[transformES2015ComputedProperties, optsLoose],
[transformES2015ForOf, optsLoose],
transformES2015StickyRegex,
transformES2015UnicodeRegex,
[transformES2015Spread, optsLoose],
[transformES2015Parameters, optsLoose],
[transformES2015Destructuring, optsLoose],
transformES2015BlockScoping,
transformES2015TypeofSymbol,
transformES2015Instanceof,
[babelPlugins.transformTemplateLiterals, { loose, spec }],
babelPlugins.transformLiterals,
babelPlugins.transformFunctionName,
[babelPlugins.transformArrowFunctions, { spec }],
babelPlugins.transformBlockScopedFunctions,
[babelPlugins.transformClasses, optsLoose],
babelPlugins.transformObjectSuper,
babelPlugins.transformShorthandProperties,
babelPlugins.transformDuplicateKeys,
[babelPlugins.transformComputedProperties, optsLoose],
[babelPlugins.transformForOf, optsLoose],
babelPlugins.transformStickyRegex,
babelPlugins.transformUnicodeRegex,
[babelPlugins.transformSpread, optsLoose],
[babelPlugins.transformParameters, optsLoose],
[babelPlugins.transformDestructuring, optsLoose],
babelPlugins.transformBlockScoping,
babelPlugins.transformTypeofSymbol,
babelPlugins.transformInstanceof,
(modules === "commonjs" || modules === "cjs") && [
transformES2015ModulesCommonJS,
babelPlugins.transformModulesCommonjs,
optsLoose,
],
modules === "systemjs" && [transformES2015ModulesSystemJS, optsLoose],
modules === "amd" && [transformES2015ModulesAMD, optsLoose],
modules === "umd" && [transformES2015ModulesUMD, optsLoose],
[transformRegenerator, { async: false, asyncGenerators: false }],
modules === "systemjs" && [
babelPlugins.transformModulesSystemjs,
optsLoose,
],
modules === "amd" && [babelPlugins.transformModulesAmd, optsLoose],
modules === "umd" && [babelPlugins.transformModulesUmd, optsLoose],
[
babelPlugins.transformRegenerator,
{ async: false, asyncGenerators: false },
],
].filter(Boolean), // filter out falsy values
};
};

View File

@ -1,8 +1,8 @@
// @flow
import presetStage1 from "./preset-stage-1";
import { proposalFunctionBind } from "./generated/plugins";
import transformFunctionBind from "@babel/plugin-proposal-function-bind";
export default (_, opts = {}) => {
export default (_: any, opts: Object = {}) => {
const {
loose = false,
useBuiltIns = false,
@ -24,6 +24,6 @@ export default (_, opts = {}) => {
},
],
],
plugins: [transformFunctionBind],
plugins: [proposalFunctionBind],
};
};

View File

@ -1,13 +1,8 @@
// @flow
import presetStage2 from "./preset-stage-2";
import * as babelPlugins from "./generated/plugins";
import transformExportDefaultFrom from "@babel/plugin-proposal-export-default-from";
import transformLogicalAssignmentOperators from "@babel/plugin-proposal-logical-assignment-operators";
import transformOptionalChaining from "@babel/plugin-proposal-optional-chaining";
import transformPipelineOperator from "@babel/plugin-proposal-pipeline-operator";
import transformNullishCoalescingOperator from "@babel/plugin-proposal-nullish-coalescing-operator";
import transformDoExpressions from "@babel/plugin-proposal-do-expressions";
export default (_, opts = {}) => {
export default (_: any, opts: Object = {}) => {
const {
loose = false,
useBuiltIns = false,
@ -24,12 +19,12 @@ export default (_, opts = {}) => {
],
],
plugins: [
transformExportDefaultFrom,
transformLogicalAssignmentOperators,
[transformOptionalChaining, { loose }],
[transformPipelineOperator, { proposal: pipelineProposal }],
[transformNullishCoalescingOperator, { loose }],
transformDoExpressions,
babelPlugins.proposalExportDefaultFrom,
babelPlugins.proposalLogicalAssignmentOperators,
[babelPlugins.proposalOptionalChaining, { loose }],
[babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal }],
[babelPlugins.proposalNullishCoalescingOperator, { loose }],
babelPlugins.proposalDoExpressions,
],
};
};

View File

@ -1,12 +1,8 @@
// @flow
import presetStage3 from "./preset-stage-3";
import * as babelPlugins from "./generated/plugins";
import transformDecorators from "@babel/plugin-proposal-decorators";
import transformFunctionSent from "@babel/plugin-proposal-function-sent";
import transformExportNamespaceFrom from "@babel/plugin-proposal-export-namespace-from";
import transformNumericSeparator from "@babel/plugin-proposal-numeric-separator";
import transformThrowExpressions from "@babel/plugin-proposal-throw-expressions";
export default (_, opts = {}) => {
export default (_: any, opts: Object = {}) => {
const {
loose = false,
useBuiltIns = false,
@ -18,13 +14,13 @@ export default (_, opts = {}) => {
presets: [[presetStage3, { loose, useBuiltIns }]],
plugins: [
[
transformDecorators,
babelPlugins.proposalDecorators,
{ legacy: decoratorsLegacy, decoratorsBeforeExport },
],
transformFunctionSent,
transformExportNamespaceFrom,
transformNumericSeparator,
transformThrowExpressions,
babelPlugins.proposalFunctionSent,
babelPlugins.proposalExportNamespaceFrom,
babelPlugins.proposalNumericSeparator,
babelPlugins.proposalThrowExpressions,
],
};
};

View File

@ -1,10 +1,7 @@
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
import syntaxImportMeta from "@babel/plugin-syntax-import-meta";
import transformClassProperties from "@babel/plugin-proposal-class-properties";
import transformJsonStrings from "@babel/plugin-proposal-json-strings";
import transformPrivateMethods from "@babel/plugin-proposal-private-methods";
// @flow
import * as babelPlugins from "./generated/plugins";
export default (_, opts) => {
export default (_: any, opts: Object) => {
let loose = false;
if (opts !== undefined) {
@ -13,11 +10,11 @@ export default (_, opts) => {
return {
plugins: [
syntaxDynamicImport,
syntaxImportMeta,
[transformClassProperties, { loose }],
transformJsonStrings,
[transformPrivateMethods, { loose }],
babelPlugins.syntaxDynamicImport,
babelPlugins.syntaxImportMeta,
[babelPlugins.proposalClassProperties, { loose }],
babelPlugins.proposalJsonStrings,
[babelPlugins.proposalPrivateMethods, { loose }],
],
};
};

View File

@ -1,39 +1,29 @@
"use strict";
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const generateBuilders = require("./generators/generateBuilders");
const generateValidators = require("./generators/generateValidators");
const generateAsserts = require("./generators/generateAsserts");
const generateConstants = require("./generators/generateConstants");
const format = require("./utils/formatCode");
const format = require("../../../scripts/utils/formatCode");
const writeFile = require("../../../scripts/utils/writeFileAndMkDir");
const baseDir = path.join(__dirname, "../src");
function writeFile(content, location) {
const file = path.join(baseDir, location);
try {
fs.mkdirSync(path.dirname(file));
} catch (error) {
if (error.code !== "EEXIST") {
throw error;
}
}
fs.writeFileSync(file, format(content, file));
}
console.log("Generating @babel/types dynamic functions");
writeFile(generateBuilders(), "builders/generated/index.js");
const buildersFile = path.join(baseDir, "builders/generated/index.js");
writeFile(buildersFile, format(generateBuilders(), buildersFile));
console.log(` ${chalk.green("✔")} Generated builders`);
writeFile(generateValidators(), "validators/generated/index.js");
const validatorsFile = path.join(baseDir, "validators/generated/index.js");
writeFile(validatorsFile, format(generateValidators(), validatorsFile));
console.log(` ${chalk.green("✔")} Generated validators`);
writeFile(generateAsserts(), "asserts/generated/index.js");
const assertsFile = path.join(baseDir, "asserts/generated/index.js");
writeFile(assertsFile, format(generateAsserts(), assertsFile));
console.log(` ${chalk.green("✔")} Generated asserts`);
writeFile(generateConstants(), "constants/generated/index.js");
const constantsFile = path.join(baseDir, "constants/generated/index.js");
writeFile(constantsFile, format(generateConstants(), constantsFile));
console.log(` ${chalk.green("✔")} Generated constants`);

View File

@ -5,7 +5,7 @@ module.exports = function formatCode(code, filename) {
filename = filename || __filename;
const prettierConfig = prettier.resolveConfig.sync(filename);
prettierConfig.filepath = filename;
prettierConfig.parser = "babylon";
prettierConfig.parser = "babel";
return prettier.format(code, prettierConfig);
};

View File

@ -0,0 +1,14 @@
const fs = require("fs");
const path = require("path");
module.exports = function writeFileAndMkDir(file, content) {
try {
fs.mkdirSync(path.dirname(file));
} catch (error) {
if (error.code !== "EEXIST") {
throw error;
}
}
fs.writeFileSync(file, content);
};