Skip to content

Commit

Permalink
Merge pull request #6 from DiFuks/support-ts4
Browse files Browse the repository at this point in the history
support ts4
  • Loading branch information
DiFuks authored Jun 21, 2024
2 parents 266d7fe + 7fd2825 commit 7263a40
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/51d398a5.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-ts4/.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-ts4/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-ts4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "example-ts4",
"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": "2.1.0",
"typescript": "4.2.4",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"packageManager": "[email protected]"
}
4 changes: 4 additions & 0 deletions packages/example-ts4/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-ts4/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-ts4/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',
});
}
25 changes: 25 additions & 0 deletions packages/example-ts4/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "ESNext",
"outDir": "./dist",
"baseUrl": "./src",
"strict": false,
"noEmit": false,
"skipLibCheck": true,
"plugins": [
{
"name": "ts-overrides-plugin",
"transform": "ts-overrides-plugin/cli",
"transformProgram": true,
"overrides": [
{
"files": ["src/modern/**/*.{ts,tsx}"],
"compilerOptions": {
"strict": true,
},
},
]
},
]
},
}
29 changes: 29 additions & 0 deletions packages/example-ts4/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-ts4/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',
}
];
7 changes: 6 additions & 1 deletion packages/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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.

### For using the plugin only in the IDE

Expand Down Expand Up @@ -111,7 +112,11 @@ 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. If, however, you are using [`Live Compiler`](https://github.com/nonara/ts-patch?tab=readme-ov-file#method-1-live-compiler), the
with `ts-patch`, 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).

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:

For the `tsc` command – replace it with `tspc` in `package.json`:
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"typescript": "5.5.1-rc"
},
"peerDependencies": {
"ts-patch": "^3.0.0",
"typescript": "^5.0.0"
"ts-patch": "^3.0.0 || ^2.0.0",
"typescript": "^5.0.0 || ^4.0.0"
},
"license": "MIT",
"packageManager": "[email protected]"
Expand Down
10 changes: 7 additions & 3 deletions packages/plugin/src/ide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { relative } from 'node:path';
import outmatch from 'outmatch';
import type ts from 'typescript/lib/tsserverlibrary';

import { type Override } from '../types/Override';
import type { Override } from '../types/Override';

interface IdePluginConfig {
overrides: Override[];
Expand All @@ -16,13 +16,17 @@ const getOverrideLanguageServices = (
): ts.LanguageService[] =>
[...overridesFromConfig].reverse().map(override => {
const overrideLanguageServiceHost: ts.LanguageServiceHost = {
fileExists: path => languageServiceHost.fileExists(path),
// don't remove this, it's needed for TS 4
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
fileExists: path => !!languageServiceHost.fileExists?.(path),
getCurrentDirectory: (): string => languageServiceHost.getCurrentDirectory(),
getDefaultLibFileName: (options: ts.CompilerOptions): string =>
languageServiceHost.getDefaultLibFileName(options),
getScriptSnapshot: fileName => languageServiceHost.getScriptSnapshot(fileName),
getScriptVersion: fileName => languageServiceHost.getScriptVersion(fileName),
readFile: (path, encoding) => languageServiceHost.readFile(path, encoding),
// don't remove this, it's needed for TS 4
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
readFile: (path, encoding) => languageServiceHost.readFile?.(path, encoding),
getCompilationSettings: () => ({
...languageServiceHost.getCompilationSettings(),
...typescript.convertCompilerOptionsFromJson(
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/types/Override.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type ts from 'typescript';
import type ts from 'typescript/lib/tsserverlibrary';

export interface Override {
files: string[];
Expand Down
Loading

0 comments on commit 7263a40

Please sign in to comment.