-
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
Merged
Jane Chu (janechu)
merged 9 commits into
microsoft:master
from
radium-v:wc-include-templates-in-src
Jun 11, 2026
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d27a952
feat(web-components): generate SSR templates and stylesheets into src…
radium-v 7ea587d
fix(web-components): close CSS blocks in dialog-body and dropdown styles
radium-v 46726de
chore(web-components): commit generated SSR templates and stylesheets
radium-v 528c4cc
add changefile
radium-v b080c62
update readme with dsd generation details
radium-v 96950cb
chore: update dependencies for fast-build, fast-html, and fast-test-h…
radium-v a352041
update stylesheets
radium-v 1b96612
Merge branch 'master' into wc-include-templates-in-src
marchbox 359cb7a
fix(web-components): update SSR dialog template
janechu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-web-components-39809c10-c214-4e43-b550-a3f303e597d0.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
janechu marked this conversation as resolved.
|
||
| */ | ||
| 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); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.