build: update tsconfig and tsup sample configuration

- Include tests in tsconfig for better type checking
- Update tsup.samples.config.ts to use double quotes
- Add esbuild alias for `@` in sample builds
This commit is contained in:
hypercross 2026-04-19 17:16:21 +08:00
parent 601eb0f417
commit 25b44fd6d1
2 changed files with 41 additions and 27 deletions

View File

@ -11,12 +11,12 @@
"declaration": true, "declaration": true,
"declarationMap": true, "declarationMap": true,
"outDir": "./dist", "outDir": "./dist",
"rootDir": "./src",
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@/*": ["src/*"] "@/*": ["src/*"],
} },
}, },
"include": ["src/**/*"], "include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules", "dist", "tests"] "exclude": ["node_modules", "dist"],
} }

View File

@ -1,13 +1,13 @@
import { defineConfig } from 'tsup'; import { defineConfig } from "tsup";
import { fileURLToPath } from 'url'; import { fileURLToPath } from "url";
import * as fs from 'fs'; import * as fs from "fs";
import * as path from 'path'; import * as path from "path";
import {csvLoader} from 'inline-schema/csv-loader/esbuild'; import { csvLoader } from "inline-schema/csv-loader/esbuild";
import {yarnSpinnerPlugin} from 'yarn-spinner-loader/esbuild'; import { yarnSpinnerPlugin } from "yarn-spinner-loader/esbuild";
import type { Plugin } from 'esbuild'; import type { Plugin } from "esbuild";
const srcDir = fileURLToPath(new URL('./src', import.meta.url)); const srcDir = fileURLToPath(new URL("./src", import.meta.url));
const samplesDir = fileURLToPath(new URL('./src/samples', import.meta.url)); const samplesDir = fileURLToPath(new URL("./src/samples", import.meta.url));
// Auto-discover samples entry points // Auto-discover samples entry points
function getSamplesEntries(): Record<string, string> { function getSamplesEntries(): Record<string, string> {
@ -18,13 +18,13 @@ function getSamplesEntries(): Record<string, string> {
const fullPath = path.join(samplesDir, item); const fullPath = path.join(samplesDir, item);
if (fs.statSync(fullPath).isDirectory()) { if (fs.statSync(fullPath).isDirectory()) {
// Directory sample (e.g. boop) - look for index.ts // Directory sample (e.g. boop) - look for index.ts
const indexPath = path.join(fullPath, 'index.ts'); const indexPath = path.join(fullPath, "index.ts");
if (fs.existsSync(indexPath)) { if (fs.existsSync(indexPath)) {
entries[item] = indexPath; entries[item] = indexPath;
} }
} else if (item.endsWith('.ts')) { } else if (item.endsWith(".ts")) {
// Single file sample (e.g. tic-tac-toe.ts) // Single file sample (e.g. tic-tac-toe.ts)
entries[item.replace('.ts', '')] = fullPath; entries[item.replace(".ts", "")] = fullPath;
} }
} }
return entries; return entries;
@ -37,20 +37,20 @@ const samplesEntries = getSamplesEntries();
*/ */
function rewriteBoardgameImports(): Plugin { function rewriteBoardgameImports(): Plugin {
return { return {
name: 'rewrite-boardgame-imports', name: "rewrite-boardgame-imports",
setup(build) { setup(build) {
build.onResolve({ filter: /^@\/(core|utils)\// }, args => { build.onResolve({ filter: /^@\/(core|utils)\// }, (args) => {
// Mark these as external and rewrite to 'boardgame-core' // Mark these as external and rewrite to 'boardgame-core'
return { return {
path: 'boardgame-core', path: "boardgame-core",
external: true, external: true,
}; };
}); });
// Also handle @/index imports // Also handle @/index imports
build.onResolve({ filter: /^@\/index$/ }, args => { build.onResolve({ filter: /^@\/index$/ }, (args) => {
return { return {
path: 'boardgame-core', path: "boardgame-core",
external: true, external: true,
}; };
}); });
@ -60,11 +60,25 @@ function rewriteBoardgameImports(): Plugin {
export default defineConfig({ export default defineConfig({
entry: samplesEntries, entry: samplesEntries,
format: ['esm'], format: ["esm"],
dts: true, dts: true,
clean: true, clean: true,
sourcemap: true, sourcemap: true,
outDir: 'dist/samples', outDir: "dist/samples",
external: ['@preact/signals-core', 'mutative', 'inline-schema', 'boardgame-core'], external: [
esbuildPlugins: [csvLoader({ writeToDisk: true }), rewriteBoardgameImports(), yarnSpinnerPlugin()], "@preact/signals-core",
"mutative",
"inline-schema",
"boardgame-core",
],
esbuildPlugins: [
csvLoader({ writeToDisk: true }),
rewriteBoardgameImports(),
yarnSpinnerPlugin(),
],
esbuildOptions(options) {
options.alias = {
"@": srcDir,
};
},
}); });