Skip to content

Commit

Permalink
🔨 Use a script to build the CLI in ESM and CJS formats
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Aug 10, 2024
1 parent 9de0c44 commit ccce6ad
Show file tree
Hide file tree
Showing 6 changed files with 635 additions and 7 deletions.
1 change: 1 addition & 0 deletions libs/create-qwikdev-astro/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
scripts/
src/
tsconfig.json
6 changes: 4 additions & 2 deletions libs/create-qwikdev-astro/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "@qwikdev/create-astro",
"type": "module",
"license": "MIT",
"version": "0.1.2",
"description": "Interactive CLI for create @QwikDev/astro projects.",
"scripts": {
"build": "rimraf dist && tsc --build"
"build": "rimraf dist && tsc --build && tsm scripts/build.ts"
},
"contributors": [
{
Expand Down Expand Up @@ -69,7 +68,10 @@
"devDependencies": {
"@types/which-pm-runs": "^1.0.2",
"@types/yargs": "^17.0.32",
"esbuild": "^0.23.0",
"globby": "^14.0.2",
"rimraf": "^5.0.5",
"tsm": "^2.3.0",
"typescript": "^5.4.5"
}
}
98 changes: 98 additions & 0 deletions libs/create-qwikdev-astro/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import fs from "node:fs";
import path from "node:path";
import zlib from "node:zlib";
import esbuild, { type BuildOptions } from "esbuild";
import { globbySync } from "globby";
import tsconfig from "../tsconfig.json";

export type Target = BuildOptions["target"] | "default";
export type Format = BuildOptions["format"];
export type Platform = BuildOptions["platform"];

const entryPoints = globbySync(tsconfig.include, {
gitignore: true,
ignore: tsconfig.exclude
});

const outdir = tsconfig.compilerOptions.outDir;
const target = tsconfig.compilerOptions.target || "default";
const watch = process.argv.includes("--watch");

build(entryPoints, outdir, target, [undefined, "cjs", "esm"], true, watch);

const files = fs.readdirSync(outdir);
for (const file of files) {
const filePath = path.join(outdir, file);
await outputSize(filePath);
}

function getBuildOptions(
{ format, minify, ...options }: BuildOptions,
watch: boolean
): BuildOptions {
const define = {
CDN: "true",
"process.env.NODE_ENV": watch ? '"development"' : '"production"'
};

return {
...options,
platform: "node",
bundle: true,
minify,
format: format ?? format,
sourcemap: !minify,
outExtension:
format === "cjs"
? { ".js": ".cjs" }
: format === "esm"
? { ".js": ".mjs" }
: { ".js": ".js" },
define
};
}

async function build(
entryPoints: string[],
outdir: string,
target: Target,
formats: Format[] = ["cjs", "esm"],
minify = false,
watch = false
) {
for (const format of formats) {
const options = getBuildOptions(
{
entryPoints,
outdir,
target,
format,
minify
},
watch
);
await (watch
? esbuild.context(options).then((ctx) => ctx.watch())
: esbuild.build(options));
}
}

async function outputSize(file: string) {
const content = fs.readFileSync(file);
const bytes = zlib.brotliCompressSync(content).length;
const size = bytesToSize(bytes);

console.log("\x1b[32m", `${file} bundle size: ${size}`);
}

function bytesToSize(bytes: number): string {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes === 0) {
return "n/a";
}
const i = Math.floor(Math.log(bytes) / Math.log(1024));
if (i === 0) {
return `${bytes} ${sizes[i]}`;
}
return `${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
}
4 changes: 0 additions & 4 deletions libs/create-qwikdev-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fs from "node:fs";
import os from "node:os";
import path, { join, resolve, relative } from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import {
cancel,
confirm,
Expand All @@ -31,9 +30,6 @@ import {
import detectPackageManager from "which-pm-runs";
import yargs from "yargs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export function isHome(dir: string): boolean {
return dir.startsWith("~/");
}
Expand Down
2 changes: 1 addition & 1 deletion libs/create-qwikdev-astro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"outDir": "./dist",
"declaration": true,
"declarationDir": "./dist",
"emitDeclarationOnly": false,
"emitDeclarationOnly": true,
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "Bundler",
Expand Down
Loading

0 comments on commit ccce6ad

Please sign in to comment.