Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/babel/src/babelCompat.dummy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// oxlint-disable eslint-plugin-unicorn/no-empty-file

// Empty file to emulate the case where `@babel/core` does not have types (pre v8)
// and `@types/babel__core` is not installed.
15 changes: 14 additions & 1 deletion packages/babel/src/babelCompat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import * as babel from '@babel/core'
// https://github.com/type-challenges/type-challenges/issues/29285
type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false

/** **When using babel 7, install `@types/babel__core` to get proper types.** */
type InputOptionsFallback = {
/** **When using babel 7, install `@types/babel__core` to get proper types.** */
plugins?: unknown[]
/** **When using babel 7, install `@types/babel__core` to get proper types.** */
presets?: unknown[]
}

// @ts-ignore -- InputOptions doesn't exist in Babel 7
type InputOptions8 = babel.InputOptions
// @ts-ignore -- PresetItem doesn't exist in Babel 7
Expand All @@ -25,7 +33,12 @@ type PluginObj<T> = babel.PluginObj<T>
// @ts-ignore -- BabelFileResult doesn't exist in Babel 8
type BabelFileResult = babel.BabelFileResult

export type InputOptions = IsAny<InputOptions8> extends false ? InputOptions8 : TransformOptions
export type InputOptions =
IsAny<InputOptions8> extends false
? InputOptions8
: IsAny<TransformOptions> extends false
? TransformOptions
: InputOptionsFallback
export type PresetItem = IsAny<PresetItem8> extends false ? PresetItem8 : babel.PluginItem
export type PluginObject<T = babel.PluginPass> =
IsAny<PluginObject8<T>> extends false ? PluginObject8<T> : PluginObj<T>
Expand Down
9 changes: 9 additions & 0 deletions packages/babel/src/babelCompat.typetest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { PluginOptions } from './options'

export const _testPlugin: PluginOptions = {
plugins: ['some-plugin'],
}

export const _testPreset: PluginOptions = {
presets: ['some-preset'],
}
36 changes: 19 additions & 17 deletions packages/babel/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import {
} from './rolldownPreset'
import { filterMap } from './utils'

export interface InnerTransformOptions extends Pick<
babel.InputOptions,
| 'assumptions'
| 'auxiliaryCommentAfter'
| 'auxiliaryCommentBefore'
| 'exclude'
| 'comments'
| 'compact'
| 'cwd'
| 'generatorOpts'
| 'include'
| 'parserOpts'
| 'plugins'
| 'retainLines'
| 'shouldPrintComment'
| 'targets'
| 'wrapPluginVisitorMethod'
export interface InnerTransformOptions extends Partial<
Pick<
babel.InputOptions,
| 'assumptions'
| 'auxiliaryCommentAfter'
| 'auxiliaryCommentBefore'
| 'exclude'
| 'comments'
| 'compact'
| 'cwd'
| 'generatorOpts'
| 'include'
| 'parserOpts'
| 'plugins'
| 'retainLines'
| 'shouldPrintComment'
| 'targets'
| 'wrapPluginVisitorMethod'
>
> {
/**
* List of presets (a set of plugins) to load and use
Expand Down
11 changes: 11 additions & 0 deletions packages/babel/src/tsconfig.typetest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.common.json",
"compilerOptions": {
"paths": {
"@babel/core": ["./babelCompat.dummy.ts"]
},
"skipLibCheck": true
},
"include": ["./babelCompat.typetest.ts"],
"exclude": []
}
2 changes: 1 addition & 1 deletion tsconfig.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"isolatedModules": true,
"declaration": true
},
"exclude": ["./examples"]
"exclude": ["./examples", "./packages/babel/src/babelCompat.typetest.ts"]
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"references": [{ "path": "./tsconfig.common.json" }, { "path": "./examples/tsconfig.json" }],
"references": [
{ "path": "./tsconfig.common.json" },
{ "path": "./examples/tsconfig.json" },
{ "path": "./packages/babel/src/tsconfig.typetest.json" }
],
"include": []
}