Skip to content

Commit

Permalink
Build script cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Oct 7, 2024
1 parent 96665bc commit 87ddb16
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 183 deletions.
158 changes: 158 additions & 0 deletions build/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import esbuild from "esbuild";
import { dtsPlugin } from "esbuild-plugin-d.ts";
import { dirname, fromFileUrl, resolve } from "@std/path";
import { cp, isDir, isFile, readFile, rm, rmdir, writeFile } from "@cross/fs";

const baseRelativeProjectRoot = "../"; // Where is this script located relative to the project root
const outputFolder = "dist";
const currentScriptDir = dirname(fromFileUrl(import.meta.url));
const relativeProjectRoot = resolve(currentScriptDir, baseRelativeProjectRoot);
const resolvedDistPath = resolve(relativeProjectRoot, outputFolder);
const resolvedNodeModulesPath = resolve(relativeProjectRoot, "node_modules");
const resolvedPackageJsonPath = resolve(relativeProjectRoot, "package.json");
const resolvedDenoConfigPath = resolve(relativeProjectRoot, "deno.json");

/* - Base esbuild configuration */
const baseConfig = {
entryPoints: [resolve(relativeProjectRoot, "src", "base64.ts")],
bundle: true,
minify: true,
sourcemap: false,
plugins: [dtsPlugin({
tsconfig: {
declarations: true,
compilerOptions: {
//@ts-ignore outDir is valid
outDir: resolvedDistPath,
},
},
})],
};

/* - All esbuild targets */
const buildConfigs = [
{
...baseConfig,
outfile: resolve(resolvedDistPath, "base64.cjs"),
platform: "node",
format: "cjs",
},
{
...baseConfig,
outfile: resolve(resolvedDistPath, "base64.umd.js"),
platform: "browser",
format: "iife",
globalName: "base64",
},
{
...baseConfig,
outfile: resolve(resolvedDistPath, "base64.js"),
platform: "neutral",
format: "esm",
},
];

/* Base package.json (name and version will be transfered from deno.json) */
const basePackageJson = {
description: "Base64 and base64url to string or arraybuffer, and back. Node, Deno or browser.",
author: "Hexagon <github.com/hexagon>",
contributors: [
{
name: "Niklas von Hertzen",
email: "[email protected]",
url: "https://hertzen.com",
},
],
homepage: "https://base64.56k.guru",
repository: {
type: "git",
url: "https://github.com/hexagon/base64",
},
bugs: {
url: "https://github.com/hexagon/base64/issues",
},
files: [
"dist/*",
"SECURITY.md",
],
keywords: [
"base64",
"base64url",
"parser",
"isomorphic",
"arraybuffer",
"string",
],
type: "module",
main: "./dist/base64.cjs",
browser: "./dist/base64.umd.js",
module: "./dist/base64.js",
types: "./dist/base64.d.ts",
exports: {
"./package.json": "./package.json",
".": {
import: { "default": "./dist/base64.js", "types": "./dist/base64.d.ts" },
require: { "default": "./dist/base64.cjs", "types": "./dist/base64.d.cts" },
browser: "./dist/base64.umd.js",
},
},
license: "MIT",
};

async function clean() {
if (await isDir(resolvedDistPath)) {
await rmdir(resolvedDistPath, {
recursive: true,
});
}
if (await isDir(resolvedNodeModulesPath)) {
await rmdir(resolvedNodeModulesPath, {
recursive: true,
});
}
if (await isFile(resolvedPackageJsonPath)) {
await rm(resolvedPackageJsonPath);
}
}

// Function to build with esbuild
async function build() {
try {
//@ts-ignore No need to worry about config errors
await Promise.all(buildConfigs.map((config) => esbuild.build(config)));
// Copy .d.ts to .d.cts
await cp(resolve(resolvedDistPath, "base64.d.ts"), resolve(resolvedDistPath, "base64.d.cts"));
} catch (error) {
console.error("Build failed:", error);
}
}

async function generatePackageJson() {
// Read deno.json
const denoConfig = JSON.parse(
new TextDecoder().decode(await readFile(resolvedDenoConfigPath)),
) as { name: string; version: string };

// Define package.json template
const packageJson = {
...basePackageJson,
name: denoConfig.name,
version: denoConfig.version,
};

// Write package.json
await writeFile(
resolvedPackageJsonPath,
new TextEncoder().encode(JSON.stringify(packageJson, undefined, 2)),
);

console.log("package.json has been generated successfully.");
}

if (Deno.args[1] === "clean") {
await clean();
} else if (Deno.args[1] === "build") {
await build();
} else if (Deno.args[1] === "package") {
await generatePackageJson();
}
28 changes: 0 additions & 28 deletions build/clean.ts

This file was deleted.

74 changes: 0 additions & 74 deletions build/esbuild.ts

This file was deleted.

72 changes: 0 additions & 72 deletions build/npm.ts

This file was deleted.

18 changes: 9 additions & 9 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"name": "@hexagon/base64",
"version": "2.0.0-dev.5",
"version": "2.0.0-dev.6",
"exports": "./src/base64.ts",
"lint": {
"include": ["src", "test"],
"exclude": ["dist", "build"]
"include": ["src", "test", "build"]
},
"fmt": {
"include": ["src", "test"],
"exclude": ["dist", "build"],
"include": ["src", "test", "build"],
"lineWidth": 100
},
"test": {
"exclude": ["dist", "build"]
},
"nodeModulesDir": "auto",
"tasks": {
"build:clean": "deno run --allow-read --allow-write build/clean.ts",
"build:npm": "deno run --allow-read --allow-write build/npm.ts",
"build": "deno test && deno task build:clean && deno run --allow-read --allow-write --allow-env --allow-run build/esbuild.ts && deno task build:npm",
"build:prep": "deno cache --allow-scripts=npm:esbuild build/build.ts",
"build:clean": "deno run --allow-read --allow-write --allow-env build/build.ts -- clean",
"build:npm": "deno run --allow-read --allow-write --allow-env build/build.ts -- package",
"build:esbuild": "deno run --allow-read --allow-write --allow-env --allow-run build/build.ts -- build",
"build": "deno test && deno task build:prep && deno task build:clean && deno task build:esbuild && deno task build:npm",
"check-deps": "deno run -A jsr:@check/deps"
},
"imports": {
"@cross/fs": "jsr:@cross/fs@^0.1.11",
"@cross/utils": "jsr:@cross/utils@^0.16.0",
"@std/path": "jsr:@std/path@^1.0.6",
"esbuild": "npm:esbuild@^0.24.0",
"esbuild-plugin-d.ts": "npm:esbuild-plugin-d.ts@^1.3.0"
Expand Down

0 comments on commit 87ddb16

Please sign in to comment.