22/**
33 * @type {import('postcss').PluginCreator }
44 */
5- import { buildCss , generateAttributesFile , isZeroUiInitialized } from './helpers' ;
6- import { runZeroUiInit } from '../cli/postInstall.js' ;
7- import { processVariants } from './ast-parsing' ;
8- import { CONFIG } from '../config' ;
9- import { formatError , registerDeps , Result } from './utilities.js' ;
10-
115type Root = { prepend : ( css : string ) => void } ;
6+ type Result = {
7+ messages : { type : string ; plugin : string ; file : string ; parent : string } [ ] ;
8+ opts : { from : string } ;
9+ prepend : ( css : string ) => void ;
10+ warn : ( message : string , options ?: { endIndex ?: number ; index ?: number ; node ?: Node ; plugin ?: string ; word ?: string } ) => void ;
11+ } ;
12+ type RuntimeModules = {
13+ buildCss : typeof import ( './helpers.js' ) . buildCss ;
14+ generateAttributesFile : typeof import ( './helpers.js' ) . generateAttributesFile ;
15+ isZeroUiInitialized : typeof import ( './helpers.js' ) . isZeroUiInitialized ;
16+ processVariants : typeof import ( './ast-parsing.js' ) . processVariants ;
17+ formatError : typeof import ( './utilities.js' ) . formatError ;
18+ registerDeps : typeof import ( './utilities.js' ) . registerDeps ;
19+ } ;
20+
21+ const zeroUIPlugin = 'postcss-react-zero-ui' ;
22+ const warnedCwds = new Set < string > ( ) ;
23+ let runtimeModulesPromise : Promise < RuntimeModules > | null = null ;
24+
25+ function loadRuntimeModules ( ) : Promise < RuntimeModules > {
26+ if ( ! runtimeModulesPromise ) {
27+ runtimeModulesPromise = Promise . all ( [ import ( './helpers.js' ) , import ( './ast-parsing.js' ) , import ( './utilities.js' ) ] ) . then (
28+ ( [ helpers , astParsing , utilities ] ) => ( {
29+ buildCss : helpers . buildCss ,
30+ generateAttributesFile : helpers . generateAttributesFile ,
31+ isZeroUiInitialized : helpers . isZeroUiInitialized ,
32+ processVariants : astParsing . processVariants ,
33+ formatError : utilities . formatError ,
34+ registerDeps : utilities . registerDeps ,
35+ } )
36+ ) ;
37+ }
1238
13- const zeroUIPlugin = CONFIG . PLUGIN_NAME ;
39+ return runtimeModulesPromise ;
40+ }
41+
42+ function warnIfNotInitialized ( result : Result , isZeroUiInitialized : RuntimeModules [ 'isZeroUiInitialized' ] ) {
43+ const cwd = process . cwd ( ) ;
44+
45+ if ( isZeroUiInitialized ( ) || warnedCwds . has ( cwd ) ) {
46+ return ;
47+ }
48+
49+ warnedCwds . add ( cwd ) ;
50+ result . warn ( '[Zero-UI] Zero UI is not initialized. Run `react-zero-ui` to patch your project config.' , { plugin : zeroUIPlugin } ) ;
51+ }
1452
1553const plugin = ( ) => {
1654 return {
1755 postcssPlugin : zeroUIPlugin ,
1856 async Once ( root : Root , { result } : { result : Result } ) {
1957 try {
58+ const { buildCss, generateAttributesFile, isZeroUiInitialized, processVariants, formatError, registerDeps } = await loadRuntimeModules ( ) ;
2059 const { finalVariants, initialGlobalValues, sourceFiles } = await processVariants ( ) ;
2160
2261 const cssBlock = buildCss ( finalVariants ) ;
@@ -25,13 +64,10 @@ const plugin = () => {
2564 /* ── register file-dependencies for HMR ─────────────────── */
2665 registerDeps ( result , zeroUIPlugin , sourceFiles , result . opts . from ?? '' ) ;
2766
28- /* ── first-run bootstrap ────────────────────────────────── */
29- if ( ! isZeroUiInitialized ( ) ) {
30- console . log ( '[Zero-UI] Auto-initializing (first-time setup)…' ) ;
31- await runZeroUiInit ( ) ;
32- }
67+ warnIfNotInitialized ( result , isZeroUiInitialized ) ;
3368 await generateAttributesFile ( finalVariants , initialGlobalValues ) ;
3469 } catch ( err : unknown ) {
70+ const { formatError, registerDeps } = await loadRuntimeModules ( ) ;
3571 const { friendly, loc } = formatError ( err ) ;
3672 if ( process . env . NODE_ENV !== 'production' ) {
3773 if ( loc ?. file ) registerDeps ( result , zeroUIPlugin , [ loc . file ] , result . opts . from ?? '' ) ;
0 commit comments