-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(web-components): generate SSR templates and stylesheets into src/ #36259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d27a952
7ea587d
46726de
528c4cc
b080c62
96950cb
a352041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "feat(web-components): generate SSR templates and stylesheets into src/ and copy into dist during compile", | ||
| "packageName": "@fluentui/web-components", | ||
| "email": "863023+radium-v@users.noreply.github.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,47 @@ | ||
| /* eslint-disable no-undef */ | ||
|
|
||
| import { execSync } from 'child_process'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| main(); | ||
| import { execSync } from 'node:child_process'; | ||
| import { cp, glob, mkdir } from 'node:fs/promises'; | ||
| import { dirname, join } from 'node:path'; | ||
|
|
||
| function compile() { | ||
| try { | ||
| console.log(chalk.bold(`🎬 compile:start`)); | ||
| import chalk from 'chalk'; | ||
|
|
||
| console.log(chalk.blueBright(`compile: generating design tokens`)); | ||
| execSync(`node ./scripts/generate-tokens`, { stdio: 'inherit' }); | ||
| const SRC = 'src'; | ||
| const OUT = 'dist/esm'; | ||
|
|
||
| console.log(chalk.blueBright(`compile: running tsc`)); | ||
| execSync(`tsc -p tsconfig.lib.json --rootDir ./src --baseUrl .`, { stdio: 'inherit' }); | ||
| async function copySsrAssets() { | ||
| const patterns = ['**/*.template.html', '**/*.styles.css']; | ||
| let count = 0; | ||
|
|
||
| console.log(chalk.bold(`🏁 compile:end`)); | ||
| } catch (err) { | ||
| console.error(err); | ||
| process.exit(1); | ||
| for (const pattern of patterns) { | ||
| for await (const file of glob(pattern, { cwd: SRC })) { | ||
| const from = join(SRC, file); | ||
| const to = join(OUT, file); | ||
| await mkdir(dirname(to), { recursive: true }); | ||
| await cp(from, to); | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| console.log(chalk.dim(`compile: copied ${count} SSR asset${count === 1 ? '' : 's'} from ${SRC}/ → ${OUT}/`)); | ||
| } | ||
|
|
||
| function main() { | ||
| compile(); | ||
| async function compile() { | ||
| console.log(chalk.bold(`🎬 compile:start`)); | ||
|
|
||
| console.log(chalk.blueBright(`compile: generating design tokens`)); | ||
| execSync(`node ./scripts/generate-tokens`, { stdio: 'inherit' }); | ||
|
|
||
| console.log(chalk.blueBright(`compile: running tsc`)); | ||
| execSync(`tsc -p tsconfig.lib.json --rootDir ./src --baseUrl .`, { stdio: 'inherit' }); | ||
|
|
||
| console.log(chalk.blueBright(`compile: copying SSR assets`)); | ||
| await copySsrAssets(); | ||
|
|
||
| console.log(chalk.bold(`🏁 compile:end`)); | ||
| } | ||
|
|
||
| compile().catch(err => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| /* eslint-disable no-undef */ | ||
|
|
||
| /** | ||
| * Regenerates SSR template HTML and stylesheet CSS files next to their | ||
| * source `.template.ts` / `.styles.ts` counterparts in `src/`. | ||
| * | ||
| * Flow: | ||
| * 1. Compile `src/` to a throwaway temp dir so we have JS modules with | ||
| * runtime metadata for the generators to walk. | ||
| * 2. Run `generate-templates` and `generate-stylesheets` from the | ||
| * `@microsoft/fast-test-harness` library, writing back into `src/` | ||
| * while preserving the per-component subdirectory structure. | ||
| * | ||
| * The generated files should be committed to the repo; the normal `compile` script | ||
| * copies them into `dist/esm/`. | ||
| * | ||
| * Running this script is only necessary when making changes to the source `.template.ts` or `.styles.ts` files, | ||
| * and should be part of the development workflow when working on those files. The generated files can then | ||
| * be modified as needed for SSR purposes, and those modifications should also be committed to the repo. | ||
| * | ||
| * Pass `--check` to compare what regeneration would produce against the | ||
| * current working tree without writing. Each output file is classified | ||
| * against the matrix of (TS@HEAD vs TS@working) × (HTML@HEAD vs | ||
| * HTML@working) × (regen output vs disk). Exits non-zero when any | ||
| * stale, hand-edited, or conflicting files are detected. | ||
| */ | ||
|
|
||
| import { execSync } from 'node:child_process'; | ||
| import { mkdirSync, mkdtempSync, rmSync } from 'node:fs'; | ||
| import { glob, readFile } from 'node:fs/promises'; | ||
| import { join, relative } from 'node:path'; | ||
|
|
||
| import chalk from 'chalk'; | ||
| import prettier from 'prettier'; | ||
|
|
||
| import { generateStylesheets } from '@microsoft/fast-test-harness/build/generate-stylesheets.js'; | ||
| import { generateFTemplates } from '@microsoft/fast-test-harness/build/generate-templates.js'; | ||
|
|
||
| const cwd = process.cwd(); | ||
| const TEMP_PARENT = join(cwd, 'temp'); | ||
| const checkMode = process.argv.includes('--check'); | ||
| const label = checkMode ? 'generate:ssr:check' : 'generate:ssr'; | ||
|
|
||
| async function main() { | ||
| mkdirSync(TEMP_PARENT, { recursive: true }); | ||
| const tempDir = mkdtempSync(join(TEMP_PARENT, 'ssr-')); | ||
| const stagingDir = checkMode ? mkdtempSync(join(TEMP_PARENT, 'ssr-staging-')) : null; | ||
| const prettierConfig = (await prettier.resolveConfig(cwd)) ?? {}; | ||
| const outDir = stagingDir ? relative(cwd, stagingDir) : 'src'; | ||
| let exitCode = 0; | ||
|
|
||
| try { | ||
| console.log(chalk.bold(`🎬 ${label} start`)); | ||
|
|
||
| console.log(chalk.blueBright(`${label}: compiling src → ${tempDir}`)); | ||
| execSync(`tsc -p tsconfig.lib.json --rootDir ./src --baseUrl . --outDir ${tempDir} --declaration false`, { | ||
| stdio: 'inherit', | ||
| }); | ||
|
|
||
| console.log(chalk.blueBright(`${label}: writing *.template.html → ${outDir}/`)); | ||
| await generateFTemplates({ | ||
| cwd, | ||
| distDir: tempDir, | ||
| outDir, | ||
| tagPrefix: 'fluent', | ||
| format: content => | ||
| prettier.format(content, { ...prettierConfig, parser: 'html', htmlWhitespaceSensitivity: 'ignore' }), | ||
| }); | ||
|
|
||
| console.log(chalk.blueBright(`${label}: writing *.styles.css → ${outDir}/`)); | ||
| await generateStylesheets({ | ||
| cwd, | ||
| distDir: tempDir, | ||
| outDir, | ||
| format: content => prettier.format(content, { ...prettierConfig, parser: 'css' }), | ||
| }); | ||
|
|
||
| if (checkMode) { | ||
| const result = await classify(stagingDir); | ||
| printSummary(result); | ||
| if (result.stale.length || result.handEdited.length || result.conflicts.length) { | ||
| exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| console.log(chalk.bold(`🏁 ${label} end`)); | ||
| } finally { | ||
| rmSync(tempDir, { recursive: true, force: true }); | ||
| if (stagingDir) rmSync(stagingDir, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| if (exitCode) process.exit(exitCode); | ||
| } | ||
|
|
||
| /** | ||
| * Classify each staged file against the working tree using the | ||
| * four-state matrix. Returns counts plus per-bucket file lists. | ||
| * | ||
| * - `unchanged` — regen produces the file already on disk | ||
| * - `created` — no committed baseline for this HTML/CSS | ||
| * - `updated` — TS changed; regen produces new HTML/CSS (normal flow) | ||
| * - `stale` — TS and HTML both at HEAD, but regen disagrees with disk | ||
| * (committed state is out of sync — CI failure signal) | ||
| * - `handEdited` — HTML differs from HEAD with no TS change; regen would clobber | ||
| * - `conflicts` — both TS and HTML differ from HEAD, and regen disagrees with disk | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the resolution process for situations where edits would be clobbered? Do we have that documented somehwere?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This script is only intended to be run manually by developers, so discrepancies and conflicts would be apparent in PRs. |
||
| */ | ||
| async function classify(stagingDir) { | ||
| const dirtyMap = buildDirtyMap(); | ||
| // git status --porcelain paths are relative to the repo root; lookups | ||
| // need to be prefixed with the path from repo root to cwd. | ||
| const repoPrefix = execSync('git rev-parse --show-prefix', { cwd, encoding: 'utf8' }).trim(); | ||
| const dirtyStatus = path => statusOf(dirtyMap.get(repoPrefix + path)); | ||
| const result = { | ||
| unchanged: [], | ||
| created: [], | ||
| updated: [], | ||
| stale: [], | ||
| handEdited: [], | ||
| conflicts: [], | ||
| }; | ||
|
|
||
| for (const pattern of ['**/*.template.html', '**/*.styles.css']) { | ||
| for await (const file of glob(pattern, { cwd: stagingDir })) { | ||
| const stagedAbs = join(stagingDir, file); | ||
| const srcHtmlAbs = join(cwd, 'src', file); | ||
| const srcHtmlRel = join('src', file); | ||
|
|
||
| let srcTsRel; | ||
| if (file.endsWith('.template.html')) { | ||
| srcTsRel = join('src', file.replace(/\.template\.html$/, '.template.ts')); | ||
| } else if (file.endsWith('.styles.css')) { | ||
| srcTsRel = join('src', file.replace(/\.styles\.css$/, '.styles.ts')); | ||
| } else { | ||
| continue; | ||
| } | ||
|
|
||
| const tsStatus = dirtyStatus(srcTsRel); | ||
| const htmlStatus = dirtyStatus(srcHtmlRel); | ||
| const newHtml = await readFile(stagedAbs, 'utf8'); | ||
| const onDiskHtml = await readFile(srcHtmlAbs, 'utf8').catch(() => null); | ||
|
|
||
| if (newHtml === onDiskHtml) { | ||
| result.unchanged.push(file); | ||
| continue; | ||
| } | ||
|
|
||
| if (htmlStatus === 'new') { | ||
| result.created.push(file); | ||
| continue; | ||
| } | ||
|
|
||
| if (htmlStatus === 'same') { | ||
| if (tsStatus === 'same') { | ||
| result.stale.push(file); | ||
| } else { | ||
| result.updated.push(file); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // htmlStatus === 'changed' | ||
| if (tsStatus === 'same') { | ||
| result.handEdited.push(file); | ||
| } else { | ||
| result.conflicts.push(file); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * One git invocation that returns a map of {pathRelativeToCwd → status code} | ||
| * for every file that differs from HEAD (modified, added, untracked, etc.). | ||
| * Tracked files in sync with HEAD are absent from the map. | ||
| */ | ||
| function buildDirtyMap() { | ||
| const map = new Map(); | ||
| const output = execSync('git status --porcelain=v1 -uall', { cwd, encoding: 'utf8' }); | ||
| for (const line of output.split('\n')) { | ||
| if (!line) continue; | ||
| const code = line.slice(0, 2); | ||
| let path = line.slice(3); | ||
| if (path.startsWith('"')) path = JSON.parse(path); | ||
| const arrow = path.indexOf(' -> '); // rename: "old -> new" | ||
| if (arrow !== -1) path = path.slice(arrow + 4); | ||
| map.set(path, code); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| function statusOf(code) { | ||
| if (!code) return 'same'; | ||
| if (code[0] === '?' || code[0] === 'A') return 'new'; | ||
| return 'changed'; | ||
| } | ||
|
|
||
| function printSummary({ unchanged, created, updated, stale, handEdited, conflicts }) { | ||
| console.log(chalk.bold(`\n${label} summary:`)); | ||
| console.log(chalk.green(` ✓ ${unchanged.length} unchanged`)); | ||
| printBucket(chalk.cyan, '+', created, 'new (no committed baseline)'); | ||
| printBucket(chalk.blue, '✎', updated, 'would update (TS changed)'); | ||
| printBucket(chalk.yellow, '!', stale, 'stale (committed HTML out of sync with committed TS)'); | ||
| printBucket(chalk.yellow, '⚠', handEdited, 'hand-edited (HTML differs from HEAD, TS unchanged)'); | ||
| printBucket(chalk.red, '✘', conflicts, 'conflicts (TS and HTML both differ from HEAD, regen disagrees with disk)'); | ||
| console.log(''); | ||
| } | ||
|
|
||
| function printBucket(color, glyph, files, description) { | ||
| if (files.length === 0) return; | ||
| console.log(color(` ${glyph} ${files.length} ${description}`)); | ||
| for (const f of files) console.log(chalk.dim(` ${f}`)); | ||
| } | ||
|
|
||
| main().catch(err => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.