Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow customizing tsc, tsup builds #2060

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"title-case": "^4.3.2",
"trash-cli": "^6.0.0",
"zod": "^3.24.2",
"zod-package-json": "^1.1.0"
"zod-package-json": "^1.1.0",
"zod-tsconfig": "^0.1.1"
},
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "4.4.1",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 3 additions & 24 deletions src/blocks/blockTSup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,7 @@ describe("blockTSup", () => {
"files": {
"tsup.config.ts": "import { defineConfig } from "tsup";

export default defineConfig({
bundle: false,
clean: true,
dts: true,
entry: ["src/**/*.ts"],
format: "esm",
outDir: "lib",
});
export default defineConfig({"bundle":false,"clean":true,"dts":true,"entry":["src/**/*.ts"],"format":"esm","outDir":"lib"});
",
},
"scripts": undefined,
Expand Down Expand Up @@ -214,14 +207,7 @@ describe("blockTSup", () => {
"files": {
"tsup.config.ts": "import { defineConfig } from "tsup";

export default defineConfig({
bundle: false,
clean: true,
dts: true,
entry: ["src/**/*.ts"],
format: "esm",
outDir: "lib",
});
export default defineConfig({"bundle":false,"clean":true,"dts":true,"entry":["src/**/*.ts"],"format":"esm","outDir":"lib"});
",
},
}
Expand Down Expand Up @@ -310,14 +296,7 @@ describe("blockTSup", () => {
"files": {
"tsup.config.ts": "import { defineConfig } from "tsup";

export default defineConfig({
bundle: false,
clean: true,
dts: true,
entry: ["src/**/*.ts","!src/**/*.test.ts"],
format: "esm",
outDir: "lib",
});
export default defineConfig({"bundle":false,"clean":true,"dts":true,"entry":["src/**/*.ts","!src/**/*.test.ts"],"format":"esm","outDir":"lib"});
",
},
"scripts": undefined,
Expand Down
22 changes: 12 additions & 10 deletions src/blocks/blockTSup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export const blockTSup = base.createBlock({
},
addons: {
entry: z.array(z.string()).default([]),
properties: z.record(z.unknown()).default({}),
runInCI: z.array(z.string()).default([]),
},
produce({ addons, options }) {
const { entry } = addons;
const { entry, properties, runInCI } = addons;

return {
addons: [
Expand Down Expand Up @@ -53,7 +54,7 @@ pnpm build --watch
name: "Build",
steps: [
{ run: "pnpm build" },
...addons.runInCI.map((run) => ({ run })),
...runInCI.map((run) => ({ run })),
],
},
],
Expand All @@ -78,14 +79,15 @@ pnpm build --watch
files: {
"tsup.config.ts": `import { defineConfig } from "tsup";

export default defineConfig({
bundle: false,
clean: true,
dts: true,
entry: ${JSON.stringify(["src/**/*.ts", ...entry])},
format: "esm",
outDir: "lib",
});
export default defineConfig(${JSON.stringify({
bundle: false,
clean: true,
dts: true,
entry: ["src/**/*.ts", ...entry],
format: "esm",
outDir: "lib",
...properties,
})});
`,
},
scripts: options.bin
Expand Down
154 changes: 153 additions & 1 deletion src/blocks/blockTypeScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { blockTypeScript } from "./blockTypeScript.js";
import { optionsBase } from "./options.fakes.js";

describe("blockTypeScript", () => {
test("without options.bin", () => {
test("without addons or options", () => {
const creation = testBlock(blockTypeScript, {
options: optionsBase,
});
Expand Down Expand Up @@ -152,6 +152,158 @@ describe("blockTypeScript", () => {
`);
});

test("with addons", () => {
const creation = testBlock(blockTypeScript, {
addons: {
compilerOptions: {
strictBindCallApply: false,
},
},
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": {
"Type Checking": {
"contents": "
You should be able to see suggestions from [TypeScript](https://typescriptlang.org) in your editor for all open files.

However, it can be useful to run the TypeScript command-line (\`tsc\`) to type check all files in \`src/\`:

\`\`\`shell
pnpm tsc
\`\`\`

Add \`--watch\` to keep the type checker running in a watch mode that updates the display as you save files:

\`\`\`shell
pnpm tsc --watch
\`\`\`
",
},
},
},
"block": [Function],
},
{
"addons": {
"files": {
"greet.ts": "import { GreetOptions } from "./types.js";

export function greet(options: GreetOptions | string) {
const {
logger = console.log.bind(console),
message,
times = 1,
} = typeof options === "string" ? { message: options } : options;

for (let i = 0; i < times; i += 1) {
logger(message);
}
}
",
"index.ts": "export * from "./greet.js";
export * from "./types.js";
",
"types.ts": "export interface GreetOptions {
logger?: (message: string) => void;
message: string;
times?: number;
}
",
},
},
"block": [Function],
},
{
"addons": {
"ignores": [
"/lib",
],
},
"block": [Function],
},
{
"addons": {
"jobs": [
{
"name": "Type Check",
"steps": [
{
"run": "pnpm tsc",
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"ignores": [
"lib/",
],
},
"block": [Function],
},
{
"addons": {
"properties": {
"devDependencies": {
"typescript": "5.8.2",
},
"files": [
"lib/",
],
"scripts": {
"tsc": "tsc",
},
},
},
"block": [Function],
},
{
"addons": {
"coverage": {
"include": [
"src",
],
},
"exclude": [
"lib",
],
},
"block": [Function],
},
{
"addons": {
"debuggers": [],
"settings": {
"typescript.tsdk": "node_modules/typescript/lib",
},
"tasks": [
{
"detail": "Build the project",
"label": "build",
"script": "build",
"type": "npm",
},
],
},
"block": [Function],
},
],
"files": {
"tsconfig.json": "{"compilerOptions":{"declaration":true,"declarationMap":true,"esModuleInterop":true,"module":"NodeNext","moduleResolution":"NodeNext","noEmit":true,"resolveJsonModule":true,"skipLibCheck":true,"strict":true,"target":"ES2022","strictBindCallApply":false},"include":["src"]}",
},
}
`);
});

test("with options.bin", () => {
const creation = testBlock(blockTypeScript, {
options: {
Expand Down
9 changes: 8 additions & 1 deletion src/blocks/blockTypeScript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CompilerOptionsSchema } from "zod-tsconfig";

import { base } from "../base.js";
import { getPackageDependencies } from "../data/packageData.js";
import { getPrimaryBin } from "./bin/getPrimaryBin.js";
Expand All @@ -15,7 +17,11 @@ export const blockTypeScript = base.createBlock({
about: {
name: "TypeScript",
},
produce({ options }) {
addons: {
compilerOptions: CompilerOptionsSchema.optional(),
},
produce({ addons, options }) {
const { compilerOptions } = addons;
const primaryBin = getPrimaryBin(options.bin, options.repository);

return {
Expand Down Expand Up @@ -126,6 +132,7 @@ export * from "./types.js";
skipLibCheck: true,
strict: true,
target: "ES2022",
...compilerOptions,
},
include: ["src"],
}),
Expand Down