From dd26341b945aa45a229ed223cc66cdfee08f6f72 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Sun, 10 Nov 2024 09:55:56 -0800 Subject: [PATCH] build(scripts): convert to ESM --- scripts/build-css.js | 57 ++++++++++++++++++++-------------------- scripts/build-docs.js | 9 ++++--- scripts/build-package.js | 31 +++++++++++----------- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/scripts/build-css.js b/scripts/build-css.js index 167de7048b..bef81602a9 100644 --- a/scripts/build-css.js +++ b/scripts/build-css.js @@ -1,35 +1,34 @@ -const fs = require("node:fs"); -const path = require("node:path"); -const sass = require("sass"); -const autoprefixer = require("autoprefixer"); -const postcss = require("postcss"); +// @ts-check +import fs from "node:fs"; +import path from "node:path"; +import sass from "sass"; +import autoprefixer from "autoprefixer"; +import postcss from "postcss"; -(async () => { - const scss = fs - .readdirSync("css") - .filter((file) => file.endsWith(".scss") && !/^\_popover/.test(file)) - .map((file) => path.parse(file)); +const scss = fs + .readdirSync("css") + .filter((file) => file.endsWith(".scss") && !/^\_popover/.test(file)) + .map((file) => path.parse(file)); - for (const { name, base } of scss) { - const file = `css/${base}`; - const outFile = `css/${name}.css`; +for (const { name, base } of scss) { + const file = `css/${base}`; + const outFile = `css/${name}.css`; - console.log("[build-css]", file, "-->", outFile); + console.log("[build-css]", file, "-->", outFile); - const { css } = sass.renderSync({ - file, - outFile, - outputStyle: "compressed", - omitSourceMapUrl: true, - includePaths: ["node_modules"], - }); + const { css } = sass.renderSync({ + file, + outFile, + outputStyle: "compressed", + omitSourceMapUrl: true, + includePaths: ["node_modules"], + }); - const prefixed = await postcss([ - autoprefixer({ - overrideBrowserslist: ["last 1 version", "ie >= 11", "Firefox ESR"], - }), - ]).process(css, { from: undefined }); + const prefixed = await postcss([ + autoprefixer({ + overrideBrowserslist: ["last 1 version", "ie >= 11", "Firefox ESR"], + }), + ]).process(css, { from: undefined }); - fs.writeFileSync(outFile, prefixed.css); - } -})(); + fs.writeFileSync(outFile, prefixed.css); +} diff --git a/scripts/build-docs.js b/scripts/build-docs.js index 9af8c19166..be68b8d969 100644 --- a/scripts/build-docs.js +++ b/scripts/build-docs.js @@ -1,7 +1,8 @@ -const fs = require("node:fs"); -const { globSync }= require("tinyglobby"); -const { sveld } = require("sveld"); -const pkg = require("../package.json"); +// @ts-check +import fs from "node:fs"; +import { globSync } from "tinyglobby"; +import { sveld } from "sveld"; +import pkg from "../package.json" assert { type: "json" }; sveld({ glob: true, diff --git a/scripts/build-package.js b/scripts/build-package.js index 820b3de56a..f05a8f3e0f 100644 --- a/scripts/build-package.js +++ b/scripts/build-package.js @@ -1,26 +1,25 @@ -const fs = require("node:fs"); -const path = require("node:path"); - -const packagePath = path.join(process.cwd(), "package.json"); -const package = JSON.parse(fs.readFileSync(packagePath, "utf8")); +// @ts-check +import fs from "node:fs"; +import path from "node:path"; +import pkg from "../package.json" assert { type: "json" }; +/** @type {Array} */ const keys_to_remove = ["prettier", "standard-version", "devDependencies"]; -const scripts_to_keep = ["postinstall"]; for (const key of keys_to_remove) { - delete package[key]; + delete pkg[key]; } -if (package.scripts) { - const preserved_scripts = {}; +/** @type {Set} */ +const scripts_to_keep = new Set(["postinstall"]); - for (const script of scripts_to_keep) { - if (package.scripts[script]) { - preserved_scripts[script] = package.scripts[script]; - } +for (const script in pkg.scripts) { + // @ts-ignore + if (!scripts_to_keep.has(script)) { + delete pkg.scripts[script]; } - - package.scripts = preserved_scripts; } -fs.writeFileSync(packagePath, JSON.stringify(package, null, 2) + "\n"); +// Write the updated package.json file. +const pkgPath = path.join(process.cwd(), "package.json"); +fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");