Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: refactor scripts to use Bun APIs #339

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"prettier-plugin-svelte": "latest",
"svelte": "latest",
"svelte-focus-key": "latest",
"totalist": "latest",
"typescript": "latest",
"vite": "latest"
},
Expand Down
6 changes: 3 additions & 3 deletions scripts/build-languages.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { $ } from "bun";
import hljs from "highlight.js";
import type { ModuleNames } from "./build-styles";
import { createMarkdown } from "./utils/create-markdown";
import { mkdir } from "./utils/fs";
import { toCamelCase } from "./utils/to-pascal-case";
import { writeTo } from "./utils/write-to";
import type { ModuleNames } from "./build-styles";

export async function buildLanguages() {
console.time("build languages");
mkdir("src/languages");
await $`rm -rf src/styles; mkdir src/styles`;

let languages = hljs.listLanguages();
let markdown = createMarkdown("Languages", languages.length);
Expand Down
22 changes: 9 additions & 13 deletions scripts/build-styles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { $, Glob } from "bun";
import path from "node:path";
import { totalist } from "totalist";
import { createMarkdown } from "./utils/create-markdown";
import { copyFile, mkdir, readFile } from "./utils/fs";
import { minifyCss } from "./utils/minify-css";
import { toCamelCase } from "./utils/to-pascal-case";
import { writeTo } from "./utils/write-to";
Expand All @@ -10,19 +9,16 @@ export type ModuleNames = Array<{ name: string; moduleName: string }>;

export async function buildStyles() {
console.time("build styles");
mkdir("src/styles");
await $`rm -rf src/styles; mkdir src/styles`;

let scoped_styles = "";
let names: string[] = [];
let styles: ModuleNames = [];

await totalist("node_modules/highlight.js/styles", async (file, absPath) => {
/**
* highlight.js >=v11.19.0 also ships minified
* CSS with the extension `.min.css`.
*
* We only include non-minified CSS files.
*/
const glob = new Glob("**/*");

for await (const file of glob.scan("node_modules/highlight.js/styles")) {
const absPath = path.resolve("node_modules/highlight.js/styles", file);
if (/(?<!\.min)\.(css)$/.test(file)) {
let { name, dir } = path.parse(file);
let moduleName = toCamelCase(name);
Expand All @@ -39,7 +35,7 @@ export async function buildStyles() {
names.push(name);
styles.push({ name, moduleName });

const content = await readFile(absPath, "utf-8");
const content = await Bun.file(absPath).text();
const css_minified = minifyCss(content);

// Escape backticks for JS template literal.
Expand Down Expand Up @@ -73,10 +69,10 @@ export async function buildStyles() {
} else {
// Copy over other file types, like images.
if (!/\.(css)$/.test(file)) {
await copyFile(absPath, `src/styles/${file}`);
await $`cp ${absPath} src/styles/`;
}
}
});
}

styles = styles.sort((a, b) => {
if (a.name > b.name) return 1;
Expand Down
12 changes: 5 additions & 7 deletions scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { $ } from "bun";

import { buildLanguages } from "./build-languages";
import { buildStyles } from "./build-styles";
import { mkdir } from "./utils/fs";

(async () => {
mkdir("www/data");

await buildLanguages();
await buildStyles();
})();
await $`rm -rf www/data; mkdir www/data`;
await buildLanguages();
await buildStyles();
125 changes: 57 additions & 68 deletions scripts/npm-package.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,65 @@
import fs from "node:fs";
import { mkdir, writeFile } from "./utils/fs";
import { $ } from "bun";

async function npmPackage() {
console.time("package");
console.time("package");
await $`rm -rf package; mkdir package`;
await Bun.write("./package/package.json", Bun.file("./package.json"));
await Bun.write("./package/README.md", Bun.file("./README.md"));
await Bun.write("./package/LICENSE", Bun.file("./LICENSE"));

mkdir("./package");
// Copy source folder to package
await $`cp -r ./src/ ./package`;

await Bun.write("./package/package.json", Bun.file("./package.json"));
await Bun.write("./package/README.md", Bun.file("./README.md"));
await Bun.write("./package/LICENSE", Bun.file("./LICENSE"));
const pkgJson = await Bun.file("./package/package.json").json();

// Copy source folder to package
mkdir("./package/src");
fs.cpSync("./src", "./package", { recursive: true });
fs.rmSync("./package/src", { recursive: true, force: true });
delete pkgJson.scripts;
delete pkgJson.devDependencies;

const pkgJson = JSON.parse(
fs.readFileSync("./package/package.json", "utf-8"),
);
pkgJson.exports = {
".": {
types: "./index.d.ts",
svelte: "./index.js",
},
"./*.svelte": {
types: "./*.svelte.d.ts",
import: "./*.svelte",
},
"./styles/*.css": {
import: "./styles/*.css",
},
"./styles": {
types: "./styles/index.d.ts",
import: "./styles/index.js",
},
"./styles/*": {
types: "./styles/*.d.ts",
import: "./styles/*.js",
},
"./styles/*.js": {
types: "./styles/*.d.ts",
import: "./styles/*.js",
},
"./languages": {
types: "./languages/index.d.ts",
import: "./languages/index.js",
},
"./languages/*": {
types: "./languages/*.d.ts",
import: "./languages/*.js",
},
"./languages/*.js": {
types: "./languages/*.d.ts",
import: "./languages/*.js",
},
"./package.json": "./package.json",
};

delete pkgJson.scripts;
delete pkgJson.devDependencies;
// Svelte entry point is deprecated but we preserve it for backwards compatibility.
pkgJson.svelte = "./index.js";
pkgJson.types = "./index.d.ts";

pkgJson.exports = {
".": {
types: "./index.d.ts",
svelte: "./index.js",
},
"./*.svelte": {
types: "./*.svelte.d.ts",
import: "./*.svelte",
},
"./styles/*.css": {
import: "./styles/*.css",
},
"./styles": {
types: "./styles/index.d.ts",
import: "./styles/index.js",
},
"./styles/*": {
types: "./styles/*.d.ts",
import: "./styles/*.js",
},
"./styles/*.js": {
types: "./styles/*.d.ts",
import: "./styles/*.js",
},
"./languages": {
types: "./languages/index.d.ts",
import: "./languages/index.js",
},
"./languages/*": {
types: "./languages/*.d.ts",
import: "./languages/*.js",
},
"./languages/*.js": {
types: "./languages/*.d.ts",
import: "./languages/*.js",
},
"./package.json": "./package.json",
};
// Most modern bundlers know if standalone StyleSheets are used.
// We specify it here to be sure so that it will not be mistakenly tree-shaken.
pkgJson.sideEffects = ["src/styles/*.css"];

// Svelte entry point is deprecated but we preserve it for backwards compatibility.
pkgJson.svelte = "./index.js";
pkgJson.types = "./index.d.ts";

// Most modern bundlers know if standalone StyleSheets are used.
// We specify it here to be sure so that it will not be mistakenly tree-shaken.
pkgJson.sideEffects = ["src/styles/*.css"];

await writeFile("./package/package.json", JSON.stringify(pkgJson, null, 2));
console.timeEnd("package");
}

npmPackage();
await Bun.write("./package/package.json", JSON.stringify(pkgJson, null, 2));
console.timeEnd("package");
8 changes: 2 additions & 6 deletions scripts/utils/create-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import fs from "node:fs";

const pkg = JSON.parse(
fs.readFileSync(new URL("../../package.json", import.meta.url), "utf8"),
);
import { dependencies } from "../../package.json";

/** Creates header metadata for supported languages/styles */
export const createMarkdown = (type: "Languages" | "Styles", len: number) =>
`
# Supported ${type}

> ${len} ${type.toLowerCase()} exported from highlight.js@${
pkg.dependencies["highlight.js"]
dependencies["highlight.js"]
}
`;
12 changes: 0 additions & 12 deletions scripts/utils/fs.ts

This file was deleted.

3 changes: 1 addition & 2 deletions scripts/utils/write-to.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from "node:path";
import prettier, { type BuiltInParserName } from "prettier";
import { writeFile } from "./fs";

const PARSER: Record<string, BuiltInParserName> = {
".md": "markdown",
Expand All @@ -17,5 +16,5 @@ export async function writeTo(file: string, source: string | object) {

if (!parser) throw new Error(`No parser found for ${file}`);

await writeFile(file, await prettier.format(value, { parser }));
await Bun.write(file, await prettier.format(value, { parser }));
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"svelte-highlight/*": ["./src/*"]
}
},
"include": ["src/**/*", "www/**/*", "tests/**/*", "scripts/**/*"],
"include": ["src/**/*", "www/**/*", "tests/**/*", "scripts/**/*"]
}