Skip to content

Commit

Permalink
feat!: Cleaner, lighter weight patch (nonara#66)
Browse files Browse the repository at this point in the history
## Summary

This is not the planned rewrite, but the codebase was in desperate need of an update. The patch build system with rollup was failing with newer versions and was hacky under the best of circumstances, anyway. This new version has a proper custom build system that produces a much leaner patch. Additionally, I reorganized the codebase, improved tests, and dropped old TS support. 

_Note: If you absolutely need it, it will still work with older TS. Simply fork and remove the version check_

## Changes

- Soft deprecated `--basedir` (use `--dir`)
- Zero bundled dependencies in patch (much lighter)
- ⚠️ Hard deprecated `--persist` option (use `package.json` -> `prepare` script)
- ⚠️ Requires TS >= 4.0

_(⚠️ denotes potential "breaking" change)_

## Development Changes

- Removed rollup and created light-weight custom build system
- Cleaned up file structure
- Improved test speed & methodology
- Changed patch detection signature
  • Loading branch information
nonara committed Nov 1, 2021
1 parent 5c47c8f commit 78dcff5
Show file tree
Hide file tree
Showing 76 changed files with 2,848 additions and 4,208 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build (CI)
name: Build

on: [ push ]

Expand All @@ -25,14 +25,9 @@ jobs:
- name: Build
run: yarn build

- name: Test (with coverage)
run: yarn run test:coverage
- name: Test
run: yarn run test
env:
CI: true
FORCE_COLOR: 1
NODE_OPTIONS: --max-old-space-size=4096

- name: Post coverage to Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/ts-patch.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2021 Ron Spickenagel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 changes: 25 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
[![npm version](https://badge.fury.io/js/ts-patch.svg)](https://badge.fury.io/js/ts-patch)
[![NPM Downloads](https://img.shields.io/npm/dm/ts-patch.svg?style=flat)](https://npmjs.org/package/ts-patch)
![Build Status](https://github.com/nonara/ts-patch/workflows/Build%20(CI)/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/nonara/ts-patch/badge.svg?branch=master)](https://coveralls.io/github/nonara/ts-patch?branch=master)

# TS Patch

Directly patch typescript installation to allow custom transformers (plugins).

- Plugins are specified in `tsconfig.json`, or provided programmatically in `CompilerOptions`.
- Based on [ttypescript](https://github.com/cevek/ttypescript) - 100% compatibility in configuration & transformers.
- Based on [ttypescript](https://github.com/cevek/ttypescript) - Fully compatible + offers more features

## Features
* Patch / unpatch any version of typescript (2.7+)
* Patch / unpatch any version of typescript (4.0+)
* Advanced options for patching individual libraries, specific locations, etc. (see `ts-patch /?`)
* _(New)_ Supports 'transforming' the `Program` instance during creation. (see: [Transforming Program](#transforming-program))
* _(New)_ Add, remove, or modify diagnostics! (see: [Altering Diagnostics](#altering-diagnostics))
Expand All @@ -25,11 +24,11 @@ Directly patch typescript installation to allow custom transformers (plugins).

2. Patch typescript
```shell
ts-patch install
# For advanced options, see: ts-patch /?
ts-patch install
```

3. Add `prepare` script (keeps patch persisted after npm installations)
3. Add `prepare` script (keeps patch persisted after npm install)

`package.json`
```jsonc
Expand All @@ -41,16 +40,17 @@ ts-patch install
}
```

## V2 Coming Soon...
## Rewrite Coming Soon...

With a couple years of hindsight, it's time for a much needed redesign to make a more complete plugin ecosystem! The new
design will also be friendlier for the various package management apps.
The development of v2 is already underway! To follow that progress, see [this discussion](https://github.com/nonara/ts-patch/discussions/40).
Development is already underway, but my time to work on it is limited! To follow progress, see [this discussion](https://github.com/nonara/ts-patch/discussions/40).
That said —
- The new v2 will still support all legacy packages and config
- v1 will be maintained and patched for any issues, having an end of life no earlier than 2023.
Notes —
- The new version will be a major release
- It will still support all legacy packages and config
- Previous major version will be maintained and patched for any issues for at least one year
## Table of Contents
- [Configuring](#configuring)
Expand Down Expand Up @@ -80,25 +80,25 @@ Add transformers to `compilerOptions` in `plugins` array.
{
"compilerOptions": {
"plugins": [
// Source Transformer -> 'type' defaults to 'program'
// Source Transformer: 'type' defaults to 'program'
{ "transform": "transformer-module", "someOption1": 123, "someOption2": 321 },
// Source Transformer -> program signature
// Source Transformer: program signature
{ "transform": "./transformers/my-transformer.ts", "type": "program" },
// Source Transformer -> program signature, applies after TS transformers
// Source Transformer: program signature, applies after TS transformers
{ "transform": "transformer-module1", "type": "config", "after": true },
// Source Transformer -> checker signature, applies to TS declarations
// Source Transformer: checker signature, applies to TS declarations
{ "transform": "transformer-module2", "type": "checker", "afterDeclarations": true },
// Source Transformer -> raw signature
// Source Transformer: raw signature
{ "transform": "transformer-module3", "type": "raw" },
// Source Transformer -> compilerOptions signature
// Source Transformer: compilerOptions signature
{ "transform": "transformer-module4", "type": "compilerOptions" },
// Program Transformer -> Only has one signature - notice no type specified, because it does not apply
// Program Transformer: Only has one signature - no type specified, because it does not apply
{ "transform": "transformer-module5", "transformProgram": true }
]
}
Expand Down Expand Up @@ -185,6 +185,8 @@ export default function(program: ts.Program, pluginOptions: any) {

#### Example Node Transformers

[`{ transform: "typescript-transform-paths" }`](https://github.com/LeDDGroup/typescript-transform-paths)

[`{ transform: "typescript-is/lib/transform-inline/transformer" }`](https://github.com/woutervh-/typescript-is)

[`{ transform: "ts-transform-img/dist/transform", type: "config" }`](https://github.com/longlho/ts-transform-img)
Expand All @@ -197,15 +199,14 @@ export default function(program: ts.Program, pluginOptions: any) {

[`{ transform: "typescript-transform-jsx" }`](https://github.com/LeDDGroup/typescript-transform-jsx)

[`{ transform: "typescript-transform-paths" }`](https://github.com/LeDDGroup/typescript-transform-paths)

[`{ transform: "ts-transformer-minify-privates" }`](https://github.com/timocov/ts-transformer-minify-privates)

### Transforming Program

There are some cases where a transformer isn't enough. Several examples are if you want to:
Sometimes you want to do more than just transform source code. For example you may want to:

- TypeCheck code after it's been transformed
- Generate code and add it to the program
- Add or remove emit files during transformation
For this, we've introduced what we call a Program Transformer. The transform action takes place during `ts.createProgram`, and allows
Expand Down Expand Up @@ -266,15 +267,12 @@ To alter diagnostics, use the [program type signature](#program-default), and us

| property | description |
| -------- |----------- |
| diagnostics | Reference to `Diagnostic[]` created during `ts.emitFilesAndReportErrors()` (works with tsc also)
| diagnostics | Reference to `Diagnostic` array
| addDiagnostic() | Directly add `Diagnostic` to `diagnostics` array |
| removeDiagnostic() | Directly remove `Diagnostic` from `diagnostics` array (uses splice, for safe removal)

#### Notes
- This alters diagnostics during _emit only_. If you want to alter diagnostics in your IDE, create a LanguageService plugin
- If an emit method other than `ts.emitFilesAndReportErrors()` is used, any diagnostics added via `addDiagnostic()`
will still be merged into the result of `program.emit() -> diagnostics`
- 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
## Resources
Expand All @@ -289,7 +287,7 @@ will still be merged into the result of `program.emit() -> diagnostics`
| Tool | Type | Description |
| ---- | ---- | ----------- |
| [TS AST Viewer](https://ts-ast-viewer.com/) | Website | Allows you to see the `Node` structure of any TS/JS source, including Flags, `Type`, and `Symbol`. This is the go-to tool for all things TypeScript AST.
| [TS AST Viewer](https://ts-ast-viewer.com/) | Website | Allows you to see the `Node` structure and other TS properties of your source code.
| [ts-query](https://www.npmjs.com/package/@phenomnomnominal/tsquery) | NPM Package | Perform fast CSS-like queries on AST to find specific nodes (by attribute, kind, name, etc)
| [ts-query Playground](https://tsquery-playground.firebaseapp.com/) | Website | Test `ts-query` in realtime
| [ts-expose-internals](https://github.com/nonara/ts-expose-internals) | NPM Package | Exposes internal types and methods of the TS compiler API
Expand All @@ -309,4 +307,4 @@ will still be merged into the result of `program.emit() -> diagnostics`
## License
This project is licensed under the MIT License
This project is licensed under the MIT License, as described in `LICENSE.md`
20 changes: 12 additions & 8 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module.exports = {
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
testEnvironment: "node",
preset: 'ts-jest',
roots: [ './test' ],
roots: [ '<rootDir>/test/tests' ],
testRegex: '.*(test|spec)\\.tsx?$',
moduleFileExtensions: [ 'ts', 'tsx', 'js', 'jsx', 'json', 'node' ],
globals: {
Expand All @@ -10,13 +12,15 @@ module.exports = {
}
},
modulePaths: [ "<rootDir>/node_modules" ],
collectCoverageFrom: [
'src/installer/**/*.ts',
'src/patch/lib/plugin.ts'
],
coveragePathIgnorePatterns: [
'src/installer/lib/system/errors.ts$'
],

testTimeout: 10000
globalSetup: './test/src/setup.ts',
globalTeardown: './test/src/teardown.ts',
testTimeout: 10000,
transformIgnorePatterns: [
'/node_modules/(?!(ts-transformer-keys|ts-transformer-enumerate|ts-nameof)/)'
]
}

export default config;
66 changes: 32 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"clean": "rimraf dist coverage test/**/node_modules && yarn prepare",
"build": "yarn run clean && yarn run compile:installer && yarn run build:patch",
"compile:installer": "tsc -p src/installer",
"build:patch": "cd src/patch && rollup -c",
"compile": "yarn run compile:installer && yarn run compile:patch",
"build": "yarn run clean && yarn run compile:patch && yarn run compile:installer",
"compile:installer": "tsc -p src",
"compile:patch": "tsc -p src/patch",
"------------ ": "-------------",
"clean": "npx rimraf dist coverage *.tsbuildinfo test/.tmp",
"clean:global": "yarn run clean && npx rimraf ./**/node_modules ./**/yarn.lock",
"reset": "yarn run clean:global && yarn install && yarn build",
"------------ ": "-------------",
"test": "jest",
"test:coverage": "jest --coverage",
"------------": "-------------",
"prepare": "node scripts/prepare.js",
"prepare": "ts-patch i -s && yarn prepare:patch && yarn prepare:test",
"prepare:patch": "cd src/patch && yarn install && tsc",
"prepare:test": "cd test && yarn install",
"postbuild": "node scripts/postbuild.js"
},
"private": true,
Expand All @@ -40,55 +45,48 @@
"homepage": "https://github.com/nonara/ts-patch#readme",
"dependencies": {
"chalk": "^4.1.0",
"glob": "^7.1.7",
"global-prefix": "^3.0.0",
"minimist": "^1.2.5",
"resolve": "^1.20.0",
"shelljs": "^0.8.4",
"strip-ansi": "^6.0.0",
"glob": "^7.1.7",
"resolve": "^1.20.0"
"strip-ansi": "^6.0.0"
},
"bin": {
"ts-patch": "./dist/bin/cli.js"
},
"peerDependencies": {
"typescript": ">2.7.0"
"typescript": ">=4.0.0"
},
"devDependencies": {
"@phenomnomnominal/tsquery": "^4.1.0",
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "^10.0.0",
"@types/jest": "^26.0.15",
"@types/minimist": "^1.2.0",
"@types/mock-fs": "^4.10.0",
"@types/node": "^14.0.5",
"@types/resolve": "1.20.0",
"@types/shelljs": "^0.8.6",
"@types/jest": "^27.0.2",
"@types/minimist": "^1.2.2",
"@types/mock-fs": "^4.13.1",
"@types/node": "^16.11.5",
"@types/resolve": "^1.20.1",
"@types/shelljs": "^0.8.9",
"@types/ts-expose-internals": "npm:ts-expose-internals@4.4.4",
"graphql": "^15.4.0",
"graphql-tag": "^2.10.3",
"jest": "^26.0.1",
"jest-mock-process": "^1.4.0",
"mock-fs": "^4.12.0",
"rfdc": "^1.1.4",
"jest": "^27.3.1",
"jest-mock-process": "^1.4.1",
"mock-fs": "^5.1.1",
"rfdc": "^1.3.0",
"rimraf": "^3.0.2",
"rollup": "^2.1.0",
"rollup-plugin-dts": "^2.0.0",
"rollup-plugin-shim": "^1.0.0",
"rollup-plugin-typescript2": "^0.30.0",
"standard-version": "^9.3.0",
"ts-jest": "^26.0.0",
"standard-version": "^9.3.2",
"ts-jest": "^27.0.7",
"ts-nameof": "^5.0.0",
"ts-node": "^9.0.0",
"ts-node": "^10.4.0",
"ts-patch": "latest",
"ts-transform-css-modules": "^0.3.3",
"ts-transform-graphql-tag": "^0.2.1",
"ts-transform-img": "^0.4.2",
"ts-transform-react-intl": "^0.4.1",
"ts-transformer-enumerate": "^0.5.1",
"ts-transformer-keys": "^0.4.3",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.3.5",
"ts-expose-internals": "^4.3.2",
"yarn": "^1.22.4"
"tsconfig-paths": "^3.11.0",
"typescript": "^4.4.4"
},
"directories": {
"resources": "./dist/resources"
Expand Down
30 changes: 0 additions & 30 deletions scripts/build-patch-types.js

This file was deleted.

Loading

0 comments on commit 78dcff5

Please sign in to comment.