Skip to content

Commit 852d4bc

Browse files
committed
perf(tools): minify css in production and bundles
fixes html minifier
1 parent cd6ca2f commit 852d4bc

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

package-lock.json

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"build:types": "tsc -b",
3434
"build:docs": "run-s build:docs:*",
3535
"build:docs:eleventy": "eleventy --config=.eleventy.cjs",
36+
"bundle": "node --input-type module -e \"await import('./tools/pfe-tools/esbuild.js').then(x => x.singleFileBuild())\"",
3637
"🧑‍🔬-----TEST-------🧑‍🔬": "❓ Test packages",
3738
"test": "wtr --group default",
3839
"🧑‍🔬--TEST---ALL FRAMEWORKS--🧑‍🔬": "❓ Test packages - all frameworks",

tools/pfe-tools/esbuild.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Meta as LitCSSModuleMeta } from '@pwrs/lit-css';
44
import esbuild from 'esbuild';
55
import glob from 'glob';
66
import Sass from 'sass';
7+
import CleanCSS from 'clean-css';
78

89
import { externalSubComponents } from './esbuild-plugins/external-sub-components.js';
910
import { 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: /.scss$/, transform: transformSass }),
73+
litCssPlugin({ filter: /\.scss$/, 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

tools/pfe-tools/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@
8989
"api-viewer-element": "^0.6.4",
9090
"cem-plugin-module-file-extensions": "^0.0.2",
9191
"cem-plugin-readonly": "^0.0.2",
92+
"clean-css": "^4.2.1",
9293
"compression": "^1.7.4",
9394
"dedent": "^0.7.0",
9495
"dotenv": "^16.0.0",
9596
"eleventy-plugin-toc": "^1.1.5",
9697
"esbuild": "^0.14.24",
9798
"esbuild-node-externals": "^1.4.1",
9899
"esbuild-plugin-lit-css": "^1.2.3",
99-
"esbuild-plugin-minify-html-literals": "^1.0.0",
100+
"esbuild-plugin-minify-html-literals": "^1.0.1",
100101
"eslint": "^8.10.0",
101102
"glob": "^7.2.0",
102103
"html-include-element": "^0.2.0",

0 commit comments

Comments
 (0)