Skip to content

Commit

Permalink
chore: add config changes in related hooks (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer authored Jan 24, 2024
1 parent 77cab9f commit 3be58c7
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 101 deletions.
12 changes: 4 additions & 8 deletions packages/config/src/diff-config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { dashCase } from '@pandacss/shared'
import type { ArtifactId, Config } from '@pandacss/types'
import microdiff, { type Difference } from 'microdiff'
import type { ArtifactId, Config, DiffConfigResult } from '@pandacss/types'
import microdiff from 'microdiff'
import { artifactMatchers } from './config-deps'

export interface DiffConfigResult {
hasConfigChanged: boolean
artifacts: Set<ArtifactId>
diffs: Difference[]
}

type ConfigOrFn = Config | (() => Config)
const runIfFn = (fn: ConfigOrFn): Config => (typeof fn === 'function' ? fn() : fn)

export { type DiffConfigResult }

/**
* Diff the two config objects and return the list of affected properties
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Builder {
// config change
if (this.affecteds.hasConfigChanged) {
logger.debug('builder', '⚙️ Config changed, reloading')
await ctx.hooks['config:change']?.({ config: ctx.config })
await ctx.hooks['config:change']?.({ config: ctx.config, changes: this.affecteds })
return
}

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function codegen(ctx: PandaContext, ids?: ArtifactId[]) {
const promises = ctx.getArtifacts(ids).map((artifact) => limit(() => ctx.output.write(artifact)))
await Promise.allSettled(promises)

await ctx.hooks['codegen:done']?.()
await ctx.hooks['codegen:done']?.({ changed: ids })

return {
box: createBox({
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function generate(config: Config, configPath?: string) {
}

logger.info('config:change', 'Config changed, restarting...')
await ctx.hooks['config:change']?.({ config: ctx.config })
await ctx.hooks['config:change']?.({ config: ctx.config, changes: affecteds })
return build(ctx, Array.from(affecteds.artifacts))
})

Expand Down
1 change: 1 addition & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"@pandacss/extractor": "workspace:*",
"csstype": "3.1.3",
"microdiff": "^1.3.2",
"ncp": "^2.0.0",
"pkg-types": "1.0.3"
}
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/artifact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type Difference } from 'microdiff'
import type { Nullable } from './shared'

export interface ArtifactContent {
Expand Down Expand Up @@ -49,3 +50,9 @@ export interface ArtifactFilters {
ids?: ArtifactId[]
affecteds?: AffectedArtifacts
}

export interface DiffConfigResult {
hasConfigChanged: boolean
artifacts: Set<ArtifactId>
diffs: Difference[]
}
7 changes: 4 additions & 3 deletions packages/types/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HooksApiInterface } from './hooks-api'
import type { ArtifactId, DiffConfigResult } from './artifact'
import type { LoadConfigResult, UserConfig } from './config'
import type { HooksApiInterface } from './hooks-api'
import type { ParserResultInterface } from './parser'

type MaybeAsyncReturn<T = void> = Promise<T> | T
Expand All @@ -17,7 +18,7 @@ export interface PandaHooks {
/**
* Called when the config file or one of its dependencies (imports) has changed.
*/
'config:change': (args: { config: UserConfig }) => MaybeAsyncReturn
'config:change': (args: { config: UserConfig; changes: DiffConfigResult }) => MaybeAsyncReturn
/**
* Called after reading the file content but before parsing it.
* You can use this hook to transform the file content to a tsx-friendly syntax so that Panda's parser can parse it.
Expand All @@ -32,7 +33,7 @@ export interface PandaHooks {
/**
* Called after the codegen is completed
*/
'codegen:done': () => MaybeAsyncReturn
'codegen:done': (args: { changed: ArtifactId[] | undefined }) => MaybeAsyncReturn
/**
* Called right before adding the design-system CSS (global, static, preflight, tokens, keyframes) to the final CSS
* Called right before writing/injecting the final CSS (styles.css) that contains the design-system CSS and the parser CSS
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type * from './hooks-api'
export type * from './analyze-report'
export type * from './artifact'
export type * from './composition'
export type * from './conditions'
export type * from './config'
export type * from './hooks'
export type * from './hooks-api'
export type * from './parser'
export type * from './parts'
export type * from './pattern'
Expand Down
Loading

2 comments on commit 3be58c7

@vercel
Copy link

@vercel vercel bot commented on 3be58c7 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

panda-docs – ./website

panda-css.com
panda-docs.vercel.app
panda-docs-chakra-ui.vercel.app
panda-docs-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 3be58c7 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.