|
1 | 1 | const fs = require('fs'); |
2 | 2 | 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 | +} |
4 | 22 |
|
5 | 23 | 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 | +} |
6 | 61 |
|
7 | 62 | module.exports = { |
8 | 63 | configFunction(eleventyConfig) { |
9 | 64 | eleventyConfig.addWatchTarget('tools/pfe-tools/11ty/**/*.{js,njk}'); |
10 | 65 | eleventyConfig.addPassthroughCopy('docs/bundle.{js,map,ts}'); |
| 66 | + eleventyConfig.addPassthroughCopy('docs/pfe.min.{map,css}'); |
11 | 67 | eleventyConfig.addPassthroughCopy('docs/demo.{js,map,ts}'); |
12 | 68 | eleventyConfig.addPassthroughCopy('docs/main.mjs'); |
13 | 69 | 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()); |
30 | 71 |
|
31 | 72 | // The demo files are written primarily for the dev SPA (`npm start`), |
32 | 73 | // 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); |
44 | 75 |
|
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); |
52 | 78 | }, |
53 | 79 | }; |
0 commit comments