build: use tsup instead of esbuild for packages (@fehmer) (#6309)

This commit is contained in:
Christian Fehmer 2025-03-03 13:48:50 +01:00 committed by GitHub
parent 4a22c0647b
commit 2b2d1a153e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 503 additions and 238 deletions

View file

@ -2,25 +2,25 @@
"name": "@monkeytype/contracts",
"private": true,
"scripts": {
"dev": "rimraf ./dist && monkeytype-esbuild --watch",
"build": "rimraf ./dist && npm run madge && monkeytype-esbuild",
"dev": "tsup-node --watch",
"build": "npm run madge && tsup-node",
"test": "vitest run",
"madge": " madge --circular --extensions ts ./src",
"ts-check": "tsc --noEmit",
"lint": "eslint \"./**/*.ts\""
},
"dependencies": {
"peerDependencies": {
"@ts-rest/core": "3.51.0",
"zod": "3.23.8"
},
"devDependencies": {
"@monkeytype/esbuild": "workspace:*",
"@monkeytype/eslint-config": "workspace:*",
"@monkeytype/tsup-config": "workspace:*",
"@monkeytype/typescript-config": "workspace:*",
"chokidar": "3.6.0",
"eslint": "8.57.1",
"madge": "8.0.0",
"rimraf": "6.0.1",
"tsup": "8.4.0",
"typescript": "5.5.4",
"vitest": "2.1.9"
},
@ -28,12 +28,12 @@
".": {
"types": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
"require": "./dist/index.js"
},
"./*": {
"types": "./src/*.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.cjs"
"require": "./dist/*.js"
}
}
}

View file

@ -0,0 +1,3 @@
import { extendConfig } from "@monkeytype/tsup-config";
export default extendConfig();

View file

@ -1,95 +0,0 @@
const esbuild = require("esbuild");
const { readdirSync, statSync } = require("fs");
const { join, extname } = require("path");
const chokidar = require("chokidar");
//check if watch parameter is passed
const isWatch = process.argv.includes("--watch");
// Recursive function to get all .ts files in a directory
const getAllFiles = (dirPath, arrayOfFiles = []) => {
const files = readdirSync(dirPath);
files.forEach((file) => {
const filePath = join(dirPath, file);
if (statSync(filePath).isDirectory()) {
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
} else if (extname(file) === ".ts") {
arrayOfFiles.push(filePath);
}
});
return arrayOfFiles;
};
// Get all TypeScript files from the src directory and subdirectories
const entryPoints = getAllFiles("./src");
// Function to generate output file names
const getOutfile = (entryPoint, format) => {
const relativePath = entryPoint.replace(/src[/\\]/, "");
const fileBaseName = relativePath.replace(".ts", "");
return `./dist/${fileBaseName}.${format === "esm" ? "mjs" : "cjs"}`;
};
// Common build settings
const commonSettings = {
bundle: true,
sourcemap: true,
minify: true,
};
function buildAll(silent, stopOnError) {
console.log("Building all files...");
entryPoints.forEach((entry) => {
build(entry, silent, stopOnError);
});
}
function build(entry, silent, stopOnError) {
if (!silent) console.log("Building", entry);
// ESM build
esbuild
.build({
...commonSettings,
entryPoints: [entry],
format: "esm",
outfile: getOutfile(entry, "esm"),
})
.catch((e) => {
console.log(`Failed to build ${entry} to ESM:`, e);
if (stopOnError) process.exit(1);
});
// CommonJS build
esbuild
.build({
...commonSettings,
entryPoints: [entry],
format: "cjs",
outfile: getOutfile(entry, "cjs"),
})
.catch((e) => {
console.log(`Failed to build ${entry} to CJS:`, e);
if (stopOnError) process.exit(1);
});
}
if (isWatch) {
buildAll(true, false);
console.log("Starting watch mode...");
chokidar.watch("./src/**/*.ts").on(
"change",
(_path) => {
console.log("File change detected...");
// build(path, false, false);
buildAll(false, false);
},
{
ignoreInitial: true,
}
);
} else {
buildAll(false, true);
}

View file

@ -1,14 +0,0 @@
{
"name": "@monkeytype/esbuild",
"private": true,
"scripts": {
"lint": "eslint \"./**/*.js\""
},
"bin": {
"monkeytype-esbuild": "./index.js"
},
"devDependencies": {
"esbuild": "0.25.0",
"eslint": "8.57.1"
}
}

