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}
export {${list}};
export const all = {${allList}};`;
file.path = "plugins.js";
export const all: { [k: string]: any } = {${allList}};`;
file.path = "plugins.ts";
file.contents = Buffer.from(formatCode(fileContents, dest));
callback(null, file);
})

View File

@ -3,6 +3,5 @@
* 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;

View File

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

View File

@ -9,6 +9,7 @@
/* global VERSION */
/* eslint-disable max-len */
/// <reference lib="dom" />
// $FlowIgnore
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));
}
export function transformFromAst(ast: Object, code: string, options: Object) {
export function transformFromAst(ast: any, code: string, options: any) {
return babelTransformFromAst(ast, code, processOptions(options));
}
export const availablePlugins = {};
@ -115,7 +116,7 @@ export const buildExternalHelpers = babelBuildExternalHelpers;
/**
* 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)) {
console.warn(
`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.
*/
export function registerPlugins(newPlugins: {
[string]: Object | Function,
[x: string]: any | Function;
}): void {
Object.keys(newPlugins).forEach(name =>
registerPlugin(name, newPlugins[name]),
@ -138,7 +139,7 @@ export function registerPlugins(newPlugins: {
/**
* 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 (name === "env") {
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.
*/
export function registerPresets(newPresets: {
[string]: Object | Function,
[x: string]: any | Function;
}): void {
Object.keys(newPresets).forEach(name =>
registerPreset(name, newPresets[name]),
@ -201,8 +202,8 @@ registerPresets({
flow: presetFlow,
});
// $FlowIgnore
export const version = VERSION;
// @ts-ignore VERSION is to be replaced by rollup
export const version: string = VERSION;
function onDOMContentLoaded() {
transformScriptTags();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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