Skip to content

Commit c20498b

Browse files
add post css parsing to build pipeline to ensure relative paths
1 parent a13b3e4 commit c20498b

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"copy-static-assets-to-lib": "node scripts/copy-static-assets-to-lib.js",
2727
"copy-govuk-assets": "node scripts/copy-govuk-assets-to-static.js",
2828
"copy-moj-assets": "node scripts/copy-moj-assets-to-static.js",
29-
"prepare": "npm run copy-govuk-assets",
29+
"prepare": "",
3030
"create-component-and-wrapper-pages": "node scripts/create-component-and-wrapper-pages.js"
3131
},
3232
"files": [

scripts/copy-static-assets-to-lib.js

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import fs from "fs-extra";
22
import { join, dirname } from "node:path";
3+
import postcss from "postcss";
4+
import valueParser from "postcss-value-parser";
35

46
const projectRoot = join(process.cwd()); // Assumes script run from project root
57

@@ -35,29 +37,83 @@ console.log("Static assets copy process completed!");
3537
const staticCssDir = join(projectRoot, "static/css");
3638
const libCssDestDir = join(projectRoot, "src/lib/assets/css");
3739

38-
const cssFilesToCopy = ["govuk-frontend.min.css", "govuk-frontend.min.css.map"];
40+
const cssFilesToCopyAndProcess = ["govuk-frontend.min.css"];
41+
const mapFileToCopy = "govuk-frontend.min.css.map";
3942

40-
console.log("Copying specific GOV.UK CSS assets to src/lib/assets/css...");
43+
// Define the PostCSS plugin logic directly here
44+
const transformAssetUrlsPlugin = {
45+
postcssPlugin: "transform-asset-urls-for-lib",
46+
Declaration(decl) {
47+
if (!/url\(/.test(decl.value)) return;
48+
const parsedValue = valueParser(decl.value);
49+
parsedValue.walk((node) => {
50+
if (node.type === "function" && node.value === "url") {
51+
const urlNode = node.nodes[0];
52+
if (!urlNode) return;
53+
const url = urlNode.value;
54+
if (url.startsWith("/assets/fonts/")) {
55+
urlNode.value = "../fonts/" + url.slice("/assets/fonts/".length);
56+
} else if (url.startsWith("/assets/images/")) {
57+
urlNode.value = "../images/" + url.slice("/assets/images/".length);
58+
} else if (
59+
url.startsWith("/assets/govuk_publishing_components/images/")
60+
) {
61+
urlNode.value =
62+
"../govuk_publishing_components/images/" +
63+
url.slice("/assets/govuk_publishing_components/images/".length);
64+
}
65+
}
66+
});
67+
decl.value = parsedValue.toString();
68+
},
69+
};
70+
71+
console.log(
72+
"Copying and processing specific GOV.UK CSS assets to src/lib/assets/css...",
73+
);
4174

4275
// Ensure the destination CSS directory exists
4376
fs.ensureDirSync(libCssDestDir);
4477
console.log(`Ensured directory exists: ${libCssDestDir}`);
4578

46-
cssFilesToCopy.forEach((fileName) => {
79+
// Process CSS files
80+
cssFilesToCopyAndProcess.forEach(async (fileName) => {
4781
const sourcePath = join(staticCssDir, fileName);
4882
const destPath = join(libCssDestDir, fileName);
4983

5084
if (fs.existsSync(sourcePath)) {
5185
try {
52-
// Copy the individual file
53-
fs.copyFileSync(sourcePath, destPath);
54-
console.log(`Copied ${sourcePath} to ${destPath}`);
86+
// Read the original CSS
87+
const css = await fs.readFile(sourcePath, "utf8");
88+
// Process with PostCSS
89+
const result = await postcss([transformAssetUrlsPlugin]).process(css, {
90+
from: sourcePath,
91+
to: destPath,
92+
map: false,
93+
}); // Disable inline map generation here
94+
// Write the transformed CSS
95+
await fs.writeFile(destPath, result.css);
96+
console.log(`Copied and processed ${sourcePath} to ${destPath}`);
5597
} catch (err) {
56-
console.error(`Error copying ${fileName}:`, err);
98+
console.error(`Error copying/processing ${fileName}:`, err);
5799
}
58100
} else {
59101
console.warn(`Source file not found, skipping: ${sourcePath}`);
60102
}
61103
});
62104

63-
console.log("Static app assets copy process to lib directory completed!");
105+
// Copy the map file separately without processing
106+
const mapSourcePath = join(staticCssDir, mapFileToCopy);
107+
const mapDestPath = join(libCssDestDir, mapFileToCopy);
108+
if (fs.existsSync(mapSourcePath)) {
109+
try {
110+
fs.copyFileSync(mapSourcePath, mapDestPath);
111+
console.log(`Copied map file ${mapSourcePath} to ${mapDestPath}`);
112+
} catch (err) {
113+
console.error(`Error copying map file ${mapFileToCopy}:`, err);
114+
}
115+
} else {
116+
console.warn(`Source map file not found, skipping: ${mapSourcePath}`);
117+
}
118+
119+
console.log("GOV.UK CSS assets copy and process completed!");

scripts/generate-index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const exports = svelteFilesInComponents
5656
// Write the file
5757
writeFileSync(
5858
indexFile,
59-
`// this file is auto-generated — do not edit by hand\nimport "$lib/components_base.css";\n\n${exports.join("\n")}\n`,
59+
`// this file is auto-generated — do not edit by hand\nimport "$lib/main.css";\n\n${exports.join("\n")}\n`,
6060
);
6161

6262
let logMessage = `Generated ${indexFile} with ${exports.length} unique exports from ./components.`;

src/lib/assets/css/govuk-frontend.min.css

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)