|
| 1 | +import { isPackageAvailable, showMissingPackages } from "@anolilab/package-json-utils"; |
| 2 | +// @ts-expect-error TS2305: Module '"@babel/helper-plugin-utils"' has no exported member 'declare'. |
| 3 | +import { declare } from "@babel/helper-plugin-utils"; |
| 4 | + |
| 5 | +type BabelAPI = { |
| 6 | + assertVersion: (version: string) => void; |
| 7 | + env: (env: string) => boolean; |
| 8 | + cache: { |
| 9 | + using: (fn: () => boolean) => boolean; |
| 10 | + }; |
| 11 | +}; |
| 12 | + |
| 13 | +type Options = { |
| 14 | + modules?: "auto" | true | false; |
| 15 | + targets?: any; |
| 16 | + removePropTypes?: boolean | object; |
| 17 | + loose?: boolean; |
| 18 | + looseClasses?: boolean; |
| 19 | + looseObjectRestSpread?: boolean; |
| 20 | + looseComputedProperties?: boolean; |
| 21 | + looseParameters?: boolean; |
| 22 | + looseTemplateLiterals?: boolean; |
| 23 | + typescript?: boolean; |
| 24 | + react?: boolean | object; |
| 25 | + polyfillRegenerator?: boolean; |
| 26 | + useBuiltIns?: boolean; |
| 27 | + corejs?: boolean | { method?: string; version?: number }; |
| 28 | + debug?: boolean; |
| 29 | + development?: boolean; |
| 30 | +}; |
| 31 | + |
| 32 | +const babelPresetTypescript = "@babel/preset-typescript"; |
| 33 | +const babelPluginTransformTypescript = "@babel/plugin-transform-typescript"; |
| 34 | +const babelPluginSyntaxJSX = "@babel/plugin-syntax-jsx"; |
| 35 | +const babelPresetReact = "@babel/preset-react"; |
| 36 | +const babelPluginTransformReactRemovePropTypes = "babel-plugin-transform-react-remove-prop-types"; |
| 37 | + |
| 38 | +const preset = declare((api: BabelAPI, options: Options) => { |
| 39 | + // see docs about api at https://babeljs.io/docs/en/config-files#apicache |
| 40 | + api.assertVersion("^7.13"); |
| 41 | + |
| 42 | + const { |
| 43 | + modules = "auto", |
| 44 | + targets, |
| 45 | + removePropTypes: removePropertyTypes = false, |
| 46 | + loose = true, |
| 47 | + looseClasses = true, |
| 48 | + looseObjectRestSpread = true, |
| 49 | + looseComputedProperties = true, |
| 50 | + looseParameters = true, |
| 51 | + looseTemplateLiterals = true, |
| 52 | + typescript = false, |
| 53 | + react = false, |
| 54 | + polyfillRegenerator = false, |
| 55 | + useBuiltIns = false, |
| 56 | + corejs = false, |
| 57 | + } = options; |
| 58 | + |
| 59 | + if (modules !== undefined && modules !== "auto") { |
| 60 | + throw new TypeError( |
| 61 | + '@anolilab/babel-preset only accepts `true`, `false`, or `"auto"` as the value of the "modules" option', |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if (removePropertyTypes && !react) { |
| 66 | + throw new Error("removePropTypes can't be enabled if react is disabled."); |
| 67 | + } |
| 68 | + |
| 69 | + const install: string[] = []; |
| 70 | + |
| 71 | + if (typescript) { |
| 72 | + if (!isPackageAvailable(babelPresetTypescript)) { |
| 73 | + install.push(babelPresetTypescript); |
| 74 | + } |
| 75 | + |
| 76 | + if (!isPackageAvailable(babelPluginTransformTypescript)) { |
| 77 | + install.push(babelPluginTransformTypescript); |
| 78 | + } |
| 79 | + |
| 80 | + if (!isPackageAvailable(babelPluginSyntaxJSX)) { |
| 81 | + install.push(babelPluginSyntaxJSX); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (react) { |
| 86 | + if (!isPackageAvailable(babelPresetReact)) { |
| 87 | + install.push(babelPresetReact); |
| 88 | + } |
| 89 | + |
| 90 | + if (removePropertyTypes && !isPackageAvailable(babelPluginTransformReactRemovePropTypes)) { |
| 91 | + install.push(babelPluginTransformReactRemovePropTypes); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (install.length > 0) { |
| 96 | + showMissingPackages("@anolilab/babel-preset", install); |
| 97 | + } |
| 98 | + |
| 99 | + const debug = typeof options.debug === "boolean" ? options.debug : false; |
| 100 | + const development = |
| 101 | + typeof options.development === "boolean" |
| 102 | + ? options.development |
| 103 | + : api.cache.using(() => process.env?.["NODE_ENV"] === "development"); |
| 104 | + |
| 105 | + const presets = [ |
| 106 | + [ |
| 107 | + "@babel/preset-env", |
| 108 | + { |
| 109 | + debug, |
| 110 | + bugfixes: true, |
| 111 | + useBuiltIns, |
| 112 | + exclude: ["transform-async-to-generator", "transform-regenerator"], |
| 113 | + // @ts-expect-error TS2367: This comparison appears to be unintentional because the types 'string' and 'boolean' have no overlap. |
| 114 | + modules: modules === false ? false : "auto", |
| 115 | + targets, |
| 116 | + shippedProposals: api.env("modern"), |
| 117 | + loose, |
| 118 | + }, |
| 119 | + ], |
| 120 | + typescript |
| 121 | + ? [ |
| 122 | + "@babel/preset-typescript", |
| 123 | + { |
| 124 | + allExtensions: true, |
| 125 | + isTSX: true, |
| 126 | + }, |
| 127 | + ] |
| 128 | + : undefined, |
| 129 | + react ? ["@babel/preset-react", { development, ...(typeof react === "object" ? react : {}) }] : undefined, |
| 130 | + ].filter(Boolean) as [string, object?][]; |
| 131 | + |
| 132 | + const plugins = [ |
| 133 | + "@babel/plugin-transform-shorthand-properties", |
| 134 | + "@babel/plugin-transform-block-scoping", |
| 135 | + "babel-plugin-annotate-pure-calls", |
| 136 | + "babel-plugin-dev-expression", |
| 137 | + "@babel/plugin-proposal-class-static-block", |
| 138 | + [ |
| 139 | + "@babel/plugin-proposal-class-properties", |
| 140 | + { |
| 141 | + loose: looseClasses, |
| 142 | + }, |
| 143 | + ], |
| 144 | + [ |
| 145 | + "@babel/plugin-proposal-private-methods", |
| 146 | + { |
| 147 | + loose: looseClasses, |
| 148 | + }, |
| 149 | + ], |
| 150 | + [ |
| 151 | + "@babel/plugin-proposal-private-property-in-object", |
| 152 | + { |
| 153 | + loose: looseClasses, |
| 154 | + }, |
| 155 | + ], |
| 156 | + [ |
| 157 | + "@babel/plugin-transform-classes", |
| 158 | + { |
| 159 | + loose: looseClasses, |
| 160 | + }, |
| 161 | + ], |
| 162 | + [ |
| 163 | + "@babel/plugin-proposal-decorators", |
| 164 | + { |
| 165 | + legacy: true, |
| 166 | + }, |
| 167 | + ], |
| 168 | + [ |
| 169 | + "@babel/plugin-transform-computed-properties", |
| 170 | + { |
| 171 | + loose: looseComputedProperties, |
| 172 | + }, |
| 173 | + ], |
| 174 | + [ |
| 175 | + "@babel/plugin-transform-parameters", |
| 176 | + { |
| 177 | + loose: looseParameters, |
| 178 | + }, |
| 179 | + ], |
| 180 | + [ |
| 181 | + "@babel/plugin-transform-template-literals", |
| 182 | + { |
| 183 | + loose: looseTemplateLiterals, |
| 184 | + }, |
| 185 | + ], |
| 186 | + [ |
| 187 | + "@babel/plugin-proposal-object-rest-spread", |
| 188 | + { |
| 189 | + loose: looseObjectRestSpread, |
| 190 | + useBuiltIns, |
| 191 | + }, |
| 192 | + ], |
| 193 | + react && removePropertyTypes |
| 194 | + ? [ |
| 195 | + "babel-plugin-transform-react-remove-prop-types", |
| 196 | + { |
| 197 | + mode: "unsafe-wrap", |
| 198 | + ignoreFilenames: ["node_modules"], |
| 199 | + ...((removePropertyTypes as object) || {}), |
| 200 | + }, |
| 201 | + ] |
| 202 | + : undefined, |
| 203 | + "@babel/plugin-proposal-export-namespace-from", |
| 204 | + typescript ? "@babel/plugin-transform-typescript" : undefined, |
| 205 | + typescript ? "@babel/plugin-syntax-jsx" : undefined, |
| 206 | + "@babel/plugin-transform-property-mutators", |
| 207 | + "@babel/plugin-transform-member-expression-literals", |
| 208 | + "@babel/plugin-transform-property-literals", |
| 209 | + "@babel/plugin-transform-for-of", |
| 210 | + "@babel/plugin-transform-arrow-functions", |
| 211 | + "@babel/plugin-transform-destructuring", |
| 212 | + "@babel/plugin-transform-spread", |
| 213 | + "@babel/plugin-proposal-nullish-coalescing-operator", |
| 214 | + "@babel/plugin-proposal-numeric-separator", |
| 215 | + "@babel/plugin-proposal-optional-catch-binding", |
| 216 | + "@babel/plugin-proposal-optional-chaining", |
| 217 | + "@babel/plugin-proposal-export-default-from", |
| 218 | + "@babel/plugin-syntax-bigint", |
| 219 | + "@babel/plugin-syntax-async-generators", |
| 220 | + "babel-plugin-macros", |
| 221 | + polyfillRegenerator |
| 222 | + ? [ |
| 223 | + "babel-plugin-polyfill-regenerator", |
| 224 | + { |
| 225 | + method: "usage-pure", |
| 226 | + }, |
| 227 | + ] |
| 228 | + : undefined, |
| 229 | + typeof corejs === "object" |
| 230 | + ? [ |
| 231 | + "babel-plugin-polyfill-corejs3", |
| 232 | + { |
| 233 | + method: corejs.method || "usage-global", |
| 234 | + absoluteImports: "core-js", |
| 235 | + version: corejs.version, |
| 236 | + ...corejs, |
| 237 | + }, |
| 238 | + ] |
| 239 | + : undefined, |
| 240 | + ].filter(Boolean) as [string, object?][]; |
| 241 | + |
| 242 | + return { |
| 243 | + assumptions: { |
| 244 | + noDocumentAll: true, |
| 245 | + }, |
| 246 | + presets, |
| 247 | + plugins, |
| 248 | + }; |
| 249 | +}); |
| 250 | + |
| 251 | +export default preset; |
0 commit comments