| 1 | /** |
| 2 | * Entry point for @babel/standalone. This wraps Babel's API in a version that's |
| 3 | * friendlier for use in web browsers. It removes the automagical detection of |
| 4 | * plugins, instead explicitly registering all the available plugins and |
| 5 | * presets, and requiring custom ones to be registered through `registerPlugin` |
| 6 | * and `registerPreset` respectively. |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | /* global VERSION */ |
| 11 | /* eslint-disable max-len */ |
| 12 | /// <reference lib="dom" /> |
| 13 | |
| 14 | import { |
| 15 | transformFromAstSync as babelTransformFromAstSync, |
| 16 | transformSync as babelTransformSync, |
| 17 | buildExternalHelpers as babelBuildExternalHelpers, |
| 18 | } from "@babel/core"; |
| 19 | import { all } from "./generated/plugins"; |
| 20 | import preset2015 from "./preset-es2015"; |
| 21 | import presetStage0 from "./preset-stage-0"; |
| 22 | import presetStage1 from "./preset-stage-1"; |
| 23 | import presetStage2 from "./preset-stage-2"; |
| 24 | import presetStage3 from "./preset-stage-3"; |
| 25 | import presetEnv from "@babel/preset-env"; |
| 26 | import presetFlow from "@babel/preset-flow"; |
| 27 | import presetReact from "@babel/preset-react"; |
| 28 | import presetTypescript from "@babel/preset-typescript"; |
| 29 | import type { InputOptions } from "@babel/core"; |
| 30 | |
| 31 | import { runScripts } from "./transformScriptTags"; |
| 32 | |
| 33 | export const availablePlugins: typeof all = {}; |
| 34 | |
| 35 | // All the plugins we should bundle |
| 36 | // Want to get rid of this long list of allowed plugins? |
| 37 | // Wait! Please read https://github.com/babel/babel/pull/6177 first. |
| 38 | registerPlugins(all); |
| 39 | |
| 40 | // All the presets we should bundle |
| 41 | // Want to get rid of this list of allowed presets? |
| 42 | // Wait! Please read https://github.com/babel/babel/pull/6177 first. |
| 43 | export const availablePresets = { |
| 44 | env: presetEnv, |
| 45 | es2015: preset2015, |
| 46 | es2016: () => { |
| 47 | return { |
| 48 | plugins: [availablePlugins["transform-exponentiation-operator"]], |
| 49 | }; |
| 50 | }, |
| 51 | es2017: () => { |
| 52 | return { |
| 53 | plugins: [availablePlugins["transform-async-to-generator"]], |
| 54 | }; |
| 55 | }, |
| 56 | react: presetReact, |
| 57 | "stage-0": presetStage0, |
| 58 | "stage-1": presetStage1, |
| 59 | "stage-2": presetStage2, |
| 60 | "stage-3": presetStage3, |
| 61 | "es2015-loose": { |
| 62 | presets: [[preset2015, { loose: true }]], |
| 63 | }, |
| 64 | // ES2015 preset with es2015-modules-commonjs removed |
| 65 | "es2015-no-commonjs": { |
| 66 | presets: [[preset2015, { modules: false }]], |
| 67 | }, |
| 68 | typescript: presetTypescript, |
| 69 | flow: presetFlow, |
| 70 | }; |
| 71 | |
| 72 | const isArray = |
| 73 | Array.isArray || |
| 74 | (arg => Object.prototype.toString.call(arg) === "[object Array]"); |
| 75 | |
| 76 | /** |
| 77 | * Loads the given name (or [name, options] pair) from the given table object |
| 78 | * holding the available presets or plugins. |
| 79 | * |
| 80 | * Returns undefined if the preset or plugin is not available; passes through |
| 81 | * name unmodified if it (or the first element of the pair) is not a string. |
| 82 | */ |
| 83 | function loadBuiltin(builtinTable: Record<string, unknown>, name: any) { |
| 84 | if (isArray(name) && typeof name[0] === "string") { |
| 85 | if (Object.prototype.hasOwnProperty.call(builtinTable, name[0])) { |
| 86 | return [builtinTable[name[0]]].concat(name.slice(1)); |
| 87 | } |
| 88 | return; |
| 89 | } else if (typeof name === "string") { |
| 90 | return builtinTable[name]; |
| 91 | } |
| 92 | // Could be an actual preset/plugin module |
| 93 | return name; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Parses plugin names and presets from the specified options. |
| 98 | */ |
| 99 | function processOptions(options: InputOptions) { |
| 100 | // Parse preset names |
| 101 | const presets = (options.presets || []).map(presetName => { |
| 102 | const preset = loadBuiltin(availablePresets, presetName); |
| 103 | |
| 104 | if (preset) { |
| 105 | // workaround for babel issue |
| 106 | // at some point, babel copies the preset, losing the non-enumerable |
| 107 | // buildPreset key; convert it into an enumerable key. |
| 108 | if ( |
| 109 | isArray(preset) && |
| 110 | typeof preset[0] === "object" && |
| 111 | Object.prototype.hasOwnProperty.call(preset[0], "buildPreset") |
| 112 | ) { |
| 113 | preset[0] = { ...preset[0], buildPreset: preset[0].buildPreset }; |
| 114 | } |
| 115 | } else { |
| 116 | throw new Error( |
| 117 | `Invalid preset specified in Babel options: "${presetName}"`, |
| 118 | ); |
| 119 | } |
| 120 | return preset; |
| 121 | }); |
| 122 | |
| 123 | // Parse plugin names |
| 124 | const plugins = (options.plugins || []).map(pluginName => { |
| 125 | const plugin = loadBuiltin(availablePlugins, pluginName); |
| 126 | |
| 127 | if (!plugin) { |
| 128 | throw new Error( |
| 129 | `Invalid plugin specified in Babel options: "${pluginName}"`, |
| 130 | ); |
| 131 | } |
| 132 | return plugin; |
| 133 | }); |
| 134 | |
| 135 | return { |
| 136 | babelrc: false, |
| 137 | ...options, |
| 138 | presets, |
| 139 | plugins, |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | export function transform(code: string, options: InputOptions) { |
| 144 | return babelTransformSync(code, processOptions(options)); |
| 145 | } |
| 146 | |
| 147 | export function transformFromAst( |
| 148 | ast: Parameters<typeof babelTransformFromAstSync>[0], |
| 149 | code: string, |
| 150 | options: InputOptions, |
| 151 | ) { |
| 152 | return babelTransformFromAstSync(ast, code, processOptions(options)); |
| 153 | } |
| 154 | |
| 155 | export const buildExternalHelpers = babelBuildExternalHelpers; |
| 156 | /** |
| 157 | * Registers a named plugin for use with Babel. |
| 158 | */ |
| 159 | export function registerPlugin(name: string, plugin: any | Function): void { |
| 160 | if (Object.prototype.hasOwnProperty.call(availablePlugins, name)) { |
| 161 | console.warn( |
| 162 | `A plugin named "${name}" is already registered, it will be overridden`, |
| 163 | ); |
| 164 | } |
| 165 | availablePlugins[name] = plugin; |
| 166 | } |
| 167 | /** |
| 168 | * Registers multiple plugins for use with Babel. `newPlugins` should be an object where the key |
| 169 | * is the name of the plugin, and the value is the plugin itself. |
| 170 | */ |
| 171 | export function registerPlugins(newPlugins: { |
| 172 | [x: string]: any | Function; |
| 173 | }): void { |
| 174 | Object.keys(newPlugins).forEach(name => |
| 175 | registerPlugin(name, newPlugins[name]), |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Registers a named preset for use with Babel. |
| 181 | */ |
| 182 | export function registerPreset(name: string, preset: any | Function): void { |
| 183 | if (Object.prototype.hasOwnProperty.call(availablePresets, name)) { |
| 184 | if (name === "env") { |
| 185 | console.warn( |
| 186 | "@babel/preset-env is now included in @babel/standalone, please remove @babel/preset-env-standalone", |
| 187 | ); |
| 188 | } else { |
| 189 | console.warn( |
| 190 | `A preset named "${name}" is already registered, it will be overridden`, |
| 191 | ); |
| 192 | } |
| 193 | } |
| 194 | // @ts-expect-error mutating available presets |
| 195 | availablePresets[name] = preset; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Registers multiple presets for use with Babel. `newPresets` should be an object where the key |
| 200 | * is the name of the preset, and the value is the preset itself. |
| 201 | */ |
| 202 | export function registerPresets(newPresets: { |
| 203 | [x: string]: any | Function; |
| 204 | }): void { |
| 205 | Object.keys(newPresets).forEach(name => |
| 206 | registerPreset(name, newPresets[name]), |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | // @ts-expect-error VERSION is to be replaced by rollup |
| 211 | export const version: string = VERSION; |
| 212 | |
| 213 | function onDOMContentLoaded() { |
| 214 | transformScriptTags(); |
| 215 | } |
| 216 | |
| 217 | // Listen for load event if we're in a browser and then kick off finding and |
| 218 | // running of scripts with "text/babel" type. |
| 219 | if (typeof window !== "undefined" && window?.addEventListener) { |
| 220 | window.addEventListener("DOMContentLoaded", onDOMContentLoaded, false); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Transform <script> tags with "text/babel" type. |
| 225 | * @param {Array} scriptTags specify script tags to transform, transform all in the <head> if not given |
| 226 | */ |
| 227 | export function transformScriptTags( |
| 228 | scriptTags?: HTMLCollectionOf<HTMLScriptElement>, |
| 229 | ) { |
| 230 | runScripts(transform, scriptTags); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Disables automatic transformation of <script> tags with "text/babel" type. |
| 235 | */ |
| 236 | export function disableScriptTags() { |
| 237 | window.removeEventListener("DOMContentLoaded", onDOMContentLoaded); |
| 238 | } |