Skip to content

Commit

Permalink
Revert "build: refactor scripts to use Bun APIs (#339)"
Browse files Browse the repository at this point in the history
This reverts commit 93f6056.
  • Loading branch information
metonym authored Apr 13, 2024
1 parent 3a4b500 commit 25d242b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 78 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"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");
await $`rm -rf src/styles; mkdir src/styles`;
mkdir("src/languages");

let languages = hljs.listLanguages();
let markdown = createMarkdown("Languages", languages.length);
Expand Down
22 changes: 13 additions & 9 deletions scripts/build-styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -9,16 +10,19 @@ export type ModuleNames = Array<{ name: string; moduleName: string }>;

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

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

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);
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.
*/
if (/(?<!\.min)\.(css)$/.test(file)) {
let { name, dir } = path.parse(file);
let moduleName = toCamelCase(name);
Expand All @@ -35,7 +39,7 @@ export async function buildStyles() {
names.push(name);
styles.push({ name, moduleName });

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

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

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

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

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

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

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"));
async function npmPackage() {
console.time("package");

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

const pkgJson = await Bun.file("./package/package.json").json();
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"));

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

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",
};
const pkgJson = JSON.parse(
fs.readFileSync("./package/package.json", "utf-8"),
);

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

// 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"];
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",
};

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

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

/** Creates header metadata for supported languages/styles */
export const createMarkdown = (type: "Languages" | "Styles", len: number) =>
`
# Supported ${type}
> ${len} ${type.toLowerCase()} exported from highlight.js@${
dependencies["highlight.js"]
pkg.dependencies["highlight.js"]
}
`;
12 changes: 12 additions & 0 deletions scripts/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from "node:fs";
import fsp from "node:fs/promises";

export const mkdir = (dir: string) => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true });
}
fs.mkdirSync(dir);
};
export const readFile = fsp.readFile;
export const writeFile = fsp.writeFile;
export const copyFile = fsp.copyFile;
3 changes: 2 additions & 1 deletion scripts/utils/write-to.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "node:path";
import prettier, { type BuiltInParserName } from "prettier";
import { writeFile } from "./fs";

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

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

await Bun.write(file, await prettier.format(value, { parser }));
await writeFile(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/**/*"],
}

0 comments on commit 25d242b

Please sign in to comment.