View file

@ -2,20 +2,20 @@
"name": "@monkeytype/funbox",
"private": true,
"scripts": {
"dev": "rimraf ./dist && monkeytype-esbuild --watch",
"build": "rimraf ./dist && npm run madge && monkeytype-esbuild",
"dev": "tsup-node --watch",
"build": "npm run madge && tsup-node",
"madge": " madge --circular --extensions ts ./src",
"ts-check": "tsc --noEmit",
"lint": "eslint \"./**/*.ts\""
},
"devDependencies": {
"@monkeytype/esbuild": "workspace:*",
"@monkeytype/eslint-config": "workspace:*",
"@monkeytype/tsup-config": "workspace:*",
"@monkeytype/typescript-config": "workspace:*",
"chokidar": "3.6.0",
"eslint": "8.57.1",
"madge": "8.0.0",
"rimraf": "6.0.1",
"tsup": "8.4.0",
"typescript": "5.5.4",
"vitest": "2.1.9"
},
@ -26,7 +26,7 @@
".": {
"types": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
"require": "./dist/index.js"
}
}
}

View file

@ -0,0 +1,3 @@
import { extendConfig } from "@monkeytype/tsup-config";
export default extendConfig(() => ({ entry: ["src/index.ts"] }));

View file

@ -0,0 +1,24 @@
{
"name": "@monkeytype/tsup-config",
"private": true,
"scripts": {
"dev": "tsup-node --watch",
"build": "tsup-node",
"ts-check": "tsc --noEmit"
},
"peerDependencies": {
"tsup": "8.4.0"
},
"devDependencies": {
"@monkeytype/typescript-config": "workspace:*",
"eslint": "8.57.1",
"typescript": "5.5.4"
},
"exports": {
".": {
"types": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
}
}

View file

@ -0,0 +1,23 @@
import { defineConfig, Options } from "tsup";
export function extendConfig(
customizer?: (options: Options) => Options
// tsup uses MaybePromise which is not exported
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): (options: Options) => any {
return (options) => {
const overrideOptions = customizer?.(options);
const config: Options = {
entry: ["src/**/*.ts"],
splitting: false,
sourcemap: true,
clean: !(options.watch === true || options.watch === "true"),
format: ["cjs", "esm"],
dts: false,
minify: true,
...(overrideOptions || {}),
};
return defineConfig(config);
};
}

View file

@ -0,0 +1,15 @@
{
"extends": "@monkeytype/typescript-config/base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
"declarationMap": true,
"moduleResolution": "Node",
"module": "ES6",
"target": "ES2015",
"lib": ["es2016"]
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

View file

@ -0,0 +1,10 @@
import { defineConfig } from "tsup";
export default defineConfig((_options) => ({
entry: ["src/index.ts"],
splitting: false,
sourcemap: false,
clear: !_options.watch,
format: ["cjs", "esm"],
dts: false,
}));

View file

@ -1,36 +1,30 @@
{
"name": "@monkeytype/util",
"private": true,
"scripts": {
"dev": "rimraf ./dist && monkeytype-esbuild --watch",
"build": "rimraf ./dist && npm run madge && monkeytype-esbuild",
"dev": "tsup-node --watch",
"build": "npm run madge && tsup-node",
"test": "vitest run",
"madge": " madge --circular --extensions ts ./src",
"ts-check": "tsc --noEmit",
"lint": "eslint \"./**/*.ts\""
},
"devDependencies": {
"@monkeytype/esbuild": "workspace:*",
"@monkeytype/eslint-config": "workspace:*",
"@monkeytype/tsup-config": "workspace:*",
"@monkeytype/typescript-config": "workspace:*",
"chokidar": "3.6.0",
"eslint": "8.57.1",
"madge": "8.0.0",
"rimraf": "6.0.1",
"tsup": "8.4.0",
"typescript": "5.5.4",
"vitest": "2.1.9",
"zod": "3.23.8"
},
"exports": {
".": {
"types": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./*": {
"types": "./src/*.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.cjs"
"require": "./dist/*.js"
}
}
}

View file

@ -0,0 +1,3 @@
import { extendConfig } from "@monkeytype/tsup-config";
export default extendConfig();

511
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff