Skip to content

Commit

Permalink
initial impl
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Dec 2, 2023
0 parents commit 5776893
Show file tree
Hide file tree
Showing 5 changed files with 790 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
/dist

96 changes: 96 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { writeFile } from "fs/promises";
import { createContext } from "./src/index.ts";
import tsc from "tsc-prog";

const isDev = process.argv.includes("--dev");

const buildContext = createContext({
bundle: true,
});

const common = {
external: ["tiny-glob", "defu", "esbuild"],
entryPoints: ["./src/index.js"],
platform: "node",
target: "node14",
format: "esm",
};

buildContext.config({
...common,
outdir: "./dist/esm",
format: "esm",
outExtension: {
".js": ".mjs",
},
});

buildContext.config({
...common,
outdir: "./dist/cjs",
format: "cjs",
outExtension: {
".js": ".js",
},
});

buildContext.on("error", (errors) => {
errors.map((x) => process.stdout.write(x.reason.toString() + "\n"));
});

buildContext.on("build", async () => {
process.stdout.write("[custom-builder] Built\n");
await writeFile(
"./dist/cjs/package.json",
JSON.stringify({ type: "commonjs" }),
"utf8"
);

await writeFile(
"./dist/esm/package.json",
JSON.stringify({ type: "module" }),
"utf8"
);

tsc.build({
basePath: process.cwd(),
compilerOptions: {
rootDir: "src",
outDir: "./dist/types",
declaration: true,
moduleResolution: "nodenext",
target: "esnext",
module: "NodeNext",
emitDeclarationOnly: true,
skipLibCheck: true,
},
include: ["src/**/*"],
exclude: ["**/*.test.ts", "**/*.spec.ts"],
});

if (isDev) return;

process.exit(0);
});

function createChain() {
let agg = Promise.resolve();
const _chainer = (fn) => {
agg = agg.then(fn);
};
_chainer.value = async () => {
await agg;
return null;
};
return _chainer;
}

const chain = createChain();

if (isDev) {
chain(() => buildContext.watch());
}

chain(() => buildContext.build());

await chain.value;
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "esbuild-multicontext",
"version": "0.0.0",
"license": "MIT",
"type": "module",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
},
"./cjs": "./dist/esm/index.js",
"./esm": "./dist/esm/index.mjs",
"./package.json": "./package.json"
},
"scripts": {
"build": "tsx build.js",
"dev": "tsx build.js --dev"
},
"dependencies": {
"defu": "^6.1.3",
"esbuild": "^0.19.8",
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"@barelyhuman/prettier-config": "^1.1.0",
"@types/node": "^20.10.2",
"prettier": "^3.1.0",
"tsc-prog": "^2.3.0",
"tsx": "^4.6.1",
"typescript": "^5.3.2"
}
}
Loading

0 comments on commit 5776893

Please sign in to comment.