Skip to content

Commit cfd010f

Browse files
committed
feat(tools): export singleFileBuild function
builds all components in a single file with no dependencies
1 parent 57289d6 commit cfd010f

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

.changeset/five-planets-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/pfe-tools": minor
3+
---
4+
5+
add `singleFileBuild` to esbuild helpers

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ project.conf.json
2020

2121
pfe.min.js
2222

23+
*.js.map
2324
**/*.LEGAL.txt
2425
*.tsbuildinfo
2526
test-results

scripts/bundle-release.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = async function({ github, workspace }) {
1212
const outfile = `${cwd}/pfe.min.js`;
1313
const mode = 'production';
1414

15-
await pfeBuild({ cwd, mode, outfile });
15+
await pfeBuild({ cwd, mode, outfile, bundle: true });
1616

1717
// list of published packages from changesets
1818
const publishedPackages = JSON.parse('${{ steps.changesets.outputs.publishedPackages }}');

tools/pfe-tools/esbuild.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ export interface PfeEsbuildOptions {
2929
workspace?: string;
3030
/** production bundles are minified */
3131
mode?: 'development'|'production';
32-
/** file to bundle to */
33-
outfile?: string;
3432
/** Packages to treat as external, i.e. not to bundle */
3533
external: string[];
3634
}
3735

36+
export interface PfeEsbuildSingleFileOptions {
37+
outfile?: string;
38+
}
39+
3840
/** lit-css transform plugin to process `.scss` files on-the-fly */
3941
export function transformSass(source: string, { filePath }: LitCSSModuleMeta): string {
4042
const result = Sass.compileString(source, {
@@ -55,6 +57,44 @@ const ALWAYS_EXCLUDE = [
5557
'pfe-styles',
5658
];
5759

60+
const basePlugins = () => [
61+
// import scss files as LitElement CSSResult objects
62+
litCssPlugin({ filter: /.scss$/, transform: transformSass }),
63+
// replace `{{version}}` with each package's version
64+
packageVersion(),
65+
];
66+
67+
/** Create a single-file production bundle of all element */
68+
export async function singleFileBuild(options?: PfeEsbuildSingleFileOptions) {
69+
const cwd = fileURLToPath(new URL('../..', import.meta.url));
70+
try {
71+
const result = await esbuild.build({
72+
absWorkingDir: cwd,
73+
allowOverwrite: true,
74+
bundle: true,
75+
entryPoints: [join(cwd, 'docs', 'demo', 'bundle.ts')],
76+
format: 'esm',
77+
legalComments: 'linked',
78+
logLevel: 'info',
79+
minify: true,
80+
minifyWhitespace: true,
81+
outfile: options?.outfile ?? 'pfe.min.js',
82+
sourcemap: true,
83+
treeShaking: true,
84+
watch: false,
85+
plugins: [
86+
...basePlugins(),
87+
],
88+
});
89+
console.log(result);
90+
result.stop?.();
91+
return result.outputFiles?.map(x => x.path) ?? [];
92+
} catch (error) {
93+
console.log(error);
94+
process.exit(1);
95+
}
96+
}
97+
5898
/**
5999
* Build all components in a monorepo
60100
*/
@@ -111,11 +151,7 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
111151
minify: mode === 'production',
112152
minifyWhitespace: mode === 'production',
113153

114-
...options?.outfile ? {
115-
outfile: options.outfile
116-
} : {
117-
outdir: workspace,
118-
},
154+
outdir: workspace,
119155

120156
external: [
121157
...options?.bundle ? [] : [
@@ -127,10 +163,7 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
127163
],
128164

129165
plugins: [
130-
// import scss files as LitElement CSSResult objects
131-
litCssPlugin({ filter: /.scss$/, transform: transformSass }),
132-
// replace `{{version}}` with each package's version
133-
packageVersion(),
166+
...basePlugins(),
134167
// ignore sub components bundling like "pfe-progress-steps-item"
135168
externalSubComponents(),
136169
// don't bundle node_module dependencies

0 commit comments

Comments
 (0)