Skip to content

Commit

Permalink
add support ts3
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFuks committed Jun 22, 2024
1 parent 385cac3 commit 05ea577
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/2b0dfbff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
ts-overrides-plugin: minor
3 changes: 3 additions & 0 deletions packages/example-ts3/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
}
18 changes: 18 additions & 0 deletions packages/example-ts3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ts-overrides-plugin example

Before all:

```bash
yarn ts-patch install
```

Commands:

```bash
yarn build:tsc
yarn build:ts-loader
yarn build:fork-ts
yarn watch:tsc
yarn watch:ts-loader
yarn watch:fork-ts
```
29 changes: 29 additions & 0 deletions packages/example-ts3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "example-ts3",
"scripts": {
"build:tsc": "tsc",
"build:ts-loader": "webpack --config webpack.config.ts-loader.js",
"build:fork-ts": "webpack --config webpack.config.fork-ts.js",
"watch:tsc": "tsc --watch",
"watch:ts-loader": "webpack --config webpack.config.ts-loader.js --watch",
"watch:fork-ts": "webpack --config webpack.config.fork-ts.js --watch",
"prepare": "ts-patch install -s"
},
"devDependencies": {
"@babel/core": "^7.23.6",
"@babel/preset-env": "^7.23.6",
"@babel/preset-typescript": "^7.23.3",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"babel-loader": "^9.1.3",
"fork-ts-checker-webpack-plugin": "^9.0.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"ts-overrides-plugin": "workspce:*",
"ts-patch": "1.4.5",
"typescript": "3.9.10",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"packageManager": "[email protected]"
}
4 changes: 4 additions & 0 deletions packages/example-ts3/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getDate as getDateLegacy } from './legacy/getDate';
import { getDate as getDateModern } from './modern/getDate';

