@@ -44,14 +44,20 @@ export interface PfeEsbuildSingleFileOptions {
4444 minify ?: boolean ;
4545}
4646
47+ /** abs-path to repo root */
48+ const REPO_ROOT = fileURLToPath ( new URL ( '../..' , import . meta. url ) ) ;
49+
50+ /** abs-path to root node_modules */
51+ const NODE_MODULES = fileURLToPath ( new URL ( '../../node_modules' , import . meta. url ) ) ;
52+
53+ /** memoization cache for temporary component entrypoint files */
54+ const COMPONENT_ENTRYPOINTS_CACHE = new Map ( ) ;
55+
4756const cleanCSS = new CleanCSS ( {
4857 sourceMap : true ,
4958 returnPromise : true ,
5059} ) ;
5160
52- /** abs-path to root node_modules */
53- const NODE_MODULES = fileURLToPath ( new URL ( '../../node_modules' , import . meta. url ) ) ;
54-
5561/**
5662 * Exclude SASS-only packages because there are no ts sources there
5763 */
@@ -99,15 +105,11 @@ export function getBasePlugins({ minify }: { minify?: boolean } = {}) {
99105 ] ;
100106}
101107
102- const REPO_ROOT = fileURLToPath ( new URL ( '../..' , import . meta. url ) ) ;
103-
104- const cache = new Map ( ) ;
105-
106108/** Generate a temporary file containing namespace exports of all pfe components */
107109export async function componentsEntryPoint ( options ?: { prefix : string } ) {
108110 const componentDirs = await readdir ( join ( REPO_ROOT , 'elements' ) ) ;
109111 const cacheKey = componentDirs . join ( '--' ) ;
110- if ( ! cache . get ( cacheKey ) ) {
112+ if ( ! COMPONENT_ENTRYPOINTS_CACHE . get ( cacheKey ) ) {
111113 try {
112114 const outdir =
113115 options ?. prefix ? join ( REPO_ROOT , options ?. prefix )
@@ -116,20 +118,20 @@ export async function componentsEntryPoint(options?: { prefix: string }) {
116118 const imports = await Promise . all ( componentDirs . reduce ( ( acc , dir ) =>
117119 `${ acc } \nexport * from '@patternfly/${ dir } ';` , '' ) ) ;
118120 await writeFile ( tmpfile , imports , 'utf8' ) ;
119- cache . set ( cacheKey , tmpfile ) ;
121+ COMPONENT_ENTRYPOINTS_CACHE . set ( cacheKey , tmpfile ) ;
120122 return tmpfile ;
121123 } catch ( error ) {
122124 console . error ( error ) ;
123125 }
124126 }
125- return cache . get ( cacheKey ) ;
127+ return COMPONENT_ENTRYPOINTS_CACHE . get ( cacheKey ) ;
126128}
129+
127130/** Create a single-file production bundle of all elements */
128131export async function singleFileBuild ( options ?: PfeEsbuildSingleFileOptions ) {
129- const cwd = fileURLToPath ( new URL ( '../..' , import . meta. url ) ) ;
130132 try {
131133 const result = await esbuild . build ( {
132- absWorkingDir : cwd ,
134+ absWorkingDir : REPO_ROOT ,
133135 allowOverwrite : true ,
134136 bundle : true ,
135137 entryPoints : [ await componentsEntryPoint ( { prefix : 'docs/demo' } ) ] ,
@@ -164,6 +166,7 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
164166 const mode = options ?. mode ?? 'development' ;
165167 const cwd = options ?. cwd ?? process . cwd ( ) ;
166168
169+ /** List of dir names of all packages which should be included in the build */
167170 const packageDirs = (
168171 // includes specified as an array
169172 Array . isArray ( options ?. include ) ? options ?. include as Array < string >
@@ -187,10 +190,14 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
187190 ignore : [ '**/*.d.ts' , '**/*.spec.ts' ] ,
188191 } ) ) ;
189192
190- /** if we're only `--include`ing a single element, prefix the entryNames, so tot build into that element's package */
193+ /**
194+ * if we're only `--include`ing a single element, prefix the entryNames,
195+ * in order to build into that element's package
196+ */
191197 const entryNames =
192198 join ( ...options ?. include ?. length === 1 ? [ options . include [ 0 ] ] : [ ] , '[dir]' , '[name]' ) ;
193199
200+ /** list of paths to package.json files */
194201 const packagePath = packageDirs . flatMap ( dir =>
195202 glob . sync ( `${ workspace } /${ dir } /package.json` , { absolute : true , cwd } ) ) ;
196203
0 commit comments