A custom Angular builder for compiling and bundling Angular schematics.
- Overview
- Getting Started
- Usage
- Examples
- Features
- Build Process
- Output Structure
- Requirements
- Troubleshooting
- License
- Contributing
This project provides a builder for Angular schematics that:
- Compiles TypeScript files using a specified tsconfig
- Cleans the output directory
- Copies specified project files to the output directory
- Prepares a clean package.json for distribution
(skip if you have one)
Install cli globally
npm install -g @angular-devkit/schematics-cli
Create a new project
schematics blank --name=new-project
npm i -D @angular/cli ngx-schematic-builder
Create or edit (if you have) angular.json
file and put build
prop in projects.*project name*.architect
.
You can copy the configuration from below.
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"projects": {
"schematic": {
"projectType": "library",
"root": "",
"sourceRoot": "src",
"architect": {
"build": {
"builder": "ngx-schematic-builder:build",
"options": {
"files": ["src/**", "LICENSE", "README.md"],
"tsConfig": "tsconfig.json"
}
}
}
}
}
}
Option | Type | Description | Required |
---|---|---|---|
tsConfig |
string |
Path to the TypeScript configuration file | Yes |
files |
string[] |
Array of files to copy to the output directory | No |
{
"compilerOptions": {
"baseUrl": "tsconfig",
"target": "es6",
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"sourceMap": true,
"strictNullChecks": true,
"types": ["node"],
"outDir": "dist" // Add this
}
}
Change package.json the "schematic" property from this
{
"schematics": "./src/collection.json"
}
To this:
{
"schematics": "./collection.json"
}
Why? After build, there is no src
folder, and dist files are directly next to the package.json file.
Run command:
ng build
After running the build command, your output directory (as specified in your tsconfig) will contain:
- Compiled JavaScript files from your TypeScript source
- Declaration files (.d.ts)
- A cleaned package.json (without scripts and devDependencies)
- Any additional files specified in the
files
option
@ng-zen/cli
- NPM Package: https://www.npmjs.com/package/@ng-zen/cli
- Example implementation: https://github.com/kstepien3/ng-zen/blob/master/angular.json
- TypeScript Compilation: Compiles your TypeScript files according to the specified tsconfig
- Output Cleaning: Ensures a clean output directory before building
- File Copying: Copies specified project files to the output directory
- Package.json Optimization: Removes development-related configurations (scripts, devDependencies) from the output package.json
The builder executes the following steps:
- Determines the output directory from the tsconfig
- Cleans the output directory
- Compiles TypeScript files
- Copies specified project files to the output directory
- Optimizes package.json by removing scripts and devDependencies
Issue: Error: Unable to locate or read the "schematic" file specified in package.json.
You might need to remove src
path
Solution: If your tsconfig file has "rootDir": "src"
& "outDir": "dist"
, make you sure your package.json file should looks like this:
{
// ...
"schematics": "./collection.json",
// ...
}
- Node.js (v14.0.0 or later)
- Angular CLI (v12.0.0 or later)
- TypeScript (v4.0.0 or later)
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open a Pull Request