Skip to content

Commit

Permalink
fix: fix the type script errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jun 30, 2022
1 parent 2dc6ae3 commit b636132
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 59 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"devDependencies": {
"@babel/cli": "^7.13.14",
"@babel/core": "^7.13.14",
"@types/babel__core": "^7.1.19",
"@types/babel__preset-env": "7.9.2",
"eslint-config-atomic": "^1.18.1",
"npm-check-updates": "14.1.1",
"prettier-config-atomic": "^3.0.10",
Expand Down
70 changes: 19 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import type { ConfigAPI, PluginItem, TransformOptions } from "@babel/core"

let keepModulesEnv = false // false by default

if (process.env.BABEL_KEEP_MODULES === "true") {
keepModulesEnv = true
}

function handleOptions(options) {
export type Options = {
targets?: typeof import("babel__preset-env")
keepModules?: boolean
addModuleExports?: boolean
addModuleExportsDefaultProperty?: boolean
react?: boolean | Record<string, any>
flow?: boolean | Record<string, any>
typescript?: boolean | Record<string, any>
removeAllUseStrict?: boolean
notStrictDirectiveTriggers?: string[]
notStrictCommentTriggers?: string[]
}

function handleOptions(options: Options) {
let {
targets,
keepModules,
Expand Down Expand Up @@ -77,7 +92,7 @@ function handleOptions(options) {
}

// eslint-disable-next-line no-unused-vars
module.exports = (api, options, dirname) => {
module.exports = (_api: ConfigAPI, options: Options, _dirname: string): TransformOptions => {
const {
targets,
keepModules,
Expand All @@ -93,25 +108,25 @@ module.exports = (api, options, dirname) => {

const presets = [
[
require("@babel/preset-env"),
require("@babel/preset-env") as typeof import("babel__preset-env"),
{
targets,
modules: keepModules ? false : "commonjs",
},
],
]
] as PluginItem[]

if (react) {
if (react !== false) {
const presetReact = require("@babel/preset-react")
presets.push(typeof react === "object" ? [presetReact, react] : presetReact)
}

if (flow) {
if (flow !== false) {
const presetFlow = require("@babel/preset-flow")
presets.push(typeof flow === "object" ? [presetFlow, flow] : presetFlow)
}

if (typescript) {
if (typescript !== false) {
const presetTypeScript = require("@babel/preset-typescript")
presets.push(typeof typescript === "object" ? [presetTypeScript, typescript] : presetTypeScript)
}
Expand Down Expand Up @@ -154,7 +169,7 @@ module.exports = (api, options, dirname) => {

// reserved keywords
require("@babel/plugin-transform-reserved-words"),
]
] as PluginItem[]

// transform modules (e.g when without Rollup)
if (!keepModules) {
Expand Down

0 comments on commit b636132

Please sign in to comment.