Broccoli TypeScript Compiler
Broccoli TypeScript Compiler (broccoli-typescript-compiler) is a specialized Broccoli plugin designed to integrate TypeScript compilation directly into a Broccoli.js build pipeline. Currently stable at version 8.0.0, this package provides robust tooling for converting TypeScript files into JavaScript, supporting both CommonJS and ES modules targets. It allows for flexible configuration via `tsconfig.json` files or inline objects, enabling developers to define compiler options such as module resolution, target ES version, source maps, and declaration file generation. Key differentiators include its tight integration with the Broccoli ecosystem, offering three distinct usage patterns: a direct compilation function, a class-based compiler for more control, and a filter function that intelligently processes only TypeScript files while passing non-TypeScript assets through. While a specific release cadence isn't published, the project actively maintains compatibility with recent TypeScript versions, ensuring it remains a viable solution for Broccoli-based projects requiring TypeScript support.
Common errors
-
Cannot find module 'typescript' or its corresponding type declarations.
cause The `typescript` package, a peer dependency, is not installed or its version does not meet the specified range.fixInstall the required `typescript` version: `npm install typescript@^4.0.3` (or the version specified in peerDependencies) in your project. -
TypeScript compilation failed: [ERROR_MESSAGE]
cause `throwOnError` is enabled (either explicitly or due to `NODE_ENV='production'`), and TypeScript reported errors during compilation.fixReview the `[ERROR_MESSAGE]` output for specific TypeScript errors. Fix the TypeScript code issues, or set `throwOnError: false` (being mindful of the `NODE_ENV` default override) if you want to allow the build to proceed with errors. -
Error: The 'tsconfig' option must be a string path or a config object.
cause The `tsconfig` option was provided in an incorrect format or was missing required properties.fixEnsure `tsconfig` is either an absolute path string to a `tsconfig.json` file or a valid JavaScript object matching the structure of `tsconfig.json`'s `compilerOptions` and other root-level properties.
Warnings
- gotcha The `throwOnError` option defaults to `true` when `process.env.NODE_ENV` is set to 'production', even if explicitly set to `false` in configuration. This can cause unexpected build failures in production environments if not anticipated.
- breaking Upgrading the peer dependency `typescript` to a new major version often requires specific maintenance steps, including updating submodules and running `yarn run generate-tsconfig-interface`. Neglecting these steps can lead to compilation issues or incorrect type resolution.
- gotcha This package is a Broccoli plugin and is only useful within a Broccoli.js build pipeline. It is not a standalone TypeScript compiler for general-purpose use outside of the Broccoli ecosystem.
Install
-
npm install broccoli-typescript-compiler -
yarn add broccoli-typescript-compiler -
pnpm add broccoli-typescript-compiler
Imports
- default
const typescript = require('broccoli-typescript-compiler');import typescript from 'broccoli-typescript-compiler';
- TypescriptCompiler
const TypescriptCompiler = require('broccoli-typescript-compiler').TypescriptCompiler;import { TypescriptCompiler } from 'broccoli-typescript-compiler'; - filterTypescript
const filterTypescript = require('broccoli-typescript-compiler').filterTypescript;import { filterTypescript } from 'broccoli-typescript-compiler';
Quickstart
import typescript from 'broccoli-typescript-compiler';
import funnel from 'broccoli-funnel';
import mergeTrees from 'broccoli-merge-trees';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const inputTree = funnel('.', { exclude: ['node_modules/**', 'dist/**'] });
const compiledJS = typescript(inputTree, {
tsconfig: {
compilerOptions: {
module: 'commonjs',
target: 'es2018',
moduleResolution: 'node',
newLine: 'LF',
rootDir: '.', // Assuming root directory
outDir: 'dist',
sourceMap: true,
declaration: true,
},
files: ['index.ts', 'src/**/*.ts'], // Adjust files as needed for your project
},
throwOnError: process.env.NODE_ENV === 'production',
annotation: 'Compile application TypeScript',
});
// Example of merging compiled JS with other assets if needed
export default mergeTrees([compiledJS /*, otherTrees */], { overwrite: true });