Perfective Build for TypeScript
The @perfective/build
package provides base configurations
and presets for tools like Gulp, TypeScript, Babel, etc.
to reduce code duplication between projects setup.
Installation
npm install --save-dev \
@perfective/build \
gulp \
typescript
Building a TypeScript project
-
Check that dev dependencies include correct versions of
gulp
andtypescript
:{ "devDependencies": { "gulp": "^4.0.2", "typescript": "~5.2.2" } }
-
Setup
tsconfig.json
using the basetsconfig.strict.json
configuration.{ "extends": "@perfective/build/tsconfig.strict.json", "compilerOptions": { "rootDir": "./src" }, "exclude": [ "dist" ] }
-
Add the
RequireExtension
plugin to yourbabel.config.js
. This plugin replaces.js
extensions with the.cjs
extensions in the NodeJSrequire()
statements.const perfective = require('@perfective/build/babel'); module.exports = { presets: [], plugins: [ perfective.babelPluginRequireExtension, ] };
NoteThere is also available the
babelPluginImportExtension
plugin. It replaces.js
extensions with.mjs
, but on practice it is not needed: you only need.cjs
for CommonJS and keep.js
for ESM modules.
Setup Prettier
-
Add
prettier
as a dev dependency:{ "devDependencies": { "prettier": "^3.1.0" } }
-
Setup
.prettierrc.js
:const perfective = require('@perfective/build/prettier'); module.exports = perfective;
-
Setup
.prettierignore
:# Build dist # ESLint *.js *.jsx *.ts *.tsx
-
Update
package.json
scripts.{ "scripts": { "lint": "npm run lint:prettier", "lint:prettier": "prettier --write .", "lint:prettier:build": "prettier --check ." } }
Use
lint:prettier
during development (to fix code automatically) andlint:prettier:build
to verify the build (to fail if code is not formatted).
Setup Jest
-
Add
jest
and related peer dependencies as dev dependencies:{ "devDependencies": { "@types/jest": "^29.5.8", "jest": "^29.7.0", "ts-jest": "^29.1.1", } }
-
Setup
jest.config.js
:const perfective = require('@perfective/build/jest'); module.exports = perfective;
-
Update
package.json
scripts:{ "scripts": { "test": "jest", "test:build": "jest --clearCache && jest --collectCoverage" } }
Use
test
for development testing andtest:build
to test during the build (and fail if test coverage is not sufficient).