Skip to content

Commit

Permalink
chore: update build script
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Sep 19, 2024
1 parent e682cf0 commit 5ee202f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node.js CI
name: CI

on:
push:
Expand All @@ -25,7 +25,7 @@ jobs:
node-version: ${{ matrix.nodejs }}
- uses: oven-sh/setup-bun@v2
with:
bun-version: 0.8.1
bun-version: 1.1.27

- name: (env) cache
uses: actions/cache@v3
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"test": "uvu -r esm -i util -i bench packages test"
},
"devDependencies": {
"astray": "1.1.1",
"bump": "1.0.0-next.1",
"bundt": "1.1.2",
"esm": "3.2.25",
"httpie": "1.1.2",
"magic-string": "0.30.11",
"oxc-parser": "0.29.0",
"uvu": "0.5.1"
},
"workspaces": [
Expand Down
145 changes: 131 additions & 14 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,137 @@
import { readdir } from 'node:fs/promises';
import { execFileSync } from 'node:child_process';
import { join, resolve } from 'node:path';
import oxc from "oxc-parser";
import MagicString from "magic-string";
import { type ESTreeMap, walk } from "astray";

let bundt = require.resolve('bundt');
let root = resolve('./packages');
import { basename, join, resolve } from "node:path";
import * as fs from "node:fs/promises";

let packages = await readdir(root, {
withFileTypes: true,
});
let root = resolve("packages");
let packages = await fs.readdir(root);

for (let pkg of packages) {
if (pkg.isFile()) continue;
type Entry = `./${string}`;
type Condition = Record<"types" | "default", Entry>;

console.log('>>', pkg.name);
execFileSync(bundt, ['index.js'], {
cwd: join(root, pkg.name),
stdio: 'inherit',
for (let name of packages) {
let dir = join(root, name);

let file = join(dir, "package.json");
let data = await fs.readFile(file, "utf8");
let pkg = JSON.parse(data).exports["."] as {
import: Entry | Condition;
require: Entry | Condition;
};

let outputs: string[] = [];

// ESM <- copy
let entry = join(dir, "index.js");
data = await fs.readFile(entry, "utf8");
let target = (pkg.import as Condition).default || pkg.import;
let outfile = join(dir, target);

await fs.writeFile(outfile, data);
outputs.push(outfile);

// CJS <- transform
data = await fs.readFile(entry, "utf8");
let s = new MagicString(data);
let AST = JSON.parse(
await oxc.parseAsync(data).then((x) => x.program),
);

type Location = {
start: number;
end: number;
};

walk(AST.body, {
ImportDeclaration(n) {
let { start, end } = n as unknown as Location;
let src = n.source as unknown as {
type: "StringLiteral";
value: string;
start: number;
end: number;
};

let from = src.value;
if (from.startsWith("node:")) {
from = from.substring(5);
}

let $locals: string[] = [];
let $default: string | undefined;

let i = 0, arr = n.specifiers;
let tmp: typeof arr[number];

for (; i < arr.length; i++) {
tmp = arr[i];

switch (tmp.type) {
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier": {
if ($default) throw new Error("Double `default` exports!");
$default = tmp.local.name;
break;
}

case "ImportSpecifier": {
let { imported, local } = tmp;
if (imported.name !== local.name) {
$locals.push(`${imported.name}: ${local.name}`);
} else {
$locals.push(local.name);
}
break;
}
}
}

let stmt = "const ";
if ($default) {
stmt += $default;
}
if ($locals.length > 0) {
if ($default) stmt += ", ";
stmt += "{ " + $locals.join(", ") + " }";
}

let qq = s.snip(src.start, src.start + 1);
stmt += ` = require(${qq + from + qq});\n`;
s.overwrite(start, end, stmt);
},
ExportDefaultDeclaration(n) {
let start = (n as unknown as Location).start;
s.overwrite(start, start + "export default".length, "module.exports =");
},
ExportNamedDeclaration(n) {
let start = (n as unknown as Location).start;
let type = n.declaration?.type;
let key: string | undefined;

if (type === "FunctionDeclaration") {
key = (n.declaration as ESTreeMap["FunctionDeclaration"]).id?.name;
} else {
console.log("EXPORT NAMED TYPE?", n.declaration);
}

if (key) {
s.remove(start, start + "export ".length);
s.append(`\nexports.${key} = ${key};`);
}
},
});

target = (pkg.require as Condition).default || pkg.require;
await fs.writeFile(outfile = join(dir, target), s.toString());
outputs.push(outfile);

console.log("\n", name);
for (let tmp of outputs) {
let s = await fs.stat(tmp);
let num = s.size > 1e3 ? (s.size / 1e3).toFixed(2) : s.size;
let size = `(${num} ${s.size > 1e3 ? "kB" : "b"})`;
console.log(" %s %s", basename(tmp), size);
}
}

0 comments on commit 5ee202f

Please sign in to comment.