console.log({ getDateLegacy, getDateModern });
16 changes: 16 additions & 0 deletions packages/example-ts3/src/legacy/getDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ok with strict: false
export const getDate = (date) => {
const modern: string | undefined = undefined;

// ok with strict: false
modern.split('');

return date.toLocaleDateString('ru-RU', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
}

// show `string` on hover in IDE
export const someVar: string | undefined = undefined;
18 changes: 18 additions & 0 deletions packages/example-ts3/src/modern/getDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { someVar } from '../legacy/getDate';

// Parameter 'date' implicitly has an 'any' type.
export const getDate = (date) => {
const modern: string | undefined = undefined;

// Show `string | undefined` on hover in IDE
console.log(someVar);

// 'modern' is possibly 'undefined'
modern.split('');

return date.toLocaleDateString('ru-RU', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
}
26 changes: 26 additions & 0 deletions packages/example-ts3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"module": "ESNext",
"outDir": "./dist",
"baseUrl": "./src",
"strict": false,
"noEmit": false,
"skipLibCheck": true,
"typeRoots": [], // A hack to avoid global types that are incompatible with older versions of TS
"plugins": [
{
"name": "ts-overrides-plugin",
"transform": "ts-overrides-plugin",
"transformProgram": true,
"overrides": [
{
"files": ["src/modern/**/*.{ts,tsx}"],
"compilerOptions": {
"strict": true,
},
},
]
},
]
},
}
29 changes: 29 additions & 0 deletions packages/example-ts3/webpack.config.fork-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = [
{
mode: 'production',
entry: './src/index.ts',
output: {
path: path.join(__dirname, 'build'),
filename: 'main.js',
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
plugins: [
new ForkTsCheckerWebpackPlugin(),
],
resolve: {
extensions: ['.ts', '.js'],
},
target: 'web',
}
];
25 changes: 25 additions & 0 deletions packages/example-ts3/webpack.config.ts-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');

module.exports = [
{
mode: 'production',
entry: './src/index.ts',
output: {
path: path.join(__dirname, 'build'),
filename: 'main.js',
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
target: 'web',
}
];
2 changes: 1 addition & 1 deletion packages/example-ts4/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"plugins": [
{
"name": "ts-overrides-plugin",
"transform": "ts-overrides-plugin/cli",
"transform": "ts-overrides-plugin",
"transformProgram": true,
"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"plugins": [
{
"name": "ts-overrides-plugin",
"transform": "ts-overrides-plugin/cli",
"transform": "ts-overrides-plugin",
"transformProgram": true,
"overrides": [
{
Expand Down
17 changes: 10 additions & 7 deletions packages/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ any other cases where you need to override the `tsconfig` settings for specific
## Installation and setup

Examples can be seen in the [`example`](https://github.com/DiFuks/ts-overrides-plugin/tree/main/packages/example) folder.
Specially for TS4, a separate [`example`](https://github.com/DiFuks/ts-overrides-plugin/tree/main/packages/example-ts4) folder.
Specially for `TS4`, a separate [`example`](https://github.com/DiFuks/ts-overrides-plugin/tree/main/packages/example-ts4) folder.
Specially for `TS3`, a separate [`example`](https://github.com/DiFuks/ts-overrides-plugin/tree/main/packages/example-ts3) folder.

### For using the plugin only in the IDE

Expand Down Expand Up @@ -76,7 +77,9 @@ For the plugin to work correctly in `webpack`, `tsc`, it's necessary to use the
Execute in the terminal:

```bash
yarn add -D ts-overrides-plugin ts-patch
yarn add -D ts-overrides-plugin ts-patch # For TS5
yarn add -D ts-overrides-plugin [email protected] # For TS4
yarn add -D ts-overrides-plugin [email protected] # For TS3
```

In the `tsconfig.json` file, add:
Expand All @@ -88,7 +91,7 @@ In the `tsconfig.json` file, add:
"plugins": [
{
"name": "ts-overrides-plugin",
"transform": "ts-overrides-plugin/cli",
"transform": "ts-overrides-plugin",
"transformProgram": true,
"overrides": [
{
Expand All @@ -112,12 +115,12 @@ In the `tsconfig.json` file, add:
```

If you are using [`Persistent Patch`](https://github.com/nonara/ts-patch?tab=readme-ov-file#method-2-persistent-patch)
with `ts-patch`, then there is nothing more to do.
with `ts-patch` (`yarn ts-patch install`), then there is nothing more to do.

For TS4 version, it is possible to use only the Persistent Patch option with [ts-patch version 2.1.0](https://github.com/nonara/ts-patch/tree/v2.1.0).
For `TypesSript 3` and `TypesSript 4` version, it is possible to use only the Persistent Patch option with.

If, however, you are using [`Live Compiler`](https://github.com/nonara/ts-patch?tab=readme-ov-file#method-1-live-compiler), the
following steps are necessary:
If, however, you are using [`Live Compiler`](https://github.com/nonara/ts-patch?tab=readme-ov-file#method-1-live-compiler) (Only `TypeScript 5`,
the following steps are necessary:

For the `tsc` command – replace it with `tspc` in `package.json`:

Expand Down
6 changes: 1 addition & 5 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
"lint": "eslint \"{**/*,*}.{ts,tsx,js,jsx,json}\"",
"lint:fix": "eslint \"{**/*,*}.{ts,tsx,js,json}\" --fix"
},
"main": "./dist/ide/index.js",
"exports": {
".": "./dist/ide/index.js",
"./cli": "./dist/cli/index.js"
},
"main": "./dist/index.js",
"dependencies": {
"outmatch": "^1.0.0"
},
Expand Down
23 changes: 23 additions & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable @typescript-eslint/no-unsafe-return,@typescript-eslint/no-explicit-any */
import type { ProgramTransformer } from 'ts-patch';
import type ts from 'typescript';

import cliPlugin from './cli';
import idePlugin from './ide';

type Plugin = {
// For IDE plugins
(...args: Parameters<ts.server.PluginModuleFactory>): ReturnType<ts.server.PluginModuleFactory>;
// For CLI plugins
(...args: Parameters<ProgramTransformer>): ReturnType<ProgramTransformer>;
};

const plugin: Plugin = (...args: unknown[]) => {
if (args.length === 1) {
return idePlugin(...(args as Parameters<ts.server.PluginModuleFactory>)) as any;
}

return cliPlugin(...(args as Parameters<ProgramTransformer>)) as any;
};

export = plugin;
66 changes: 63 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3906,6 +3906,27 @@ __metadata:
languageName: node
linkType: hard

"example-ts3@workspace:packages/example-ts3":
version: 0.0.0-use.local
resolution: "example-ts3@workspace:packages/example-ts3"
dependencies:
"@babel/core": "npm:^7.23.6"
"@babel/preset-env": "npm:^7.23.6"
"@babel/preset-typescript": "npm:^7.23.3"
"@types/babel__core": "npm:^7"
"@types/babel__preset-env": "npm:^7"
babel-loader: "npm:^9.1.3"
fork-ts-checker-webpack-plugin: "npm:^9.0.2"
ts-loader: "npm:^9.5.1"
ts-node: "npm:^10.9.2"
ts-overrides-plugin: "workspce:*"
ts-patch: "npm:1.4.5"
typescript: "npm:3.9.10"
webpack: "npm:^5.89.0"
webpack-cli: "npm:^5.1.4"
languageName: unknown
linkType: soft

"example-ts4@workspace:packages/example-ts4":
version: 0.0.0-use.local
resolution: "example-ts4@workspace:packages/example-ts4"
Expand Down Expand Up @@ -4316,7 +4337,7 @@ __metadata:
languageName: node
linkType: hard

"glob@npm:^7.0.0, glob@npm:^7.1.3":
"glob@npm:^7.0.0, glob@npm:^7.1.3, glob@npm:^7.1.7":
version: 7.2.3
resolution: "glob@npm:7.2.3::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fglob%2F-%2Fglob-7.2.3.tgz"
dependencies:
Expand Down Expand Up @@ -5368,7 +5389,7 @@ __metadata:
languageName: node
linkType: hard

"minimist@npm:^1.2.0, minimist@npm:^1.2.6, minimist@npm:^1.2.8":
"minimist@npm:^1.2.0, minimist@npm:^1.2.5, minimist@npm:^1.2.6, minimist@npm:^1.2.8":
version: 1.2.8
resolution: "minimist@npm:1.2.8::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fminimist%2F-%2Fminimist-1.2.8.tgz"
checksum: 10c0/19d3fcdca050087b84c2029841a093691a91259a47def2f18222f41e7645a0b7c44ef4b40e88a1e58a40c84d2ef0ee6047c55594d298146d0eb3f6b737c20ce6
Expand Down Expand Up @@ -6380,7 +6401,7 @@ __metadata:
languageName: node
linkType: hard

"shelljs@npm:^0.8.5":
"shelljs@npm:^0.8.4, shelljs@npm:^0.8.5":
version: 0.8.5
resolution: "shelljs@npm:0.8.5::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fshelljs%2F-%2Fshelljs-0.8.5.tgz"
dependencies:
Expand Down Expand Up @@ -6806,6 +6827,25 @@ __metadata:
languageName: unknown
linkType: soft

"ts-patch@npm:1.4.5":
version: 1.4.5
resolution: "ts-patch@npm:1.4.5::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fts-patch%2F-%2Fts-patch-1.4.5.tgz"
dependencies:
chalk: "npm:^4.1.0"
glob: "npm:^7.1.7"
global-prefix: "npm:^3.0.0"
minimist: "npm:^1.2.5"
resolve: "npm:^1.20.0"
shelljs: "npm:^0.8.4"
strip-ansi: "npm:^6.0.0"
peerDependencies:
typescript: ">2.7.0"
bin:
ts-patch: bin/cli.js
checksum: 10c0/3e9b0d3838d0fc930f89000206c586485582c7f7f2746f1e4227a6777b10c06c7642395cd88cbd2950a3938da4146d4e8a1fbe0e02f46624764b9d42a01d7470
languageName: node
linkType: hard

"ts-patch@npm:2.1.0":
version: 2.1.0
resolution: "ts-patch@npm:2.1.0::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fts-patch%2F-%2Fts-patch-2.1.0.tgz"
Expand Down Expand Up @@ -6947,6 +6987,16 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:3.9.10":
version: 3.9.10
resolution: "typescript@npm:3.9.10::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Ftypescript%2F-%2Ftypescript-3.9.10.tgz"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 10c0/863cc06070fa18a0f9c6a83265fb4922a8b51bf6f2c6760fb0b73865305ce617ea4bc6477381f9f4b7c3a8cb4a455b054f5469e6e41307733fe6a2bd9aae82f8
languageName: node
linkType: hard

"typescript@npm:4.2.4":
version: 4.2.4
resolution: "typescript@npm:4.2.4::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Ftypescript%2F-%2Ftypescript-4.2.4.tgz"
Expand All @@ -6967,6 +7017,16 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:typescript@npm%3A3.9.10#optional!builtin<compat/typescript>":
version: 3.9.10
resolution: "typescript@patch:typescript@npm%3A3.9.10%3A%3A__archiveUrl=https%253A%252F%252Fregistry.npmjs.org%252Ftypescript%252F-%252Ftypescript-3.9.10.tgz#optional!builtin<compat/typescript>::version=3.9.10&hash=3bd3d3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 10c0/9041fb3886e7d6a560f985227b8c941d17a750f2edccb5f9b3a15a2480574654d9be803ad4a14aabcc2f2553c4d272a25fd698a7c42692f03f66b009fb46883c
languageName: node
linkType: hard

"typescript@patch:typescript@npm%3A4.2.4#optional!builtin<compat/typescript>":
version: 4.2.4
resolution: "typescript@patch:typescript@npm%3A4.2.4%3A%3A__archiveUrl=https%253A%252F%252Fregistry.npmjs.org%252Ftypescript%252F-%252Ftypescript-4.2.4.tgz#optional!builtin<compat/typescript>::version=4.2.4&hash=334f98"
Expand Down

0 comments on commit 05ea577

Please sign in to comment.