@@ -4,6 +4,7 @@ import type { Meta as LitCSSModuleMeta } from '@pwrs/lit-css';
44import esbuild from 'esbuild' ;
55import glob from 'glob' ;
66import Sass from 'sass' ;
7+ import CleanCSS from 'clean-css' ;
78
89import { externalSubComponents } from './esbuild-plugins/external-sub-components.js' ;
910import { packageVersion } from './esbuild-plugins/package-version.js' ;
@@ -38,13 +39,22 @@ export interface PfeEsbuildSingleFileOptions {
3839 outfile ?: string ;
3940}
4041
42+ const cleanCSS = new CleanCSS ( {
43+ sourceMap : true ,
44+ returnPromise : true ,
45+ } ) ;
46+
4147/** lit-css transform plugin to process `.scss` files on-the-fly */
42- export function transformSass ( source : string , { filePath } : LitCSSModuleMeta ) : string {
43- const result = Sass . compileString ( source , {
44- loadPaths : [ dirname ( filePath ) , NODE_MODULES ] ,
45- } ) ;
46- // TODO: forward sourcemaps by returning an object
47- return result . css ;
48+ export async function transformSass ( source : string , { filePath, minify } : LitCSSModuleMeta & { minify ?: boolean } ) : Promise < string > {
49+ const loadPaths = [ dirname ( filePath ) , NODE_MODULES ] ;
50+ const result = Sass . compileString ( source , { loadPaths } ) ;
51+ // TODO: forward sourcemaps by returning an object, would probably need a PR to lit-css
52+ if ( ! minify ) {
53+ return result . css ;
54+ } else {
55+ const { styles } = await cleanCSS . minify ( result . css /* , result.sourceMap */ ) ;
56+ return styles ;
57+ }
4858}
4959
5060/** abs-path to root node_modules */
@@ -58,13 +68,15 @@ const ALWAYS_EXCLUDE = [
5868 'pfe-styles' ,
5969] ;
6070
61- const basePlugins = ( ) => [
71+ const basePlugins = ( { minify } : { minify ?: boolean } = { } ) => [
6272 // import scss files as LitElement CSSResult objects
63- litCssPlugin ( { filter : / .s c s s $ / , transform : transformSass } ) ,
73+ litCssPlugin ( { filter : / \. s c s s $ / , transform : ( source , { filePath } ) => transformSass ( source , { filePath, minify } ) } ) ,
74+ ...! minify ? [ ] : [
75+ // minify lit-html templates
76+ minifyHTMLLiteralsPlugin ( ) ,
77+ ] ,
6478 // replace `{{version}}` with each package's version
6579 packageVersion ( ) ,
66- // minify lit-html templates
67- minifyHTMLLiteralsPlugin ( ) ,
6880] ;
6981
7082/** Create a single-file production bundle of all element */
@@ -75,8 +87,9 @@ export async function singleFileBuild(options?: PfeEsbuildSingleFileOptions) {
7587 absWorkingDir : cwd ,
7688 allowOverwrite : true ,
7789 bundle : true ,
78- entryPoints : [ join ( cwd , 'docs' , 'demo' , 'bundle .ts' ) ] ,
90+ entryPoints : [ join ( cwd , 'docs' , 'demo' , 'components .ts' ) ] ,
7991 format : 'esm' ,
92+ keepNames : true ,
8093 legalComments : 'linked' ,
8194 logLevel : 'info' ,
8295 minify : true ,
@@ -86,14 +99,12 @@ export async function singleFileBuild(options?: PfeEsbuildSingleFileOptions) {
8699 treeShaking : true ,
87100 watch : false ,
88101 plugins : [
89- ...basePlugins ( ) ,
102+ ...basePlugins ( { minify : true } ) ,
90103 ] ,
91104 } ) ;
92- console . log ( result ) ;
93105 result . stop ?.( ) ;
94106 return result . outputFiles ?. map ( x => x . path ) ?? [ ] ;
95- } catch ( error ) {
96- console . log ( error ) ;
107+ } catch {
97108 process . exit ( 1 ) ;
98109 }
99110}
@@ -166,7 +177,7 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
166177 ] ,
167178
168179 plugins : [
169- ...basePlugins ( ) ,
180+ ...basePlugins ( { minify : mode === 'production' } ) ,
170181 // ignore sub components bundling like "pfe-progress-steps-item"
171182 externalSubComponents ( ) ,
172183 // don't bundle node_module dependencies
0 commit comments