convert @babel/standalone to typescript (#13212)

* babel-standalone flowts rename

* babel-standalone flowts convert

* babel-standalone - update rollup plugin to support ts files

* babel-standalone code generation update

* babel-standalone

* babel-standalone eslint fixes

* babel-standalone tsconfig.json

* babel-standalone

* babel-standalone tsconfig related fix

* make generate-tsconfig

* yarn install

* babel-standalone use fs.existsSync

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Bogdan Savluk 2021-05-14 15:54:09 +02:00 committed by GitHub
parent bb70ea47f5
commit b670c11070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 36 additions and 31 deletions

View File

@ -189,8 +189,8 @@ function generateStandalone() {
*/ */
${imports} ${imports}
export {${list}}; export {${list}};
export const all = {${allList}};`; export const all: { [k: string]: any } = {${allList}};`;
file.path = "plugins.js"; file.path = "plugins.ts";
file.contents = Buffer.from(formatCode(fileContents, dest)); file.contents = Buffer.from(formatCode(fileContents, dest));
callback(null, file); callback(null, file);
}) })

View File

@ -3,6 +3,5 @@
* Babel requires the entire package.json file just to get the version number. * Babel requires the entire package.json file just to get the version number.
*/ */
/* global BABEL_VERSION */ declare const BABEL_VERSION: string;
export const version = BABEL_VERSION; export const version = BABEL_VERSION;

View File

