1+ import { cartesianProduct } from '@noya-app/noya-utils' ;
12import { path } from 'imfs' ;
23import { loadDesignSystem } from 'noya-module-loader' ;
34import { clean } from './clean' ;
45import { sortFiles } from './common' ;
56import { compileDesignSystem } from './compileDesignSystem' ;
7+ import { reduceIterator , reduceIteratorChunked } from './processWork' ;
68import { CompilerConfiguration } from './types' ;
79import { sanitizePackageName } from './validate' ;
810
@@ -33,78 +35,120 @@ const colorNames = [
3335
3436const colorModes = [ 'light' as const , 'dark' as const ] ;
3537
36- export function compile ( configuration : CompilerConfiguration ) {
38+ export function compilePermutation (
39+ configuration : CompilerConfiguration ,
40+ {
41+ colorMode,
42+ colorName,
43+ libraryName,
44+ } : { colorMode : 'light' | 'dark' ; colorName : string ; libraryName : string } ,
45+ ) {
46+ const allDSFiles : Record < string , string > = { } ;
47+
48+ const {
49+ files : dsFiles ,
50+ dependencies,
51+ devDependencies,
52+ allExportMap,
53+ } = compileDesignSystem ( {
54+ ...configuration ,
55+ ds : {
56+ ...configuration . ds ,
57+ config : {
58+ ...configuration . ds . config ,
59+ colorMode,
60+ colors : {
61+ ...configuration . ds . config . colors ,
62+ primary : colorName ,
63+ } ,
64+ } ,
65+ } ,
66+ designSystemDefinition : configuration . resolvedDefinitions [ libraryName ] ,
67+ includeTailwindBase : libraryName === 'vanilla' ,
68+ spreadTheme : libraryName . endsWith ( 'radix' ) ,
69+ exportTypes :
70+ libraryName === 'vanilla'
71+ ? [
72+ 'html-css' ,
73+ 'html-tailwind' ,
74+ 'react-css' ,
75+ 'react-css-modules' ,
76+ 'react-tailwind' ,
77+ ]
78+ : libraryName . endsWith ( 'chakra' ) || libraryName . endsWith ( 'radix' )
79+ ? [ 'react' ]
80+ : [ 'react-css' , 'react-tailwind' ] ,
81+ } ) ;
82+
83+ const basename = path . basename ( libraryName ) ;
84+
85+ Object . assign (
86+ allDSFiles ,
87+ addPathPrefix (
88+ dsFiles ,
89+ `src/app/${ basename } /${ colorName } /${ colorMode } /components/` ,
90+ ) ,
91+ ) ;
92+
93+ for ( const [ componentName , exportMap ] of Object . entries ( allExportMap ) ) {
94+ for ( const [ exportType , files ] of Object . entries ( exportMap ) ) {
95+ Object . assign (
96+ allDSFiles ,
97+ addPathPrefix (
98+ files ,
99+ `public/${ basename } /${ colorName } /${ colorMode } /components/${ componentName } /${ exportType } /` ,
100+ ) ,
101+ ) ;
102+ }
103+ }
104+
105+ return {
106+ files : allDSFiles ,
107+ dependencies,
108+ devDependencies,
109+ } ;
110+ }
111+
112+ type ProgressCallback = ( progress : {
113+ current : number ;
114+ total : number ;
115+ fileCount : number ;
116+ } ) => void ;
117+
118+ export function * compileGenerator (
119+ configuration : CompilerConfiguration ,
120+ onProgress ?: ProgressCallback ,
121+ ) {
37122 const allDefinitions = Object . keys ( configuration . resolvedDefinitions ) ;
38123
39124 const allDSFiles : Record < string , string > = { } ;
40125 const allDependencies : Record < string , string > = { } ;
41126 const allDevDependencies : Record < string , string > = { } ;
42127
43- for ( const libraryName of allDefinitions ) {
44- for ( const colorMode of colorModes ) {
45- for ( const colorName of colorNames ) {
46- const {
47- files : dsFiles ,
48- dependencies,
49- devDependencies,
50- allExportMap,
51- } = compileDesignSystem ( {
52- ...configuration ,
53- ds : {
54- ...configuration . ds ,
55- config : {
56- ...configuration . ds . config ,
57- colorMode,
58- colors : {
59- ...configuration . ds . config . colors ,
60- primary : colorName ,
61- } ,
62- } ,
63- } ,
64- designSystemDefinition :
65- configuration . resolvedDefinitions [ libraryName ] ,
66- includeTailwindBase : libraryName === 'vanilla' ,
67- spreadTheme : libraryName . endsWith ( 'radix' ) ,
68- exportTypes :
69- libraryName === 'vanilla'
70- ? [
71- 'html-css' ,
72- 'html-tailwind' ,
73- 'react-css' ,
74- 'react-css-modules' ,
75- 'react-tailwind' ,
76- ]
77- : libraryName . endsWith ( 'chakra' ) || libraryName . endsWith ( 'radix' )
78- ? [ 'react' ]
79- : [ 'react-css' , 'react-tailwind' ] ,
80- } ) ;
81-
82- Object . assign ( allDependencies , dependencies ) ;
83- Object . assign ( allDevDependencies , devDependencies ) ;
84-
85- const basename = path . basename ( libraryName ) ;
86-
87- Object . assign (
88- allDSFiles ,
89- addPathPrefix (
90- dsFiles ,
91- `src/app/${ basename } /${ colorName } /${ colorMode } /components/` ,
92- ) ,
93- ) ;
94-
95- for ( const [ componentName , exportMap ] of Object . entries ( allExportMap ) ) {
96- for ( const [ exportType , files ] of Object . entries ( exportMap ) ) {
97- Object . assign (
98- allDSFiles ,
99- addPathPrefix (
100- files ,
101- `public/${ basename } /${ colorName } /${ colorMode } /components/${ componentName } /${ exportType } /` ,
102- ) ,
103- ) ;
104- }
105- }
106- }
107- }
128+ const allPermutations = cartesianProduct (
129+ allDefinitions ,
130+ colorModes ,
131+ colorNames ,
132+ ) ;
133+
134+ let fileCount = 0 ;
135+
136+ for ( let i = 0 ; i < allPermutations . length ; i ++ ) {
137+ const [ libraryName , colorMode , colorName ] = allPermutations [ i ] ;
138+ const { files, dependencies, devDependencies } = compilePermutation (
139+ configuration ,
140+ { libraryName, colorMode, colorName } ,
141+ ) ;
142+
143+ fileCount += Object . keys ( files ) . length ;
144+
145+ Object . assign ( allDSFiles , files ) ;
146+ Object . assign ( allDependencies , dependencies ) ;
147+ Object . assign ( allDevDependencies , devDependencies ) ;
148+
149+ onProgress ?.( { current : i + 1 , total : allPermutations . length , fileCount } ) ;
150+
151+ yield allDSFiles ;
108152 }
109153
110154 const files = {
@@ -182,10 +226,21 @@ export default config
182226 return sortFiles ( files ) ;
183227}
184228
229+ export function compile (
230+ configuration : CompilerConfiguration ,
231+ ) : Record < string , string > {
232+ return reduceIterator (
233+ compileGenerator ( configuration ) ,
234+ ( previous , current ) => current ,
235+ { } ,
236+ ) ;
237+ }
238+
185239export async function compileAsync (
186240 configuration : Omit < CompilerConfiguration , 'resolvedDefinitions' > & {
187241 definitions : string [ ] ;
188242 } ,
243+ onProgress ?: ProgressCallback ,
189244) {
190245 const resolvedDefinitions = Object . fromEntries (
191246 await Promise . all (
@@ -196,10 +251,14 @@ export async function compileAsync(
196251 ) ,
197252 ) ;
198253
199- return compile ( {
200- ...configuration ,
201- resolvedDefinitions,
202- } ) ;
254+ const resolvedConfiguration = { ...configuration , resolvedDefinitions } ;
255+
256+ return await reduceIteratorChunked (
257+ compileGenerator ( resolvedConfiguration , onProgress ) ,
258+ 1000 ,
259+ ( previous , current ) => current ,
260+ { } ,
261+ ) ;
203262}
204263
205264function addPathPrefix (
0 commit comments