Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📁 types(patch): improve typings (@roots/bud-react) #2442

Merged
merged 1 commit into from
Sep 10, 2023
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
41 changes: 8 additions & 33 deletions sources/@roots/bud-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,12 @@
],
"type": "module",
"exports": {
".": {
"import": "./lib/index.js",
"default": "./lib/index.js"
},
"./babel-refresh": {
"import": "./lib/babel-refresh/index.js",
"default": "./lib/babel-refresh/index.js"
},
"./extension": {
"import": "./lib/extension.js",
"default": "./lib/extension.js"
},
"./react-refresh": {
"import": "./lib/react-refresh/index.js",
"default": "./lib/react-refresh/index.js"
},
"./swc-refresh": {
"import": "./lib/swc-refresh/index.js",
"default": "./lib/swc-refresh/index.js"
},
"./typescript-refresh": {
"import": "./lib/typescript-refresh/index.js",
"default": "./lib/typescript-refresh/index.js"
},
"./types": {
"import": "./lib/types.js",
"default": "./lib/types.js"
}
".": "./lib/index.js",
"./babel-refresh": "./lib/babel-refresh/index.js",
"./extension": "./lib/extension/index.js",
"./react-refresh": "./lib/react-refresh/index.js",
"./swc-refresh": "./lib/swc-refresh/index.js",
"./typescript-refresh": "./lib/typescript-refresh/index.js"
},
"typesVersions": {
"*": {
Expand All @@ -87,7 +65,7 @@
"./lib/babel-refresh/index.d.ts"
],
"extension": [
"./lib/extension.d.ts"
"./lib/extension/index.d.ts"
],
"react-refresh": [
"./lib/react-refresh/index.d.ts"
Expand All @@ -97,9 +75,6 @@
],
"typescript-refresh": [
"./lib/typescript-refresh/index.d.ts"
],
"types": [
"./lib/types.d.ts"
]
}
},
Expand All @@ -108,6 +83,7 @@
"devDependencies": {
"@babel/core": "7.22.11",
"@roots/bud": "workspace:*",
"@roots/bud-babel": "workspace:*",
"@roots/bud-esbuild": "workspace:*",
"@roots/bud-swc": "workspace:*",
"@roots/bud-typescript": "workspace:*",
Expand All @@ -120,7 +96,6 @@
"dependencies": {
"@babel/preset-react": "7.22.5",
"@pmmmwh/react-refresh-webpack-plugin": "0.5.11",
"@roots/bud-babel": "workspace:*",
"@roots/bud-framework": "workspace:*",
"@roots/bud-support": "workspace:*",
"react": "18.2.0",
Expand Down
28 changes: 0 additions & 28 deletions sources/@roots/bud-react/src/babel-refresh/extension.ts

This file was deleted.

34 changes: 24 additions & 10 deletions sources/@roots/bud-react/src/babel-refresh/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// Copyright © Roots Software Foundation LLC
// Licensed under the MIT license.
import type {Bud} from '@roots/bud-framework'

import {Extension} from '@roots/bud-framework/extension'
import {
bind,
development,
label,
} from '@roots/bud-framework/extension/decorators'

/**
* Adds TypeScript react-refresh transform
*
* @see https://bud.js.org
* @see https://github.com/roots/bud
*
* @packageDocumentation
* Register `react-refresh-typescript` transform with TSC compiler
*/
@label(`@roots/bud-react/babel-refresh`)
@development
export default class BudBabelRefresh extends Extension {
/**
* {@link Extension.register}
*/
@bind
public override async register(bud: Bud) {
this.logger.log(`Registering react-refresh-babel transformer`)

import BudBabelRefresh from './extension.js'
export default BudBabelRefresh
bud.babel.setPlugin(
`react-refresh/babel`,
await this.resolve(`react-refresh/babel`, import.meta.url),
)
}
}
92 changes: 0 additions & 92 deletions sources/@roots/bud-react/src/extension/extension.ts

This file was deleted.

95 changes: 93 additions & 2 deletions sources/@roots/bud-react/src/extension/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,93 @@
import BudReact from './extension.js'
export default BudReact
import type {Bud} from '@roots/bud-framework'

import {Extension} from '@roots/bud-framework/extension'
import {
bind,
dependsOn,
expose,
label,
} from '@roots/bud-framework/extension/decorators'
import merge from '@roots/bud-support/lodash/merge'

import type BudReactRefresh from '../react-refresh/index.js'

/**
* React configuration
*/
@label(`@roots/bud-react`)
@dependsOn([`@roots/bud-react/react-refresh`])
@expose(`react`)
export default class BudReact extends Extension {
/**
* {@link Extension.configAfter}
*
*/
@bind
public override async boot(bud: Bud) {
if (
![this.useSWC, this.useTypeScript, this.useBabel].some(
t => t === true,
)
) {
this.logger.warn(`No supported compiler found.`)
}

if (this.useSWC) {
bud.swc.setJsc(
merge(bud.swc.jsc, {transform: {react: {runtime: `automatic`}}}),
)
}

if (this.useBabel) {
const babelPluginUrl = await this.resolve(`@babel/preset-react`, import.meta.url).catch(bud.catch)
this.app.babel.setPreset(
`@babel/preset-react`,
babelPluginUrl,
)
}
}

/**
* Accessor for `@roots/bud-react/react-refresh`
*
* @readonly
*/
public get refresh(): BudReactRefresh {
return this.app.extensions.get(`@roots/bud-react/react-refresh`)
}

/**
* Use babel
*
* @readonly
*/
public get useBabel(): boolean {
if (this.useTypeScript) return false
if (this.useSWC) return false
return this.app.extensions.has(`@roots/bud-babel`)
}

/**
* Use SWC
*
* @readonly
*/
public get useSWC(): boolean {
return this.app.extensions.has(`@roots/bud-swc`)
}

/**
* Use TypeScript
*
* @readonly
*/
public get useTypeScript(): boolean {
if (this.useSWC) return false

if (this.app.extensions.has(`@roots/bud-typescript`)) {
return !this.app.extensions.get(`@roots/bud-typescript`).get(`babel`)
}

return false
}
}
37 changes: 35 additions & 2 deletions sources/@roots/bud-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,40 @@
* @see https://github.com/roots/bud
*/

import BudReact from './extension/index.js'
import './types.js'
import type {
Extension,
PublicExtensionApi,
} from '@roots/bud-framework/extension'
import type BudBabelRefresh from '@roots/bud-react/babel-refresh'
import type BudReactRefresh from '@roots/bud-react/react-refresh'
import type BudSWCRefresh from '@roots/bud-react/swc-refresh'
import type BudTypeScriptRefresh from '@roots/bud-react/typescript-refresh'

import BudReact from '@roots/bud-react/extension'

interface PublicBudReactRefresh extends PublicExtensionApi {
setTransformExtension(extension: Extension): void
}

interface PublicBudReact extends PublicExtensionApi {
refresh: PublicBudReactRefresh
useBabel: boolean
useSWC: boolean
useTypeScript: boolean
}

declare module '@roots/bud-framework' {
interface Bud {
react: PublicBudReact
}

interface Modules {
'@roots/bud-react': BudReact
'@roots/bud-react/babel-refresh': BudBabelRefresh
'@roots/bud-react/react-refresh': BudReactRefresh
'@roots/bud-react/swc-refresh': BudSWCRefresh
'@roots/bud-react/typescript-refresh': BudTypeScriptRefresh
}
}

export default BudReact
Loading
Loading