|
1 | 1 | import fs from "fs-extra";
|
2 | 2 | import { join, dirname } from "node:path";
|
| 3 | +import postcss from "postcss"; |
| 4 | +import valueParser from "postcss-value-parser"; |
3 | 5 |
|
4 | 6 | const projectRoot = join(process.cwd()); // Assumes script run from project root
|
5 | 7 |
|
@@ -35,29 +37,83 @@ console.log("Static assets copy process completed!");
|
35 | 37 | const staticCssDir = join(projectRoot, "static/css");
|
36 | 38 | const libCssDestDir = join(projectRoot, "src/lib/assets/css");
|
37 | 39 |
|
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"; |
39 | 42 |
|
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 | +); |
41 | 74 |
|
42 | 75 | // Ensure the destination CSS directory exists
|
43 | 76 | fs.ensureDirSync(libCssDestDir);
|
44 | 77 | console.log(`Ensured directory exists: ${libCssDestDir}`);
|
45 | 78 |
|
46 |
| -cssFilesToCopy.forEach((fileName) => { |
| 79 | +// Process CSS files |
| 80 | +cssFilesToCopyAndProcess.forEach(async (fileName) => { |
47 | 81 | const sourcePath = join(staticCssDir, fileName);
|
48 | 82 | const destPath = join(libCssDestDir, fileName);
|
49 | 83 |
|
50 | 84 | if (fs.existsSync(sourcePath)) {
|
51 | 85 | 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}`); |
55 | 97 | } catch (err) {
|
56 |
| - console.error(`Error copying ${fileName}:`, err); |
| 98 | + console.error(`Error copying/processing ${fileName}:`, err); |
57 | 99 | }
|
58 | 100 | } else {
|
59 | 101 | console.warn(`Source file not found, skipping: ${sourcePath}`);
|
60 | 102 | }
|
61 | 103 | });
|
62 | 104 |
|
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!"); |
0 commit comments