Skip to content

Commit 4c8d24d

Browse files
Update build.mjs
1 parent 40a2bfa commit 4c8d24d

File tree

1 file changed

+36
-100
lines changed

1 file changed

+36
-100
lines changed

packages/tel-frontend-review/build.mjs

Lines changed: 36 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,51 @@
11
import { promises as fs } from "node:fs";
22
import { join, dirname, parse } from "node:path";
33
import { fileURLToPath } from "node:url";
4-
import nunjucks from "nunjucks";
54
import fse from "fs-extra";
65
import * as sass from "sass";
7-
import { execSync } from "node:child_process";
86

97
// Replace __dirname in ES modules
108
const __dirname = dirname(fileURLToPath(import.meta.url));
119

12-
const repoRoot = join(__dirname, "../../"); // repo root
10+
const repoRoot = join(__dirname, "../../"); // back to repo root
1311
const reviewSrc = join(__dirname, "src");
1412
const reviewDist = join(__dirname, "dist");
15-
const nhsukDist = join(repoRoot, "node_modules/nhsuk-frontend/dist");
1613

17-
// TEL frontend paths
14+
// TEL frontend package paths
1815
const telFrontendDir = join(repoRoot, "packages/tel-frontend");
1916
const telFrontendDist = join(telFrontendDir, "dist");
2017
const telFrontendSrcScss = join(telFrontendDir, "src/styles.scss");
2118

2219
// -------- Helpers --------
2320

