diff --git a/.yarn/versions/2b0dfbff.yml b/.yarn/versions/2b0dfbff.yml new file mode 100644 index 0000000..75deb38 --- /dev/null +++ b/.yarn/versions/2b0dfbff.yml @@ -0,0 +1,2 @@ +releases: + ts-overrides-plugin: minor diff --git a/packages/example-ts3/.babelrc b/packages/example-ts3/.babelrc new file mode 100644 index 0000000..937768f --- /dev/null +++ b/packages/example-ts3/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["@babel/preset-env", "@babel/preset-typescript"] +} \ No newline at end of file diff --git a/packages/example-ts3/README.md b/packages/example-ts3/README.md new file mode 100644 index 0000000..16dd4cb --- /dev/null +++ b/packages/example-ts3/README.md @@ -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 +``` diff --git a/packages/example-ts3/package.json b/packages/example-ts3/package.json new file mode 100644 index 0000000..7b7efe0 --- /dev/null +++ b/packages/example-ts3/package.json @@ -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": "yarn@4.0.2" +} diff --git a/packages/example-ts3/src/index.ts b/packages/example-ts3/src/index.ts new file mode 100644 index 0000000..8f3b5fc --- /dev/null +++ b/packages/example-ts3/src/index.ts @@ -0,0 +1,4 @@ +import { getDate as getDateLegacy } from './legacy/getDate'; +import { getDate as getDateModern } from './modern/getDate'; + +console.log({ getDateLegacy, getDateModern }); \ No newline at end of file diff --git a/packages/example-ts3/src/legacy/getDate.ts b/packages/example-ts3/src/legacy/getDate.ts new file mode 100644 index 0000000..2b74ad3 --- /dev/null +++ b/packages/example-ts3/src/legacy/getDate.ts @@ -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; diff --git a/packages/example-ts3/src/modern/getDate.ts b/packages/example-ts3/src/modern/getDate.ts new file mode 100644 index 0000000..9891742 --- /dev/null +++ b/packages/example-ts3/src/modern/getDate.ts @@ -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', + }); +} diff --git a/packages/example-ts3/tsconfig.json b/packages/example-ts3/tsconfig.json new file mode 100644 index 0000000..cc41189 --- /dev/null +++ b/packages/example-ts3/tsconfig.json @@ -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, + }, + }, + ] + }, + ] + }, +} diff --git a/packages/example-ts3/webpack.config.fork-ts.js b/packages/example-ts3/webpack.config.fork-ts.js new file mode 100644 index 0000000..f5000f2 --- /dev/null +++ b/packages/example-ts3/webpack.config.fork-ts.js @@ -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', + } +]; diff --git a/packages/example-ts3/webpack.config.ts-loader.js b/packages/example-ts3/webpack.config.ts-loader.js new file mode 100644 index 0000000..8584b36 --- /dev/null +++ b/packages/example-ts3/webpack.config.ts-loader.js @@ -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', + } +]; diff --git a/packages/example-ts4/tsconfig.json b/packages/example-ts4/tsconfig.json index 412eb19..6125b48 100644 --- a/packages/example-ts4/tsconfig.json +++ b/packages/example-ts4/tsconfig.json @@ -9,7 +9,7 @@ "plugins": [ { "name": "ts-overrides-plugin", - "transform": "ts-overrides-plugin/cli", + "transform": "ts-overrides-plugin", "transformProgram": true, "overrides": [ { diff --git a/packages/example/tsconfig.json b/packages/example/tsconfig.json index 412eb19..6125b48 100644 --- a/packages/example/tsconfig.json +++ b/packages/example/tsconfig.json @@ -9,7 +9,7 @@ "plugins": [ { "name": "ts-overrides-plugin", - "transform": "ts-overrides-plugin/cli", + "transform": "ts-overrides-plugin", "transformProgram": true, "overrides": [ { diff --git a/packages/plugin/README.md b/packages/plugin/README.md index 60372e5..4fe76ef 100644 --- a/packages/plugin/README.md +++ b/packages/plugin/README.md @@ -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 @@ -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 ts-patch@2.1.0 # For TS4 +yarn add -D ts-overrides-plugin ts-patch@1.4.5 # For TS3 ``` In the `tsconfig.json` file, add: @@ -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": [ { @@ -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`: diff --git a/packages/plugin/package.json b/packages/plugin/package.json index 7d63fe7..8374dda 100644 --- a/packages/plugin/package.json +++ b/packages/plugin/package.json @@ -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" }, diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts new file mode 100644 index 0000000..5044548 --- /dev/null +++ b/packages/plugin/src/index.ts @@ -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): ReturnType; + // For CLI plugins + (...args: Parameters): ReturnType; +}; + +const plugin: Plugin = (...args: unknown[]) => { + if (args.length === 1) { + return idePlugin(...(args as Parameters)) as any; + } + + return cliPlugin(...(args as Parameters)) as any; +}; + +export = plugin; diff --git a/yarn.lock b/yarn.lock index 7691c70..5ac1f73 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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: @@ -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 @@ -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: @@ -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" @@ -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" @@ -6967,6 +7017,16 @@ __metadata: languageName: node linkType: hard +"typescript@patch:typescript@npm%3A3.9.10#optional!builtin": + 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::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": 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::version=4.2.4&hash=334f98"