11import type { Plugin } from '@web/dev-server-core' ;
2- import type { LitCSSOptions } from 'esbuild-plugin- lit-css' ;
2+ import type { Meta as LitCSSModuleMeta } from '@pwrs/ lit-css' ;
33
44import esbuild from 'esbuild' ;
55import glob from 'glob' ;
@@ -11,15 +11,16 @@ import { litCssPlugin } from 'esbuild-plugin-lit-css';
1111
1212import { nodeExternalsPlugin } from 'esbuild-node-externals' ;
1313import { readdirSync } from 'fs' ;
14- import { resolve , join } from 'path' ;
15-
16- type Transformer = LitCSSOptions [ 'transform' ] ;
14+ import { resolve , join , dirname } from 'path' ;
15+ import { fileURLToPath } from 'url' ;
1716
1817export interface PfeEsbuildOptions {
1918 /** Extra esbuild plugins */
2019 plugins ?: Plugin [ ] ;
20+ /** Whether to bundle the components and all their dependencies */
21+ bundle ?: boolean ;
2122 /** repository root (default: process.cwd()) */
22- cwd ?: string
23+ cwd ?: string ;
2324 /** exclude these directories (under the workspace) from the build */
2425 entryPointFilesExcludes ?: string [ ] ;
2526 /** packages to include */
@@ -28,10 +29,24 @@ export interface PfeEsbuildOptions {
2829 workspace ?: string ;
2930 /** production bundles are minified */
3031 mode ?: 'development' | 'production' ;
32+ /** file to bundle to */
33+ outfile ?: string ;
3134 /** Packages to treat as external, i.e. not to bundle */
3235 external : string [ ] ;
3336}
3437
38+ /** lit-css transform plugin to process `.scss` files on-the-fly */
39+ export function transformSass ( source : string , { filePath } : LitCSSModuleMeta ) : string {
40+ const result = Sass . compileString ( source , {
41+ loadPaths : [ dirname ( filePath ) , NODE_MODULES ] ,
42+ } ) ;
43+ // TODO: forward sourcemaps by returning an object
44+ return result . css ;
45+ }
46+
47+ /** abs-path to root node_modules */
48+ const NODE_MODULES = fileURLToPath ( new URL ( '../../node_modules' , import . meta. url ) ) ;
49+
3550/**
3651 * Exclude SASS-only packages because there are no ts sources there
3752 */
@@ -79,36 +94,41 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
7994 const packagePath = packageDirs . flatMap ( dir =>
8095 glob . sync ( `${ workspace } /${ dir } /package.json` , { absolute : true , cwd } ) ) ;
8196
82- const transform : Transformer = ( data , { filePath } ) => Sass . renderSync ( {
83- data,
84- file : filePath ,
85- includePaths : [
86- join ( options ?. cwd ?? process . cwd ( ) , 'node_modules' ) ,
87- ] ,
88- } ) . css . toString ( ) ;
89-
9097 try {
9198 const result = await esbuild . build ( {
9299 entryPoints,
93100 entryNames,
94101 absWorkingDir : cwd ,
95- outdir : workspace ,
96102 format : 'esm' ,
97103 allowOverwrite : true ,
98- bundle : true ,
99- external : [ '@patternfly*' , 'lit' , 'tslib' , ...options ?. external ?? [ ] ] ,
100104 treeShaking : true ,
101105 legalComments : 'linked' ,
102106 watch : Boolean ( process . env . WATCH ) || false ,
103107 logLevel : 'info' ,
104108 sourcemap : true ,
109+ bundle : options ?. bundle ?? true ,
105110
106111 minify : mode === 'production' ,
107112 minifyWhitespace : mode === 'production' ,
108113
114+ ...options ?. outfile ? {
115+ outfile : options . outfile
116+ } : {
117+ outdir : workspace ,
118+ } ,
119+
120+ external : [
121+ ...options ?. bundle ? [ ] : [
122+ '@patternfly*' ,
123+ 'lit' ,
124+ 'tslib' ,
125+ ] ,
126+ ...options ?. external ?? [ ]
127+ ] ,
128+
109129 plugins : [
110130 // import scss files as LitElement CSSResult objects
111- litCssPlugin ( { filter : / .s c s s $ / , transform } ) ,
131+ litCssPlugin ( { filter : / .s c s s $ / , transform : transformSass } ) ,
112132 // replace `{{version}}` with each package's version
113133 packageVersion ( ) ,
114134 // ignore sub components bundling like "pfe-progress-steps-item"
@@ -118,6 +138,7 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
118138 ] ,
119139 } ) ;
120140 result . stop ?.( ) ;
141+ return result . outputFiles ?. map ( x => x . path ) ?? [ ] ;
121142 } catch ( error ) {
122143 console . log ( error ) ;
123144 process . exit ( 1 ) ;
0 commit comments