Skip to content

Commit a6253d3

Browse files
committed
feat(tools): generate single-file entrypoint at runtime
1 parent e1663d4 commit a6253d3

File tree

12 files changed

+175
-164
lines changed

12 files changed

+175
-164
lines changed

.changeset/yellow-dingos-heal.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@patternfly/create-element": patch
3+
"@patternfly/pfe-tools": patch
4+
---
5+
6+
Generate the single-file bundle entrypoint at runtime

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package-lock.json
2626
_site
2727
docs/_data/todos.json
2828
docs/demo.js
29+
docs/pfe.min.js
2930
docs/bundle.js
3031
docs/core
3132
docs/components

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ core/pfe-core/demo/*
128128
/docs/demo/**/*.js.map
129129
/docs/demo/demo.js.LEGAL.txt
130130
!/docs/demo/demo.css
131+
!/docs/demo/components.ts
131132
!/docs/core/styles/demo.njk
132133

133134

docs/_includes/_head.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
<link rel="stylesheet" href="/core/styles/pfe-layouts.min.css">
2222
<link rel="stylesheet" href="/main.css">
2323

24-
<script type="module" src="/bundle.js"></script>
24+
<script type="module" src="/pfe.min.js"></script>
2525
</head>

docs/_plugins/bundle.cjs

Lines changed: 0 additions & 62 deletions
This file was deleted.

docs/_plugins/pfe-assets.cjs

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,79 @@
11
const fs = require('fs');
22
const { join } = require('path');
3-
const { bundle } = require('./bundle.cjs');
3+
4+
/** Generate a map of files per package which should be copied to the site dir */
5+
function getFilesToCopy() {
6+
const repoRoot = join(__dirname, '..', '..');
7+
8+
// Copy all component and core files to _site
9+
const files = Object.fromEntries([
10+
...fs.readdirSync(join(repoRoot, 'elements')).map(dir => [
11+
`elements/${dir}`,
12+
`components/${dir.replace('pfe-', '')}`,
13+
]),
14+
...fs.readdirSync(join(repoRoot, 'core')).map(dir => [
15+
`core/${dir}`,
16+
`core/${dir.replace('pfe-', '')}`,
17+
]),
18+
]);
19+
20+
return files;
21+
}
422

523
let didFirstBuild = false;
24+
/** Generate a single-file bundle of all pfe components and their dependencies */
25+
async function bundle() {
26+
if (!didFirstBuild) {
27+
const { singleFileBuild } = await import('@patternfly/pfe-tools/esbuild.js');
28+
const { pfeEnvPlugin } = await import('@patternfly/pfe-tools/esbuild-plugins/pfe-env.js');
29+
30+
await singleFileBuild({
31+
minify: process.env.NODE_ENV === 'production',
32+
outfile: 'docs/pfe.min.js',
33+
conditions: ['esbuild'],
34+
plugins: [
35+
pfeEnvPlugin(),
36+
]
37+
}).catch(() => void 0);
38+
didFirstBuild = true;
39+
}
40+
}
41+
42+
/**
43+
* @typedef {object} EleventyTransformContext
44+
* @property {string} outputPath path this file will be written to
45+
*/
46+
47+
/**
48+
* Replace paths in demo files from the dev SPA's format to 11ty's format
49+
* @this {EleventyTransformContext}
50+
*/
51+
function demoPaths(content) {
52+
if (this.outputPath.match(/(components|core|tools)\/.*\/demo\/index\.html$/)) {
53+
return content.replace(/(?<attr>href|src)="\/(?<workspace>elements|core)\/pfe-(?<unprefixed>.*)\/(?<filename>.*)\.(?<extension>[.\w]+)"/g, (...args) => {
54+
const [{ attr, workspace, unprefixed, filename, extension }] = args.reverse();
55+
return `${attr}="/${workspace === 'elements' ? 'components' : workspace}/${unprefixed}/${filename}.${extension}"`;
56+
});
57+
} else {
58+
return content;
59+
}
60+
}
661

