build(scripts): convert to ESM

This commit is contained in:
Eric Liu 2024-11-10 09:55:56 -08:00
commit 4881ff7ffd
3 changed files with 48 additions and 49 deletions

View file

@ -1,35 +1,34 @@
const fs = require("node:fs"); // @ts-check
const path = require("node:path"); import fs from "node:fs";
const sass = require("sass"); import path from "node:path";
const autoprefixer = require("autoprefixer"); import sass from "sass";
const postcss = require("postcss"); import autoprefixer from "autoprefixer";
import postcss from "postcss";
(async () => { const scss = fs
const scss = fs .readdirSync("css")
.readdirSync("css") .filter((file) => file.endsWith(".scss") && !/^\_popover/.test(file))
.filter((file) => file.endsWith(".scss") && !/^\_popover/.test(file)) .map((file) => path.parse(file));
.map((file) => path.parse(file));
for (const { name, base } of scss) { for (const { name, base } of scss) {
const file = `css/${base}`; const file = `css/${base}`;
const outFile = `css/${name}.css`; const outFile = `css/${name}.css`;
console.log("[build-css]", file, "-->", outFile); console.log("[build-css]", file, "-->", outFile);
const { css } = sass.renderSync({ const { css } = sass.renderSync({
file, file,
outFile, outFile,
outputStyle: "compressed", outputStyle: "compressed",
omitSourceMapUrl: true, omitSourceMapUrl: true,
includePaths: ["node_modules"], includePaths: ["node_modules"],
}); });
const prefixed = await postcss([ const prefixed = await postcss([
autoprefixer({ autoprefixer({
overrideBrowserslist: ["last 1 version", "ie >= 11", "Firefox ESR"], overrideBrowserslist: ["last 1 version", "ie >= 11", "Firefox ESR"],
}), }),
]).process(css, { from: undefined }); ]).process(css, { from: undefined });
fs.writeFileSync(outFile, prefixed.css); fs.writeFileSync(outFile, prefixed.css);
} }
})();

View file

@ -1,7 +1,8 @@
const fs = require("node:fs"); // @ts-check
const { globSync }= require("tinyglobby"); import fs from "node:fs";
const { sveld } = require("sveld"); import { globSync } from "tinyglobby";
const pkg = require("../package.json"); import { sveld } from "sveld";
import pkg from "../package.json" assert { type: "json" };
sveld({ sveld({
glob: true, glob: true,

View file

@ -1,26 +1,25 @@
const fs = require("node:fs"); // @ts-check
const path = require("node:path"); import fs from "node:fs";
import path from "node:path";
const packagePath = path.join(process.cwd(), "package.json"); import pkg from "../package.json" assert { type: "json" };
const package = JSON.parse(fs.readFileSync(packagePath, "utf8"));
/** @type {Array<keyof typeof pkg>} */
const keys_to_remove = ["prettier", "standard-version", "devDependencies"]; const keys_to_remove = ["prettier", "standard-version", "devDependencies"];
const scripts_to_keep = ["postinstall"];
for (const key of keys_to_remove) { for (const key of keys_to_remove) {
delete package[key]; delete pkg[key];
} }
if (package.scripts) { /** @type {Set<keyof typeof pkg.scripts>} */
const preserved_scripts = {}; const scripts_to_keep = new Set(["postinstall"]);
for (const script of scripts_to_keep) { for (const script in pkg.scripts) {
if (package.scripts[script]) { // @ts-ignore
preserved_scripts[script] = package.scripts[script]; 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");