| 1 | import { declarePreset } from "@babel/helper-plugin-utils"; |
| 2 | import transformTypeScript from "@babel/plugin-transform-typescript"; |
| 3 | import normalizeOptions from "./normalize-options"; |
| 4 | import type { Options } from "./normalize-options"; |
| 5 | |
| 6 | export default declarePreset((api, opts: Options) => { |
| 7 | api.assertVersion(7); |
| 8 | |
| 9 | const { |
| 10 | allExtensions, |
| 11 | allowNamespaces, |
| 12 | disallowAmbiguousJSXLike, |
| 13 | isTSX, |
| 14 | jsxPragma, |
| 15 | jsxPragmaFrag, |
| 16 | onlyRemoveTypeImports, |
| 17 | optimizeConstEnums, |
| 18 | } = normalizeOptions(opts); |
| 19 | |
| 20 | const pluginOptions = process.env.BABEL_8_BREAKING |
| 21 | ? (isTSX: boolean, disallowAmbiguousJSXLike: boolean) => ({ |
| 22 | allowNamespaces, |
| 23 | disallowAmbiguousJSXLike, |
| 24 | isTSX, |
| 25 | jsxPragma, |
| 26 | jsxPragmaFrag, |
| 27 | onlyRemoveTypeImports, |
| 28 | optimizeConstEnums, |
| 29 | }) |
| 30 | : (isTSX: boolean, disallowAmbiguousJSXLike: boolean) => ({ |
| 31 | allowDeclareFields: opts.allowDeclareFields, |
| 32 | allowNamespaces, |
| 33 | disallowAmbiguousJSXLike, |
| 34 | isTSX, |
| 35 | jsxPragma, |
| 36 | jsxPragmaFrag, |
| 37 | onlyRemoveTypeImports, |
| 38 | optimizeConstEnums, |
| 39 | }); |
| 40 | |
| 41 | return { |
| 42 | overrides: allExtensions |
| 43 | ? [ |
| 44 | { |
| 45 | plugins: [ |
| 46 | [ |
| 47 | transformTypeScript, |
| 48 | pluginOptions(isTSX, disallowAmbiguousJSXLike), |
| 49 | ], |
| 50 | ], |
| 51 | }, |
| 52 | ] |
| 53 | : // Only set 'test' if explicitly requested, since it requires that |
| 54 | // Babel is being called` |
| 55 | [ |
| 56 | { |
| 57 | test: /\.ts$/, |
| 58 | plugins: [[transformTypeScript, pluginOptions(false, false)]], |
| 59 | }, |
| 60 | { |
| 61 | test: /\.mts$/, |
| 62 | sourceType: "module", |
| 63 | plugins: [[transformTypeScript, pluginOptions(false, true)]], |
| 64 | }, |
| 65 | { |
| 66 | test: /\.cts$/, |
| 67 | sourceType: "script", |
| 68 | plugins: [[transformTypeScript, pluginOptions(false, true)]], |
| 69 | }, |
| 70 | { |
| 71 | test: /\.tsx$/, |
| 72 | // disallowAmbiguousJSXLike is a no-op when parsing TSX, since it's |
| 73 | // always disallowed. |
| 74 | plugins: [[transformTypeScript, pluginOptions(true, false)]], |
| 75 | }, |
| 76 | ], |
| 77 | }; |
| 78 | }); |