@ -192,7 +192,7 @@ export {
transformUnicodeEscapes, transformUnicodeEscapes,
transformUnicodeRegex, transformUnicodeRegex,
}; };
export const all = { export const all: { [k: string]: any } = {
"external-helpers": externalHelpers, "external-helpers": externalHelpers,
"syntax-async-generators": syntaxAsyncGenerators, "syntax-async-generators": syntaxAsyncGenerators,
"syntax-class-properties": syntaxClassProperties, "syntax-class-properties": syntaxClassProperties,

View File

@ -9,6 +9,7 @@
/* global VERSION */ /* global VERSION */
/* eslint-disable max-len */ /* eslint-disable max-len */
/// <reference lib="dom" />
// $FlowIgnore // $FlowIgnore
import "./dynamic-require-entrypoint.cjs"; import "./dynamic-require-entrypoint.cjs";
@ -102,11 +103,11 @@ function processOptions(options) {
}; };
} }
export function transform(code: string, options: Object) { export function transform(code: string, options: any) {
return babelTransform(code, processOptions(options)); return babelTransform(code, processOptions(options));
} }
export function transformFromAst(ast: Object, code: string, options: Object) { export function transformFromAst(ast: any, code: string, options: any) {
return babelTransformFromAst(ast, code, processOptions(options)); return babelTransformFromAst(ast, code, processOptions(options));
} }
export const availablePlugins = {}; export const availablePlugins = {};
@ -115,7 +116,7 @@ export const buildExternalHelpers = babelBuildExternalHelpers;
/** /**
* Registers a named plugin for use with Babel. * Registers a named plugin for use with Babel.
*/ */
export function registerPlugin(name: string, plugin: Object | Function): void { export function registerPlugin(name: string, plugin: any | Function): void {
if (Object.prototype.hasOwnProperty.call(availablePlugins, name)) { if (Object.prototype.hasOwnProperty.call(availablePlugins, name)) {
console.warn( console.warn(
`A plugin named "${name}" is already registered, it will be overridden`, `A plugin named "${name}" is already registered, it will be overridden`,
@ -128,7 +129,7 @@ export function registerPlugin(name: string, plugin: Object | Function): void {
* is the name of the plugin, and the value is the plugin itself. * is the name of the plugin, and the value is the plugin itself.
*/ */
export function registerPlugins(newPlugins: { export function registerPlugins(newPlugins: {
[string]: Object | Function, [x: string]: any | Function;
}): void { }): void {
Object.keys(newPlugins).forEach(name => Object.keys(newPlugins).forEach(name =>
registerPlugin(name, newPlugins[name]), registerPlugin(name, newPlugins[name]),
@ -138,7 +139,7 @@ export function registerPlugins(newPlugins: {
/** /**
* Registers a named preset for use with Babel. * Registers a named preset for use with Babel.
*/ */
export function registerPreset(name: string, preset: Object | Function): void { export function registerPreset(name: string, preset: any | Function): void {
if (Object.prototype.hasOwnProperty.call(availablePresets, name)) { if (Object.prototype.hasOwnProperty.call(availablePresets, name)) {
if (name === "env") { if (name === "env") {
console.warn( console.warn(
@ -157,7 +158,7 @@ export function registerPreset(name: string, preset: Object | Function): void {
* is the name of the preset, and the value is the preset itself. * is the name of the preset, and the value is the preset itself.
*/ */
export function registerPresets(newPresets: { export function registerPresets(newPresets: {
[string]: Object | Function, [x: string]: any | Function;
}): void { }): void {
Object.keys(newPresets).forEach(name => Object.keys(newPresets).forEach(name =>
registerPreset(name, newPresets[name]), registerPreset(name, newPresets[name]),
@ -201,8 +202,8 @@ registerPresets({
flow: presetFlow, flow: presetFlow,
}); });
// $FlowIgnore // @ts-ignore VERSION is to be replaced by rollup
export const version = VERSION; export const version: string = VERSION;
function onDOMContentLoaded() { function onDOMContentLoaded() {
transformScriptTags(); transformScriptTags();

View File

@ -1,7 +1,6 @@
// @flow
import * as babelPlugins from "./generated/plugins"; import * as babelPlugins from "./generated/plugins";
export default (_: any, opts: Object): Object => { export default (_: any, opts: any): any => {
let loose = false; let loose = false;
let modules = "commonjs"; let modules = "commonjs";
let spec = false; let spec = false;

View File

@ -1,8 +1,7 @@
// @flow
import presetStage1 from "./preset-stage-1"; import presetStage1 from "./preset-stage-1";
import { proposalFunctionBind } from "./generated/plugins"; import { proposalFunctionBind } from "./generated/plugins";
export default (_: any, opts: Object = {}) => { export default (_: any, opts: any = {}) => {
const { const {
loose = false, loose = false,
useBuiltIns = false, useBuiltIns = false,

View File

@ -1,8 +1,7 @@
// @flow
import presetStage2 from "./preset-stage-2"; import presetStage2 from "./preset-stage-2";
import * as babelPlugins from "./generated/plugins"; import * as babelPlugins from "./generated/plugins";
export default (_: any, opts: Object = {}) => { export default (_: any, opts: any = {}) => {
const { const {
loose = false, loose = false,
useBuiltIns = false, useBuiltIns = false,

View File

@ -1,8 +1,7 @@
// @flow
import presetStage3 from "./preset-stage-3"; import presetStage3 from "./preset-stage-3";
import * as babelPlugins from "./generated/plugins"; import * as babelPlugins from "./generated/plugins";
export default (_: any, opts: Object = {}) => { export default (_: any, opts: any = {}) => {
const { const {
loose = false, loose = false,
useBuiltIns = false, useBuiltIns = false,

View File

@ -1,7 +1,6 @@
// @flow
import * as babelPlugins from "./generated/plugins"; import * as babelPlugins from "./generated/plugins";
export default (_: any, opts: Object) => { export default (_: any, opts: any) => {
let loose = false; let loose = false;
if (opts !== undefined) { if (opts !== undefined) {

View File

@ -29,13 +29,24 @@ export default function () {
typeof packageJson["browser"] === "object" typeof packageJson["browser"] === "object"
) { ) {
for (const nodeFile in packageJson["browser"]) { for (const nodeFile in packageJson["browser"]) {
const browserFile = packageJson["browser"][nodeFile].replace( const browserFileAsJs = packageJson["browser"][nodeFile].replace(
/^(\.\/)?lib\//, /^(\.\/)?lib\//,
"src/" "src/"
); );
const nodeFileSrc = path.normalize(
const browserFileAsTs = browserFileAsJs.replace(/.js$/, ".ts");
const browserFile = fs.existsSync(browserFileAsTs)
? browserFileAsTs
: browserFileAsJs;
const nodeFileSrcAsJs = path.normalize(
nodeFile.replace(/^(\.\/)?lib\//, "src/") nodeFile.replace(/^(\.\/)?lib\//, "src/")
); );
const nodeFileSrcAsTs = nodeFileSrcAsJs.replace(/.js$/, ".ts");
const nodeFileSrc = fs.existsSync(nodeFileSrcAsTs)
? nodeFileSrcAsTs
: nodeFileSrcAsJs;
if (id.endsWith(nodeFileSrc)) { if (id.endsWith(nodeFileSrc)) {
if (browserFile === false) { if (browserFile === false) {
return ""; return "";
@ -98,12 +109,7 @@ export default function () {
if (!/\.[a-z]+$/.test(asJS)) asJS += ".js"; if (!/\.[a-z]+$/.test(asJS)) asJS += ".js";
const asTS = asJS.replace(/\.js$/, ".ts"); const asTS = asJS.replace(/\.js$/, ".ts");
try { return fs.existsSync(asTS) ? asTS : asJS;
fs.statSync(asTS);
return asTS;
} catch {
return asJS;
}
}, },
}; };
} }

View File

@ -29,6 +29,7 @@
"./packages/babel-plugin-transform-react-jsx/src/**/*.ts", "./packages/babel-plugin-transform-react-jsx/src/**/*.ts",
"./packages/babel-plugin-transform-runtime/src/**/*.ts", "./packages/babel-plugin-transform-runtime/src/**/*.ts",
"./packages/babel-plugin-transform-typescript/src/**/*.ts", "./packages/babel-plugin-transform-typescript/src/**/*.ts",
"./packages/babel-standalone/src/**/*.ts",
"./packages/babel-template/src/**/*.ts", "./packages/babel-template/src/**/*.ts",
"./packages/babel-traverse/src/**/*.ts", "./packages/babel-traverse/src/**/*.ts",
"./packages/babel-types/src/**/*.ts" "./packages/babel-types/src/**/*.ts"
@ -116,6 +117,9 @@
"@babel/plugin-transform-typescript": [ "@babel/plugin-transform-typescript": [
"./packages/babel-plugin-transform-typescript/src" "./packages/babel-plugin-transform-typescript/src"
], ],
"@babel/standalone": [
"./packages/babel-standalone/src"
],
"@babel/template": [ "@babel/template": [
"./packages/babel-template/src" "./packages/babel-template/src"
], ],