Build is a powerful tool that compiles all your TypeScript files into
JavaScript, leveraging the speed of ESBuild and the type-checking
capabilities of the TypeScript compiler.
Feature 📦
- Fast compilation using
ESBuild
TypeScript support with type-checking (tsc & tsc-alias)
- Watch mode for development (
--Watch)
- Customizable
ESBuild configuration via object or function (--ESBuild)
- Supports both
CommonJS and ES modules
- Glob pattern support for input files
- Exclusion patterns for input files
Installation 🚀
Install the package as a development dependency:
npm install -D -E @playform/build
Usage 🛠️
Command Line
Run the build tool from the command line, providing file patterns:
npx @playform/build 'Source/**/*.ts' 'OtherSource/**/*.mts'
CLI Options
Usage: Build [options] <File...>
Arguments:
File One or more glob patterns matching files to build 📝
Options:
-V, --version Output the version number
-E, --ESBuild <File> Path to a custom ESBuild configuration file (exports an object or a function) 📜
-T, --TypeScript <File> Path to a custom TypeScript configuration file (default: "tsconfig.json") 📜
-W, --Watch Enable watch mode: rebuild on file changes 👁️
-h, --help Display help information
NPM Scripts
Add Build to your package.json scripts:
{
"scripts": {
"Build": "Build 'Source/**/*.ts'",
"Run": "Build 'Source/**/*.ts' --Watch",
"prepublishOnly": "Build 'Source/**/*.ts'"
}
}
Configuration ⚙️
ESBuild Configuration (--ESBuild) 📜
You can provide a custom configuration file to modify the default ESBuild
options used by @playform/build. This file (e.g., ESBuild.js or
esbuild.config.ts) should use export default with either a configuration
object or a function.
1. Exporting an Object:
Provide an object with ESBuild options. These will be merged with the base
configuration.
const config = {
minify: true,
sourcemap: "external",
platform: "node",
};
export default config;
2. Exporting a Function:
Provide a function that receives the current ESBuild configuration object
(BuildOptions from esbuild) as its argument. You can modify this object
directly or return a new (partial) object containing options to be merged.
This allows for dynamic configuration, such as filtering entry points.
export default (Current) => {
console.log("Applying custom ESBuild function...");
if (Array.isArray(Current.entryPoints)) {
Current.entryPoints = Current.entryPoints.filter(
(entry) => !/\.(test|spec)\.[mc]?[jt]s$/.test(entry),
);
console.log("Filtered entry points:", Current.entryPoints);
}
Current.define = {
...(Current.define ?? {}),
"process.env.BUILD_TIME": `"${new Date().toISOString()}"`,
};
return {
banner: {
js: "// Generated by @playform/build",
},
logLevel: "info",
};
};
Usage:
Specify the path to your configuration file using the --ESBuild option:
npx @playform/build 'Source/**/*.ts' --ESBuild ESBuild.ts
See the base configuration used internally in
Source/Variable/ESBuild.ts (link might need
adjustment based on your actual path).
TypeScript Configuration (--TypeScript) 📜
By default, Build looks for tsconfig.json in the current working directory.
You can specify a different path using the --TypeScript option. This
tsconfig.json is used by ESBuild for path resolution and by the tsc and
tsc-alias commands run after the build for type checking and path alias
replacement.
A typical tsconfig.json might look like this:
{
"compilerOptions": {
"outDir": "Target",
"rootDir": "Source",
"baseUrl": ".",
"paths": {
"@/*": ["Source/*"]
},
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["Source/**/*"],
"exclude": ["node_modules", "Target"]
}
Important: Ensure compilerOptions.outDir in your tsconfig.json aligns
with the output directory configured for ESBuild (either via the default
Source/Variable/ESBuild.ts or your custom --ESBuild config). Also, set
"noEmit": true as esbuild handles the file generation; tsc is only used
for type checking here.
JSConfig Configuration (Optional) 📜
If you are working with JavaScript and JSDoc for type checking, you can use a
jsconfig.json file instead of tsconfig.json. The principles are similar.
{
"compilerOptions": {
"outDir": "Target",
"rootDir": "Source",
"checkJs": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"@/*": ["Source/*"]
},
"noEmit": true
},
"extends": "@playform/build/jsconfig",
"include": ["Source/**/*"],
"exclude": ["node_modules", "Target"]
}
Remember to pass the path to this file if it's not named tsconfig.json (even
if it's a JS project, the default flag might still look for tsconfig.json
unless you explicitly pass --TypeScript jsconfig.json).
Contributing 🤝
Contributions are welcome! Please see CONTRIBUTING.md for
guidelines and feel free to submit a Pull Request.
Changelog
See CHANGELOG.md for a history of changes to this component.