24-
// Build the TEL Frontend package (CSS + JS) using Gulp
25-
async function buildTelFrontend() {
26-
console.log("Building TEL Frontend CSS + JS via Gulp...");
27-
28-
await fse.emptyDir(telFrontendDist);
29-
await fse.ensureDir(telFrontendDist);
30-
31-
try {
32-
execSync("npx gulp build", {
33-
cwd: telFrontendDir,
34-
stdio: "inherit",
35-
});
36-
} catch (err) {
37-
console.error("Gulp build failed:", err);
38-
process.exit(1);
39-
}
40-
41-
console.log("TEL Frontend CSS/JS built successfully");
42-
}
43-
44-
// Copy built TEL frontend files + NHS frontend assets + review site assets
21+
// Copy built TEL frontend files + NHS frontend assets + review-specific assets
4522
async function buildReviewAssets() {
4623
console.log("Copying review site assets...");
4724

48-
const stylesDir = join(reviewDist, "stylesheets");
49-
const scriptsDir = join(reviewDist, "javascripts");
25+
await fse.ensureDir(join(reviewDist, "stylesheets"));
26+
await fse.ensureDir(join(reviewDist, "javascripts"));
5027

51-
await fse.ensureDir(stylesDir);
52-
await fse.ensureDir(scriptsDir);
53-
54-
// --- NHS.UK Frontend v10 ---
55-
const nhsCssSrc = join(repoRoot, "node_modules/nhsuk-frontend/dist/nhsuk/nhsuk-frontend.min.css");
56-
const nhsJsSrc = join(repoRoot, "node_modules/nhsuk-frontend/dist/nhsuk/nhsuk-frontend.min.js");
57-
58-
await fse.copy(nhsCssSrc, join(stylesDir, "nhsuk.min.css"));
59-
await fse.copy(nhsJsSrc, join(scriptsDir, "nhsuk.min.js"));
28+
// Copy precompiled NHS.UK frontend CSS + JS
29+
await fse.copy(
30+
join(repoRoot, "node_modules/nhsuk-frontend/dist/nhsuk.min.css"),
31+
join(reviewDist, "stylesheets/nhsuk.min.css")
32+
);
33+
await fse.copy(
34+
join(repoRoot, "node_modules/nhsuk-frontend/dist/nhsuk.min.js"),
35+
join(reviewDist, "javascripts/nhsuk.min.js")
36+
);
6037

61-
// --- TEL Frontend ---
62-
await fse.copy(join(telFrontendDist, "tel-frontend.css"), join(stylesDir, "tel-frontend.css"));
63-
await fse.copy(join(telFrontendDist, "tel-frontend.js"), join(scriptsDir, "tel-frontend.js"));
64-
await fse.copy(join(telFrontendDist, "tel-frontend.min.js"), join(scriptsDir, "tel-frontend.min.js"));
38+
// Copy TEL frontend built CSS + JS
39+
await fse.copy(
40+
join(telFrontendDist, "tel-frontend.css"),
41+
join(reviewDist, "stylesheets/tel-frontend.css")
42+
);
43+
await fse.copy(
44+
join(telFrontendDist, "tel-frontend.min.js"),
45+
join(reviewDist, "javascripts/tel-frontend.min.js")
46+
);
6547

66-
// --- Review site static assets ---
48+
// Copy static assets for review site (images, etc.)
6749
const reviewAssetsSrc = join(reviewSrc, "assets");
6850
if (await fse.pathExists(reviewAssetsSrc)) {
6951
await fse.copy(reviewAssetsSrc, join(reviewDist, "assets"));
@@ -72,13 +54,13 @@ async function buildReviewAssets() {
7254
console.log("Review site assets copied");
7355
}
7456

75-
// Compile review site SCSS (review site-specific styles)
57+
// Compile review site SCSS (only your custom styles)
7658
async function buildReviewCSS() {
7759
console.log("Building review site CSS...");
7860

7961
const css = sass.compile(join(reviewSrc, "scss/main.scss"), {
8062
style: "expanded",
81-
loadPaths: ["node_modules"],
63+
loadPaths: ["node_modules"], // allows @use of TEL frontend SCSS
8264
});
8365

8466
const outDir = join(reviewDist, "stylesheets");
@@ -88,72 +70,26 @@ async function buildReviewCSS() {
8870
console.log("Review site CSS built at:", join(outDir, "review.css"));
8971
}
9072

91-
// Render review site HTML using Nunjucks
92-
async function buildReviewHtml() {
93-
console.log("Rendering review site HTML...");
94-
95-
const telComponents = join(repoRoot, "packages/tel-frontend/src/tel/components");
96-
97-
const env = nunjucks.configure(
98-
[
99-
reviewSrc, // review site source
100-
join(repoRoot, "node_modules/nhsuk-frontend"), // NHS macros
101-
telComponents // TEL frontend macros
102-
],
103-
{ autoescape: true }
104-
);
105-
106-
// Root-level pages
107-
const files = await fse.readdir(reviewSrc);
108-
for (const file of files) {
109-
if (file.endsWith(".njk")) {
110-
const name = parse(file).name;
111-
const html = env.render(file, { title: "TEL Frontend Review" });
112-
113-
const outDir = join(reviewDist, name === "index" ? "" : name);
114-
await fse.ensureDir(outDir);
115-
await fs.writeFile(join(outDir, "index.html"), html);
116-
console.log(`Rendered ${file} -> ${outDir}/index.html`);
117-
}
118-
}
119-
120-
// Example pages
121-
const examplesSrc = join(reviewSrc, "examples");
122-
const examplesDist = join(reviewDist, "examples");
123-
await fse.ensureDir(examplesDist);
124-
125-
if (await fse.pathExists(examplesSrc)) {
126-
const exampleFiles = await fse.readdir(examplesSrc);
127-
for (const file of exampleFiles) {
128-
if (file.endsWith(".njk")) {
129-
const name = file.replace(/\.njk$/, ".html");
130-
const rendered = env.render(join("examples", file));
131-
await fs.writeFile(join(examplesDist, name), rendered, "utf8");
132-
console.log(`Rendered ${file} -> ${join(examplesDist, name)}`);
133-
}
134-
}
135-
}
136-
137-
console.log("Review HTML rendered");
138-
}
139-
14073
// -------- Main build --------
14174
async function build() {
14275
try {
14376
await fse.emptyDir(reviewDist);
14477

145-
// 1. Build TEL frontend
146-
await buildTelFrontend();
78+
// Step 1: Build TEL frontend (CSS + JS) via Gulp
79+
console.log("Building TEL Frontend CSS + JS via Gulp...");
80+
const { execSync } = await import("node:child_process");
81+
execSync("npx gulp build", {
82+
cwd: telFrontendDir,
83+
stdio: "inherit",
84+
});
85+
console.log("TEL Frontend CSS/JS built successfully");
14786

148-
// 2. Copy assets
87+
// Step 2: Copy assets (TEL + NHS + review site)
14988
await buildReviewAssets();
15089

151-
// 3. Compile review SCSS
90+
// Step 3: Compile review site SCSS
15291
await buildReviewCSS();
15392

154-
// 4. Render review HTML
155-
await buildReviewHtml();
156-
15793
console.log("Review site build finished successfully");
15894
} catch (err) {
15995
console.error("Build failed:", err);

0 commit comments

Comments
 (0)