From b670c1107027efd46459433c1aa47910ec5d3625 Mon Sep 17 00:00:00 2001 From: Bogdan Savluk Date: Fri, 14 May 2021 15:54:09 +0200 Subject: [PATCH] convert @babel/standalone to typescript (#13212) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- Gulpfile.mjs | 4 ++-- ...-package-shim.js => babel-package-shim.ts} | 3 +-- .../src/generated/{plugins.js => plugins.ts} | 2 +- .../src/{index.js => index.ts} | 17 +++++++------- .../{preset-es2015.js => preset-es2015.ts} | 3 +-- .../{preset-stage-0.js => preset-stage-0.ts} | 3 +-- .../{preset-stage-1.js => preset-stage-1.ts} | 3 +-- .../{preset-stage-2.js => preset-stage-2.ts} | 3 +-- .../{preset-stage-3.js => preset-stage-3.ts} | 3 +-- ...rmScriptTags.js => transformScriptTags.ts} | 0 scripts/rollup-plugin-babel-source.js | 22 ++++++++++++------- tsconfig.json | 4 ++++ 12 files changed, 36 insertions(+), 31 deletions(-) rename packages/babel-standalone/src/{babel-package-shim.js => babel-package-shim.ts} (83%) rename packages/babel-standalone/src/generated/{plugins.js => plugins.ts} (99%) rename packages/babel-standalone/src/{index.js => index.ts} (93%) rename packages/babel-standalone/src/{preset-es2015.js => preset-es2015.ts} (96%) rename packages/babel-standalone/src/{preset-stage-0.js => preset-stage-0.ts} (91%) rename packages/babel-standalone/src/{preset-stage-1.js => preset-stage-1.ts} (93%) rename packages/babel-standalone/src/{preset-stage-2.js => preset-stage-2.ts} (91%) rename packages/babel-standalone/src/{preset-stage-3.js => preset-stage-3.ts} (93%) rename packages/babel-standalone/src/{transformScriptTags.js => transformScriptTags.ts} (100%) diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 3e46ed3e62..2abf94c7f8 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -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); }) diff --git a/packages/babel-standalone/src/babel-package-shim.js b/packages/babel-standalone/src/babel-package-shim.ts similarity index 83% rename from packages/babel-standalone/src/babel-package-shim.js rename to packages/babel-standalone/src/babel-package-shim.ts index a67c572f3e..0c42c63339 100644 --- a/packages/babel-standalone/src/babel-package-shim.js +++ b/packages/babel-standalone/src/babel-package-shim.ts @@ -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; diff --git a/packages/babel-standalone/src/generated/plugins.js b/packages/babel-standalone/src/generated/plugins.ts similarity index 99% rename from packages/babel-standalone/src/generated/plugins.js rename to packages/babel-standalone/src/generated/plugins.ts index 8d0b9b6c5d..d97f81053b 100644 --- a/packages/babel-standalone/src/generated/plugins.js +++ b/packages/babel-standalone/src/generated/plugins.ts @@ -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, diff --git a/packages/babel-standalone/src/index.js b/packages/babel-standalone/src/index.ts similarity index 93% rename from packages/babel-standalone/src/index.js rename to packages/babel-standalone/src/index.ts index 6a5ad0634c..81e6feb4da 100644 --- a/packages/babel-standalone/src/index.js +++ b/packages/babel-standalone/src/index.ts @@ -9,6 +9,7 @@ /* global VERSION */ /* eslint-disable max-len */ +/// // $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(); diff --git a/packages/babel-standalone/src/preset-es2015.js b/packages/babel-standalone/src/preset-es2015.ts similarity index 96% rename from packages/babel-standalone/src/preset-es2015.js rename to packages/babel-standalone/src/preset-es2015.ts index 0add83d9be..c4a76a9f96 100644 --- a/packages/babel-standalone/src/preset-es2015.js +++ b/packages/babel-standalone/src/preset-es2015.ts @@ -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; diff --git a/packages/babel-standalone/src/preset-stage-0.js b/packages/babel-standalone/src/preset-stage-0.ts similarity index 91% rename from packages/babel-standalone/src/preset-stage-0.js rename to packages/babel-standalone/src/preset-stage-0.ts index 54643f575c..452991bcca 100644 --- a/packages/babel-standalone/src/preset-stage-0.js +++ b/packages/babel-standalone/src/preset-stage-0.ts @@ -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, diff --git a/packages/babel-standalone/src/preset-stage-1.js b/packages/babel-standalone/src/preset-stage-1.ts similarity index 93% rename from packages/babel-standalone/src/preset-stage-1.js rename to packages/babel-standalone/src/preset-stage-1.ts index a3b891743a..5448a3189c 100644 --- a/packages/babel-standalone/src/preset-stage-1.js +++ b/packages/babel-standalone/src/preset-stage-1.ts @@ -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, diff --git a/packages/babel-standalone/src/preset-stage-2.js b/packages/babel-standalone/src/preset-stage-2.ts similarity index 91% rename from packages/babel-standalone/src/preset-stage-2.js rename to packages/babel-standalone/src/preset-stage-2.ts index f0eff5756e..2c4610116e 100644 --- a/packages/babel-standalone/src/preset-stage-2.js +++ b/packages/babel-standalone/src/preset-stage-2.ts @@ -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, diff --git a/packages/babel-standalone/src/preset-stage-3.js b/packages/babel-standalone/src/preset-stage-3.ts similarity index 93% rename from packages/babel-standalone/src/preset-stage-3.js rename to packages/babel-standalone/src/preset-stage-3.ts index 78701c8767..4fa70155f0 100644 --- a/packages/babel-standalone/src/preset-stage-3.js +++ b/packages/babel-standalone/src/preset-stage-3.ts @@ -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) { diff --git a/packages/babel-standalone/src/transformScriptTags.js b/packages/babel-standalone/src/transformScriptTags.ts similarity index 100% rename from packages/babel-standalone/src/transformScriptTags.js rename to packages/babel-standalone/src/transformScriptTags.ts diff --git a/scripts/rollup-plugin-babel-source.js b/scripts/rollup-plugin-babel-source.js index 522dfd9789..09b06e0203 100644 --- a/scripts/rollup-plugin-babel-source.js +++ b/scripts/rollup-plugin-babel-source.js @@ -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; }, }; } diff --git a/tsconfig.json b/tsconfig.json index 61c1dc005e..acaed6e185 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" ],