762
module.exports = {
863
configFunction(eleventyConfig) {
964
eleventyConfig.addWatchTarget('tools/pfe-tools/11ty/**/*.{js,njk}');
1065
eleventyConfig.addPassthroughCopy('docs/bundle.{js,map,ts}');
66+
eleventyConfig.addPassthroughCopy('docs/pfe.min.{map,css}');
1167
eleventyConfig.addPassthroughCopy('docs/demo.{js,map,ts}');
1268
eleventyConfig.addPassthroughCopy('docs/main.mjs');
1369
eleventyConfig.addPassthroughCopy('brand/**/*');
14-
15-
const repoRoot = join(__dirname, '..', '..');
16-
17-
// Copy all component and core files to _site
18-
const files = Object.fromEntries([
19-
...fs.readdirSync(join(repoRoot, 'elements')).map(dir => [
20-
`elements/${dir}`,
21-
`components/${dir.replace('pfe-', '')}`,
22-
]),
23-
...fs.readdirSync(join(repoRoot, 'core')).map(dir => [
24-
`core/${dir}`,
25-
`core/${dir.replace('pfe-', '')}`,
26-
]),
27-
]);
28-
29-
eleventyConfig.addPassthroughCopy(files);
70+
eleventyConfig.addPassthroughCopy(getFilesToCopy());
3071

3172
// The demo files are written primarily for the dev SPA (`npm start`),
3273
// so here we transform the paths found in those files to match the docs site's file structure
33-
eleventyConfig.addTransform('demo-paths', function(content) {
34-
// eslint-disable-next-line no-invalid-this
35-
if (this.outputPath.match(/(components|core|tools)\/.*\/demo\/index\.html$/)) {
36-
return content.replace(/(?<attr>href|src)="\/(?<workspace>elements|core)\/pfe-(?<unprefixed>.*)\/(?<filename>.*)\.(?<extension>[.\w]+)"/g, (...args) => {
37-
const [{ attr, workspace, unprefixed, filename, extension }] = args.reverse();
38-
return `${attr}="/${workspace === 'elements' ? 'components' : workspace}/${unprefixed}/${filename}.${extension}"`;
39-
});
40-
} else {
41-
return content;
42-
}
43-
});
74+
eleventyConfig.addTransform('demo-paths', demoPaths);
4475

45-
eleventyConfig.on('beforeBuild', async () => {
46-
if (!didFirstBuild) {
47-
// create /docs/bundle.js
48-
await bundle();
49-
didFirstBuild = true;
50-
}
51-
});
76+
// create /docs/pfe.min.js
77+
eleventyConfig.on('eleventy.before', bundle);
5278
},
5379
};

docs/demo/components.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

docs/kitchen-sink.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{# <link type="text/css" rel="stylesheet" href="/core/styles/pfe-vars-as-px.min.css" media="all"/> #}
2424
{# <link type="text/css" rel="stylesheet" href="/core/styles/pfe-vars.min.css" media="all"/> #}
2525
<link type="text/css" rel="stylesheet" href="/core/styles/red-hat-font.min.css" media="all"/>
26-
<script type="module" src="/bundle.js"></script>
26+
<script type="module" src="/pfe.min.js"></script>
2727
<noscript><link href="/core/core/pfe--noscript.min.css" rel="stylesheet"></noscript>
2828
<style>
2929
/* Replicate styles from /sites/ and static.redhat.com */

docs/main.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PfeIcon } from '/bundle.js';
1+
import { PfeIcon } from '/pfe.min.js';
22

33
PfeIcon.addIconSet(
44
'fas',

tools/create-element/generator/element.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,6 @@ async function updateTsconfig(options: GenerateElementOptions): Promise<void> {
235235
await execaCommand(`npx eslint --fix ${configPath}`);
236236
}
237237

238-
async function updateDocsBundle(options: GenerateElementOptions): Promise<void> {
239-
const pathname = join(process.cwd(), 'docs', 'demo', 'components.ts');
240-
const content = await readFile(pathname, 'utf8');
241-
242-
await writeFile(pathname, `${content}\nimport '@patternfly/${options.tagName}';`, 'utf8');
243-
await execaCommand(`npx eslint --fix ${pathname}`);
244-
}
245-
246238
/**
247239
* Generate an Element
248240
*/
@@ -259,7 +251,6 @@ export async function generateElement(options: GenerateElementOptions): Promise<
259251
await writeElementFiles(options);
260252
await analyzeElement(options);
261253
await updateTsconfig(options);
262-
await updateDocsBundle(options);
263254
await execaCommand('npm install');
264255
}
265256
}

0 commit comments

Comments
 (0)