Skip to content

Commit

Permalink
fix: hmr codegen and add log option
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Aug 3, 2023
1 parent 27e874e commit fe7b9fa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ async function initCtx() {
if (ctx) {
return ctx
}
ctx = await createContext()
ctx = await createContext({
log: true
})
return ctx
}

Expand Down
13 changes: 11 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import { getCreateContextDefaults } from '@/defaults'

export async function createContext(options?: ICreateContextOptions) {
const opt = defu(options, getCreateContextDefaults())
let pandaConfig: Awaited<ReturnType<typeof getPandacssConfig>>
try {
pandaConfig = await getPandacssConfig(opt.pandaConfig)
} catch {
throw new Error(
'Cannot find config file: panda.config.ts or panda.config.js/cjs/mjs. Did you forget to run `panda init`?'
)
}

const pandaConfig = await getPandacssConfig(opt.pandaConfig)
const outdir = pandaConfig.config.outdir
const projectRoot = dirname(pandaConfig.path)
async function codegen() {
Expand All @@ -37,7 +44,9 @@ export async function createContext(options?: ICreateContextOptions) {
'/helpers.mjs'
)}: inject escape function into helpers
`)
console.log(words.filter(Boolean).join('\n'))
if (opt.log) {
console.log(words.filter(Boolean).join('\n'))
}
}

async function rollback() {
Expand Down
3 changes: 2 additions & 1 deletion src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export function getCreateContextDefaults(): Required<ICreateContextOptions> {
return {
pandaConfig: {
cwd: process.cwd()
}
},
log: false
}
}

Expand Down
27 changes: 18 additions & 9 deletions src/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import _isPseudoPlugin from '@csstools/postcss-is-pseudo-class'
import { defu } from 'defu'
import type { IPostcssPluginOptions } from './types'
import { getPostcssPluginDefaults } from './defaults'
import { createContext } from './core/context'

function normalizeString(strs: string | string[]) {
if (Array.isArray(strs)) {
Expand Down Expand Up @@ -127,22 +128,30 @@ const postcssWeappPandacssEscapePlugin: PluginCreator<IPostcssPluginOptions> = (
const isPseudoPlugin = _isPseudoPlugin() as Plugin

return {
postcssPlugin: 'postcss-weapp-pandacss-escape-plugin',
prepare(result) {
const isPseudoRule = isPseudoPlugin.prepare?.(result).Rule
return {
postcssPlugin: 'postcss-weapp-pandacss-escape-wrapper-plugin',
plugins: [
async function () {
try {
const ctx = await createContext()
await ctx.codegen()
} catch (error) {
console.log((<Error>error).message)
}
},
cascadeLayersPlugin,
isPseudoPlugin,
{
postcssPlugin: 'postcss-weapp-pandacss-escape-plugin',
Declaration(decl) {
decl.prop = escape(decl.prop)
},
Rule(rule, helper) {
isPseudoRule?.(rule, helper)
Rule(rule) {
utilitiesTransformer.transformSync(rule, {
lossless: false,
updateSelector: true
})
},
OnceExit(root, helper) {
cascadeLayersPlugin.OnceExit?.(root, helper)
OnceExit(root) {
root.walkRules(/:not\(#\\#\)/, (rule) => {
atLayerTransformer.transformSync(rule, {
lossless: false,
Expand All @@ -151,7 +160,7 @@ const postcssWeappPandacssEscapePlugin: PluginCreator<IPostcssPluginOptions> = (
})
}
}
}
]
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type PandacssConfigFileOptions = Parameters<typeof loadConfigFile>[0]

export interface ICreateContextOptions {
pandaConfig?: Partial<PandacssConfigFileOptions>
log?: boolean
}

/**
Expand Down

0 comments on commit fe7b9fa

Please sign in to comment.