@@ -26,7 +26,8 @@ import presetEnv from "@babel/preset-env";
2626import presetFlow from "@babel/preset-flow" ;
2727import presetReact from "@babel/preset-react" ;
2828import presetTypescript from "@babel/preset-typescript" ;
29- import type { InputOptions } from "@babel/core" ;
29+ import type { InputOptions , PluginItem } from "@babel/core" ;
30+ type PresetItem = NonNullable < InputOptions [ "presets" ] > [ number ] ;
3031
3132import { runScripts } from "./transformScriptTags.ts" ;
3233
@@ -85,7 +86,9 @@ export const availablePresets = {
8586
8687const isArray =
8788 Array . isArray ||
88- ( arg => Object . prototype . toString . call ( arg ) === "[object Array]" ) ;
89+ ( ( arg =>
90+ Object . prototype . toString . call ( arg ) ===
91+ "[object Array]" ) as typeof Array . isArray ) ;
8992
9093/**
9194 * Loads the given name (or [name, options] pair) from the given table object
@@ -107,6 +110,22 @@ function loadBuiltin(builtinTable: Record<string, unknown>, name: any) {
107110 return name ;
108111}
109112
113+ /**
114+ * This function will be called only when the preset or plugin, specified via string or [string, object],
115+ * is not available in the builtin table. Therefore we do not handle the case when the plugin/preset is an
116+ * object or a function here.
117+ * @param item
118+ * @returns
119+ */
120+ function getPluginOrPresetName ( item : PluginItem | PresetItem ) {
121+ if ( typeof item === "string" ) {
122+ return item ;
123+ } else if ( isArray ( item ) ) {
124+ return getPluginOrPresetName ( item [ 0 ] ) ;
125+ }
126+ return JSON . stringify ( item ) ;
127+ }
128+
110129/**
111130 * Parses plugin names and presets from the specified options.
112131 */
@@ -128,7 +147,7 @@ function processOptions(options: InputOptions) {
128147 }
129148 } else {
130149 throw new Error (
131- `Invalid preset specified in Babel options: "${ presetName } "` ,
150+ `Invalid preset specified in Babel options: "${ getPluginOrPresetName ( presetName ) } "` ,
132151 ) ;
133152 }
134153 return preset ;
@@ -140,7 +159,7 @@ function processOptions(options: InputOptions) {
140159
141160 if ( ! plugin ) {
142161 throw new Error (
143- `Invalid plugin specified in Babel options: "${ pluginName } "` ,
162+ `Invalid plugin specified in Babel options: "${ getPluginOrPresetName ( pluginName ) } "` ,
144163 ) ;
145164 }
146165 return plugin ;
0 commit comments