Gathering detailed insights and metrics for ts-patch
Gathering detailed insights and metrics for ts-patch
Gathering detailed insights and metrics for ts-patch
Gathering detailed insights and metrics for ts-patch
@fastify-dev-toolkit/server
### install ```bash npm install @fastify-dev-toolkit/plugin npm install ts-patch // add to package.json scripts: { "prepare": "ts-patch install -s" } ```
@aliser/ts-transformer-append-js-extension
A TypeScript transformer for use with ts-patch that will append the JS extension to all relative imports that have no extension.
eslint-ts-patch
Support eslint.config.mjs and eslint.config.ts for ESLint
diff-match-patch-ts
Port of diff-match-patch to TypeScript.
Augment the TypeScript compiler to support extended functionality
npm install ts-patch
87.6
Supply Chain
100
Quality
79.8
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
771 Stars
223 Commits
26 Forks
8 Watching
4 Branches
10 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (91.87%)
JavaScript (8.13%)
Cumulative downloads
Total Downloads
Last day
-9.4%
41,480
Compared to previous day
Last week
1.4%
243,742
Compared to previous week
Last month
15.6%
1,003,791
Compared to previous month
Last year
126.6%
6,806,736
Compared to previous year
6
Patch typescript to allow custom transformers (plugins) during build.
Plugins are specified in tsconfig.json
, or provided programmatically in CompilerOptions
.
Migrating from ttypescript is easy! See: Method 1: Live Compiler
ts-patch /?
)Program
(see: Transforming Program)1<yarn|npm|pnpm> add -D ts-patch
The live compiler patches on-the-fly, each time it is run.
Via commandline: Simply use tspc
(instead of tsc
)
With tools such as ts-node, webpack, ts-jest, etc: specify the compiler as ts-patch/compiler
Persistent patch modifies the typescript installation within the node_modules path. It requires additional configuration to remain persisted, but it carries less load time and complexity compared to the live compiler.
1# For advanced options, see: ts-patch /? 2ts-patch install
prepare
script (keeps patch persisted after npm install)package.json
1{ 2 /* ... */ 3 "scripts": { 4 "prepare": "ts-patch install -s" 5 } 6}
tsconfig.json: Add transformers to compilerOptions
in plugins
array.
Examples
1{ 2 "compilerOptions": { 3 "plugins": [ 4 // Source Transformers 5 { "transform": "transformer-module" }, 6 { "transform": "transformer2", "extraOption": 123 }, 7 { "transform": "trans-with-mapping", "resolvePathAliases": true }, 8 { "transform": "esm-transformer", "isEsm": true }, 9 10 // Program Transformer 11 { "transform": "transformer-module5", "transformProgram": true } 12 ] 13 } 14}
Option | Type | Description |
---|---|---|
transform | string | Module name or path to transformer (*.ts or *.js) |
after | boolean | Apply transformer after stock TS transformers |
afterDeclarations | boolean | Apply transformer to declaration (*.d.ts) files |
transformProgram | boolean | Transform Program during ts.createProgram() (see: Program Transformers) |
isEsm | boolean | Transformer is ES Module (note: experimental — requires esm) |
resolvePathAliases | boolean | Resolve path aliases in transformer (requires tsconfig-paths) |
type | string | See: Source Transformer Entry Point (default: 'program') |
import | string | Name of exported transformer function (defaults to default export) |
tsConfig | string | tsconfig.json file for transformer (allows specifying compileOptions, path mapping support, etc) |
... | Provide your own custom options, which will be passed to the transformer |
Note: Required options are bold
For an overview of the typescript compiler (such as what a SourceFile
and Program
is) see: Typescript Compiler Notes.
Source Transformers will transform the AST of SourceFiles during compilation, allowing you to alter the output of the JS or declarations files.
1(program: ts.Program, config: PluginConfig, extras: TransformerExtras) => ts.TransformerFactory
PluginConfig: Type Declaration
TransformerExtras: Type Declaration
ts.TransformerFactory: (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile
Note: Additional legacy signatures are supported, but it is not recommended to develop a new transformer using them.
Transformers can be written in JS or TS.
1import type * as ts from 'typescript'; 2import type { TransformerExtras, PluginConfig } from 'ts-patch'; 3 4/** Changes string literal 'before' to 'after' */ 5export default function (program: ts.Program, pluginConfig: PluginConfig, { ts: tsInstance }: TransformerExtras) { 6 return (ctx: ts.TransformationContext) => { 7 const { factory } = ctx; 8 9 return (sourceFile: ts.SourceFile) => { 10 function visit(node: ts.Node): ts.Node { 11 if (tsInstance.isStringLiteral(node) && node.text === 'before') { 12 return factory.createStringLiteral('after'); 13 } 14 return tsInstance.visitEachChild(node, visit, ctx); 15 } 16 return tsInstance.visitNode(sourceFile, visit); 17 }; 18 }; 19} 20
Live Examples:
{ transform: "typescript-transform-paths" }
{ transform: "typescript-is/lib/transform-inline/transformer" }
{ transform: "typia/lib/transform" }
(💻playground)
{ transform: "@nestia/core/lib/transform" }
Diagnostics can be altered in a Source Transformer.
To alter diagnostics you can use the following, provided from the TransformerExtras
parameter:
property | description |
---|---|
diagnostics | Reference to Diagnostic array |
addDiagnostic() | Safely add Diagnostic to diagnostics array |
removeDiagnostic() | Safely remove Diagnostic from diagnostics array |
This alters diagnostics during emit only. If you want to alter diagnostics in your IDE as well, you'll need to create a LanguageService plugin to accompany your source transformer
Sometimes you want to do more than just transform source code. For example you may want to:
For this, we've introduced what we call a Program Transformer. The transform action takes place during ts.createProgram
, and allows
re-creating the Program
instance that typescript uses.
1(program: ts.Program, host: ts.CompilerHost | undefined, options: PluginConfig, extras: ProgramTransformerExtras) => ts.Program
ProgramTransformerExtras >>> Type Declaration
To configure a Program Transformer, supply "transformProgram": true
in the config transformer entry.
Note: The before
, after
, and afterDeclarations
options do not apply to a Program Transformer and will be ignored
1/** 2 * Add a file to Program 3 */ 4import * as path from 'path'; 5import type * as ts from 'typescript'; 6import type { ProgramTransformerExtras, PluginConfig } from 'ts-patch'; 7 8export const newFile = path.resolve(__dirname, 'added-file.ts'); 9 10export default function ( 11 program: ts.Program, 12 host: ts.CompilerHost | undefined, 13 options: PluginConfig, 14 { ts: tsInstance }: ProgramTransformerExtras 15) { 16 return tsInstance.createProgram( 17 /* rootNames */ program.getRootFileNames().concat([ newFile ]), 18 program.getCompilerOptions(), 19 host, 20 /* oldProgram */ program 21 ); 22}
Note: For a more complete example, see Transforming Program with additional AST transformations
Live Examples:
{ transform: "@typescript-virtual-barrel/compiler-plugin", transformProgram: true }
{ transform: "ts-overrides-plugin", transformProgram: true }
The plugin package configuration allows you to specify custom options for your TypeScript plugin.
This configuration is defined in the package.json
of your plugin under the tsp
property.
An example use case is enabling parseAllJsDoc
if you require full JSDoc parsing in tsc for your transformer in TS v5.3+. (see: 5.3 JSDoc parsing changes)
For all available options, see the PluginPackageConfig
type in plugin-types.ts
1{ 2 "name": "your-plugin-name", 3 "version": "1.0.0", 4 "tsp": { 5 "tscOptions": { 6 "parseAllJsDoc": true 7 } 8 } 9}
Tool | Type | Description |
---|---|---|
TS AST Viewer | Web App | Allows you to see the Node structure and other TS properties of your source code. |
ts-expose-internals | NPM Package | Exposes internal types and methods of the TS compiler API |
#compiler-internals-and-api
on TypeScript Discord Server(env) TSP_SKIP_CACHE
Skips patch cache when patching via cli or live compiler.
(env) TSP_COMPILER_TS_PATH
Specify typescript library path to use for ts-patch/compiler
(defaults to require.resolve('typescript')
)
(env) TSP_CACHE_DIR
Override patch cache directory
(cli) ts-patch clear-cache
Cleans patch cache & lockfiles
Ron S. |
If you're interested in helping and are knowledgeable with the TS compiler codebase, feel free to reach out!
This project is licensed under the MIT License, as described in LICENSE.md
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
2 existing vulnerabilities detected
Details
Reason
Found 6/18 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More