Skip to content

Commit 66e61b3

Browse files
chore: Support custom css properties (and use in avatar) (#78)
1 parent cba15f8 commit 66e61b3

File tree

14 files changed

+105
-48
lines changed

14 files changed

+105
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ lib
66
dist
77
/src/test-utils/selectors
88
/src/test-utils/dom/index.ts
9+
/src/internal/generated/custom-css-properties
910
.DS_STORE

package-lock.json

Lines changed: 18 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"homepage": "https://cloudscape.design",
99
"scripts": {
1010
"prebuild": "rm -rf lib dist .cache",
11-
"build": "npm-run-all build:pkg --parallel build:src:* --parallel build:pages:* build:themeable",
11+
"build": "npm-run-all build:pkg build:custom-css-properties --parallel build:src:* --parallel build:pages:* build:themeable",
1212
"lint": "npm-run-all --parallel lint:*",
1313
"lint:eslint": "eslint --ignore-path .gitignore --ext ts,tsx,js .",
1414
"lint:stylelint": "stylelint --ignore-path .gitignore '{src,pages}/**/*.{css,scss}'",
@@ -27,6 +27,7 @@
2727
"start:watch:ts": "npm run build:src:js -- --watch",
2828
"start:watch:css": "chokidar \"./src/**/*.scss\" -c \"npm run build:src:css\"",
2929
"build:pkg": "node scripts/package-json.js",
30+
"build:custom-css-properties": "node scripts/custom-css-properties.js",
3031
"build:src:js": "tsc -p tsconfig.json --inlineSources --sourceMap",
3132
"build:src:css": "node scripts/compile-styles.js",
3233
"build:src:test-utils": "node scripts/test-utils.js",
@@ -78,6 +79,7 @@
7879
"@vitejs/plugin-react": "^4.2.1",
7980
"@vitest/coverage-v8": "^3.0.7",
8081
"@vitest/eslint-plugin": "^1.1.31",
82+
"change-case": "^4.1.2",
8183
"chokidar-cli": "^3.0.0",
8284
"deep-freeze-es6": "^1.4.1",
8385
"delay-cli": "^2.0.0",

scripts/custom-css-properties.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { Buffer } from "buffer";
4+
import { getHashDigest } from "loader-utils";
5+
import path from "path";
6+
7+
import customCssPropertiesList from "./utils/custom-css-properties-list.js";
8+
import { writeSourceFile } from "./utils/files.js";
9+
import { gitCommitVersion } from "./utils/workspace.js";
10+
11+
const outputBasePath = "src/internal/generated/custom-css-properties";
12+
const hash = getHashDigest(Buffer.from(JSON.stringify(customCssPropertiesList)), "md5", "base36", 6);
13+
14+
const getHashedProperty = (property) => {
15+
return `--awsui-${property.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase())}-${hash}`;
16+
};
17+
18+
function writeJsFile() {
19+
const filepath = path.join(outputBasePath, "index.ts");
20+
21+
writeSourceFile(
22+
filepath,
23+
`
24+
const customCSSPropertiesMap = {
25+
${customCssPropertiesList.map((property) => `"${property}": "${getHashedProperty(property)}",`).join("\n")}
26+
};
27+
export default customCSSPropertiesMap;
28+
`,
29+
);
30+
}
31+
32+
function writeSassFile() {
33+
const filepath = path.join(outputBasePath, "index.scss");
34+
35+
writeSourceFile(
36+
filepath,
37+
`
38+
// Build environment
39+
$awsui-commit-hash: "${gitCommitVersion}";
40+
// Manually managed CSS-variables
41+
${customCssPropertiesList.map((property) => `$${property}: ${getHashedProperty(property)};`).join("\n")}
42+
`,
43+
);
44+
}
45+
46+
writeJsFile();
47+
writeSassFile();

scripts/docs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "node:path";
44

55
import { documentTestUtils, writeComponentsDocumentation } from "@cloudscape-design/documenter";
66

7-
import { writeSourceFile } from "./utils.js";
7+
import { writeSourceFile } from "./utils/files.js";
88

99
const targetDir = "lib/components/internal/api-docs";
1010

scripts/environment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
import * as fs from "node:fs";
55
import path from "node:path";
6-
import process from "node:process";
6+
7+
import { gitCommitVersion } from "./utils/workspace.js";
78

89
const pkg = JSON.parse(fs.readFileSync("package.json", "utf-8"));
9-
const gitCommitVersion = (process.env.GITHUB_SHA || "HEAD").slice(0, 8);
1010
const packageVersion = `${pkg.version} (${gitCommitVersion})`;
1111

1212
const basePath = "lib/components/internal/environment";

scripts/package-json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import fs from "node:fs";
44

5-
import { writeJSON } from "./utils.js";
5+
import { writeJSON } from "./utils/files.js";
66

77
const pkg = JSON.parse(fs.readFileSync("package.json", "utf-8"));
88

scripts/test-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3+
import { pascalCase } from "change-case";
34
import { execaSync } from "execa";
45
import { globbySync } from "globby";
56
import path from "node:path";
67

78
import { generateTestUtils } from "@cloudscape-design/test-utils-converter";
89

9-
import { pluralizeComponentName } from "./pluralize.js";
10-
import { pascalCase } from "./utils.js";
10+
import { pluralizeComponentName } from "./utils/pluralize.js";
1111

1212
const componentNames = globbySync(["src/test-utils/dom/**/index.ts", "!src/test-utils/dom/index.ts"]).map(
1313
(filePath) => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
/*
4+
* This file is only needed to generate the proper js and scss files in custom-css-properties script
5+
*/
6+
const customCssPropertiesList = ["avatarSize"];
7+
export default customCssPropertiesList;
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import lodash from "lodash";
43
import fs from "node:fs";
54
import path from "node:path";
65

7-
export function pascalCase(text) {
8-
return capitalize(lodash.camelCase(text));
9-
}
10-
11-
function capitalize(text) {
12-
return text[0].toUpperCase() + text.slice(1);
13-
}
14-
156
export function listPublicDirs(baseDir) {
167
return fs
178
.readdirSync(baseDir)

0 commit comments

Comments
 (0)