@@ -15,9 +15,7 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
1515import { readdirSync } from 'fs' ;
1616import { resolve , join , dirname } from 'path' ;
1717import { fileURLToPath } from 'url' ;
18- import { mkdtemp , readdir , writeFile } from 'fs/promises' ;
19-
20- import os from 'os' ;
18+ import { mkdtemp , readdir , readFile , stat , writeFile } from 'fs/promises' ;
2119
2220export interface PfeEsbuildOptions {
2321 /** Extra esbuild plugins */
@@ -39,6 +37,8 @@ export interface PfeEsbuildOptions {
3937}
4038
4139export interface PfeEsbuildSingleFileOptions {
40+ /** list of NPM package names to bundle alongside the repo's components */
41+ additionalPackages ?: string [ ] ;
4242 outfile ?: string ;
4343 plugins ?: Plugin [ ] ;
4444 minify ?: boolean ;
@@ -66,6 +66,8 @@ const ALWAYS_EXCLUDE = [
6666 'pfe-styles' ,
6767] ;
6868
69+ const exists = ( filePath : string ) => stat ( filePath ) . then ( ( ) => true , ( ) => false ) ;
70+
6971/** lit-css transform plugin to process `.scss` files on-the-fly */
7072export async function transformSass (
7173 source : string ,
@@ -106,36 +108,56 @@ export function getBasePlugins({ minify }: { minify?: boolean } = {}) {
106108}
107109
108110/** Generate a temporary file containing namespace exports of all pfe components */
109- export async function componentsEntryPoint ( options ?: { prefix : string } ) {
110- const componentDirs = await readdir ( join ( REPO_ROOT , 'elements' ) ) ;
111+ export async function componentsEntryPoint ( options ?: { additionalPackages ? : string [ ] } ) {
112+ const componentDirs = await readdir ( join ( REPO_ROOT , 'elements' ) ) . catch ( ( ) => [ ] as string [ ] ) ;
111113 const cacheKey = componentDirs . join ( '--' ) ;
114+
115+ const additionalImports =
116+ options ?. additionalPackages
117+ ?. reduce ( ( acc , specifier ) => `${ acc } \nexport * from '${ specifier } ';` , '' ) ?? '' ;
118+
119+ if ( ! cacheKey ) {
120+ return additionalImports ;
121+ }
122+
112123 if ( ! COMPONENT_ENTRYPOINTS_CACHE . get ( cacheKey ) ) {
113124 try {
114- const outdir =
115- options ?. prefix ? join ( REPO_ROOT , options ?. prefix )
116- : await mkdtemp ( join ( os . tmpdir ( ) , 'pfe' ) ) ;
117- const tmpfile = join ( outdir , 'components.ts' ) ;
118- const imports = await Promise . all ( componentDirs . reduce ( ( acc , dir ) =>
119- `${ acc } \nexport * from '@patternfly/${ dir } ';` , '' ) ) ;
120- await writeFile ( tmpfile , imports , 'utf8' ) ;
121- COMPONENT_ENTRYPOINTS_CACHE . set ( cacheKey , tmpfile ) ;
122- return tmpfile ;
125+ const imports = await componentDirs . reduce ( async ( last , dir ) => {
126+ const acc = await last ;
127+ const elementPath = join ( REPO_ROOT , 'elements' , dir ) ;
128+ const packageJsonPath = join ( elementPath , 'package.json' ) ;
129+ const isPackage = await exists ( packageJsonPath ) ;
130+ if ( isPackage ) {
131+ const { name } = JSON . parse ( await readFile ( packageJsonPath , 'utf8' ) ) ;
132+ return `${ acc } \nexport * from '${ name } ';` ;
133+ } else {
134+ return `${ acc } \nexport * from '${ elementPath . replace ( REPO_ROOT , './' ) } /${ dir } .js';` ;
135+ }
136+ } , Promise . resolve ( additionalImports ) ) ;
137+
138+ COMPONENT_ENTRYPOINTS_CACHE . set ( cacheKey , imports ) ;
139+ return imports ;
123140 } catch ( error ) {
124141 console . error ( error ) ;
125142 }
126143 }
144+
127145 return COMPONENT_ENTRYPOINTS_CACHE . get ( cacheKey ) ;
128146}
129147
130148/** Create a single-file production bundle of all elements */
131149export async function singleFileBuild ( options ?: PfeEsbuildSingleFileOptions ) {
132150 try {
133- const prefix = fileURLToPath ( new URL ( './demo' , import . meta. url ) ) . replace ( REPO_ROOT , '' ) ;
134151 const result = await esbuild . build ( {
135152 absWorkingDir : REPO_ROOT ,
136153 allowOverwrite : true ,
137154 bundle : true ,
138- entryPoints : [ await componentsEntryPoint ( { prefix } ) ] ,
155+ stdin : {
156+ contents : await componentsEntryPoint ( { additionalPackages : options ?. additionalPackages } ) ,
157+ resolveDir : REPO_ROOT ,
158+ sourcefile : 'components.ts' ,
159+ loader : 'ts' ,
160+ } ,
139161 format : 'esm' ,
140162 keepNames : true ,
141163 legalComments : 'linked' ,
0 commit comments