From 1c2197bd30cadfc71621938e9cdd01d409ab2c7e Mon Sep 17 00:00:00 2001 From: Elizabeth Mitchell Date: Tue, 5 Mar 2024 11:51:54 -0800 Subject: [PATCH] chore: update css-to-ts script PiperOrigin-RevId: 612919272 --- .gitignore | 1 - css-to-ts.js | 25 ------------------------- package.json | 34 +++++++++++++--------------------- scripts/css-to-ts.ts | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 47 deletions(-) delete mode 100644 css-to-ts.js create mode 100644 scripts/css-to-ts.ts diff --git a/.gitignore b/.gitignore index 3dcd35900..23bc3e537 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ node_modules *.js !web-test-runner.config.js -!css-to-ts.js !commitlint.config.js *.css *-styles.ts diff --git a/css-to-ts.js b/css-to-ts.js deleted file mode 100644 index e48efc385..000000000 --- a/css-to-ts.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @license - * Copyright 2022 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import * as fs from 'fs'; - -for (let i = 2; i < process.argv.length; i++) { - try { - const filePath = process.argv[i]; - const content = fs.readFileSync(filePath); - fs.writeFileSync(filePath.replace('.css', '.ts'), `/** - * @license - * Copyright 2022 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - import {css} from 'lit'; - export const styles = css\`${content.toString('utf8')}\`; - `); - - } catch (error) { - console.error(error); - } -} diff --git a/package.json b/package.json index 56619aaf7..7b529b6f4 100644 --- a/package.json +++ b/package.json @@ -39,13 +39,12 @@ "**/*.js.map", "**/*.d.ts", "**/*.scss", - "!css-to-ts.js", "!web-test-runner.config.js", "!commitlint.config.js", "!**/test/**", "!**/*_test.*", "!.wireit/**", - "!catalog", + "!catalog/", "!scripts/", "!**/testing/**", "testing/harness.{js,js.map,d.ts}", @@ -87,20 +86,18 @@ "**/*.ts", "!**/*.d.ts", "!**/*-styles.ts", - "!catalog", - "!scripts/", - "!node_modules" + "!catalog/", + "!scripts/" ], "output": [ ".tsbuildinfo", "**/*.js", "**/*.js.map", "**/*.d.ts", - "!css-to-ts.js", "!web-test-runner.config.js", "!commitlint.config.js", "!types/", - "!catalog", + "!catalog/", "!scripts/" ], "clean": "if-file-deleted", @@ -109,18 +106,17 @@ ] }, "build:css-to-ts": { - "command": "find . \\( -path ./.wireit -o -path ./node_modules -o -path ./catalog \\) -prune -o -name '*-styles.css' -print | xargs node css-to-ts.js", + "command": "find . \\( -path ./.wireit -o -path ./node_modules -o -path ./catalog \\) -prune -o -name '*-styles.css' -print | xargs -L1 node scripts/css-to-ts.js", "files": [ - "css-to-ts.js", - "!scripts/", - "!node_modules" + "**/*-styles.css", + "!catalog/" ], "output": [ "**/*-styles.ts", - "!catalog", - "!scripts/" + "!catalog/" ], "dependencies": [ + "build:scripts", "build:sass" ] }, @@ -128,15 +124,12 @@ "command": "sass --style=compressed --load-path=node_modules --load-path=node_modules/sass-true/sass $(ls -d */ | grep -vE 'node_modules|catalog')", "files": [ "**/*.scss", - "!catalog", - "!scripts/", - "!node_modules" + "!catalog/" ], "output": [ "**/*.css", "**/*.css.map", - "!catalog", - "!scripts/" + "!catalog/" ] }, "test": { @@ -177,10 +170,9 @@ "**/*.ts", "!**/*.d.ts", "!**/*-styles.ts", - "!catalog", + "!catalog/", "!scripts/", - "scripts/analyzer/update-docs.js", - "!node_modules" + "scripts/analyzer/update-docs.js" ], "output": [], "dependencies": [ diff --git a/scripts/css-to-ts.ts b/scripts/css-to-ts.ts new file mode 100644 index 000000000..178672c74 --- /dev/null +++ b/scripts/css-to-ts.ts @@ -0,0 +1,32 @@ +/** + * @license + * Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import * as fs from 'fs'; + +const cssFilePath = process.argv[2]; +if (!cssFilePath) { + throw new Error(`Usage: node scripts/css-to-ts.js [output.ts]`); +} + +const tsFilePath = process.argv[3] || cssFilePath.replace('.css', '.ts'); +const cssContent = fs + .readFileSync(cssFilePath, {encoding: 'utf8'}) + // Remove source map comments since the css is embedded. + // "/*# sourceMappingURL=checkbox-styles.css.map */" + .replace(/\/\*#\ sourceMappingURL=[^\*]+ \*\//, ''); + +fs.writeFileSync( + tsFilePath, + `/** + * @license + * Copyright 2024 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ +// Generated stylesheet for ${cssFilePath}. +import {css} from 'lit'; +export const styles = css\`${cssContent}\`; +`, +);