From 61f7cdf3971725dee4df1fdbe914643174f39037 Mon Sep 17 00:00:00 2001 From: Adrian Godong Date: Mon, 29 Nov 2021 12:41:15 -0800 Subject: [PATCH] feat: implement write-dts mode (#679) --- README.md | 2 +- src/ForkTsCheckerWebpackPluginOptions.json | 4 ++-- .../TypeScriptReporterConfiguration.ts | 2 +- .../TypeScriptReporterOptions.ts | 2 +- .../reporter/ControlledTypeScriptSystem.ts | 6 ++++-- test/e2e/TypeScriptSolutionBuilderApi.spec.ts | 20 +++++++++++++++++++ .../TypeScriptReporterConfiguration.spec.ts | 1 + 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 71fae761..352b8ae4 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ Options for the TypeScript checker (`typescript` option object). | `configOverwrite` | `object` | `{ compilerOptions: { skipLibCheck: true, sourceMap: false, inlineSourceMap: false, declarationMap: false } }` | This configuration will overwrite configuration from the `tsconfig.json` file. Supported fields are: `extends`, `compilerOptions`, `include`, `exclude`, `files`, and `references`. | | `context` | `string` | `dirname(configuration.configFile)` | The base path for finding files specified in the `tsconfig.json`. Same as the `context` option from the [ts-loader](https://github.com/TypeStrong/ts-loader#context). Useful if you want to keep your `tsconfig.json` in an external package. Keep in mind that **not** having a `tsconfig.json` in your project root can cause different behaviour between `fork-ts-checker-webpack-plugin` and `tsc`. When using editors like `VS Code` it is advised to add a `tsconfig.json` file to the root of the project and extend the config file referenced in option `configFile`. | | `build` | `boolean` | `false` | The equivalent of the `--build` flag for the `tsc` command. | -| `mode` | `'readonly'` or `'write-tsbuildinfo'` or `'write-references'` | `'write-tsbuildinfo'` | If you use the `babel-loader`, it's recommended to use `write-references` mode to improve initial compilation time. If you use `ts-loader`, it's recommended to use `write-tsbuildinfo` mode to not overwrite files emitted by the `ts-loader`. | +| `mode` | `'readonly'` or `'write-tsbuildinfo'` or `'write-dts'` or `'write-references'` | `'write-tsbuildinfo'` | If you use the `babel-loader`, it's recommended to use `write-references` mode to improve initial compilation time. If you use `ts-loader`, it's recommended to use `write-tsbuildinfo` mode to not overwrite files emitted by the `ts-loader`. If you use `ts-loader` with `transpileOnly` flag set to `true`, use `'write-dts` to emit the type definition files. | | `diagnosticOptions` | `object` | `{ syntactic: false, semantic: true, declaration: false, global: false }` | Settings to select which diagnostics do we want to perform. | | `extensions` | `object` | `{}` | See [TypeScript extensions options](#typescript-extensions-options). | | `profile` | `boolean` | `false` | Measures and prints timings related to the TypeScript performance. | diff --git a/src/ForkTsCheckerWebpackPluginOptions.json b/src/ForkTsCheckerWebpackPluginOptions.json index e7ecc2aa..72852a97 100644 --- a/src/ForkTsCheckerWebpackPluginOptions.json +++ b/src/ForkTsCheckerWebpackPluginOptions.json @@ -138,8 +138,8 @@ }, "mode": { "type": "string", - "enum": ["readonly", "write-tsbuildinfo", "write-references"], - "description": "`readonly` keeps all emitted files in memory, `write-tsbuildinfo` which writes only .tsbuildinfo files and `write-references` which writes both .tsbuildinfo and referenced projects output" + "enum": ["readonly", "write-tsbuildinfo", "write-dts", "write-references"], + "description": "`readonly` keeps all emitted files in memory, `write-tsbuildinfo` which writes only .tsbuildinfo files, `write-dts` writes .tsbuildinfo and type definition files, and `write-references` which writes both .tsbuildinfo and referenced projects output" }, "compilerOptions": { "type": "object", diff --git a/src/typescript-reporter/TypeScriptReporterConfiguration.ts b/src/typescript-reporter/TypeScriptReporterConfiguration.ts index 49bf10d9..cb92e7b3 100644 --- a/src/typescript-reporter/TypeScriptReporterConfiguration.ts +++ b/src/typescript-reporter/TypeScriptReporterConfiguration.ts @@ -16,7 +16,7 @@ interface TypeScriptReporterConfiguration { configOverwrite: TypeScriptConfigurationOverwrite; build: boolean; context: string; - mode: 'readonly' | 'write-tsbuildinfo' | 'write-references'; + mode: 'readonly' | 'write-tsbuildinfo' | 'write-dts' | 'write-references'; diagnosticOptions: TypeScriptDiagnosticsOptions; extensions: { vue: TypeScriptVueExtensionConfiguration; diff --git a/src/typescript-reporter/TypeScriptReporterOptions.ts b/src/typescript-reporter/TypeScriptReporterOptions.ts index 1a2c89e7..bf93311e 100644 --- a/src/typescript-reporter/TypeScriptReporterOptions.ts +++ b/src/typescript-reporter/TypeScriptReporterOptions.ts @@ -11,7 +11,7 @@ type TypeScriptReporterOptions = configOverwrite?: TypeScriptConfigurationOverwrite; context?: string; build?: boolean; - mode?: 'readonly' | 'write-tsbuildinfo' | 'write-references'; + mode?: 'readonly' | 'write-tsbuildinfo' | 'write-dts' | 'write-references'; diagnosticOptions?: Partial; extensions?: { vue?: TypeScriptVueExtensionOptions; diff --git a/src/typescript-reporter/reporter/ControlledTypeScriptSystem.ts b/src/typescript-reporter/reporter/ControlledTypeScriptSystem.ts index 517ecb47..cbdb1417 100644 --- a/src/typescript-reporter/reporter/ControlledTypeScriptSystem.ts +++ b/src/typescript-reporter/reporter/ControlledTypeScriptSystem.ts @@ -39,7 +39,7 @@ interface ControlledTypeScriptSystem extends ts.System { setArtifacts(artifacts: FilesMatch): void; } -type FileSystemMode = 'readonly' | 'write-tsbuildinfo' | 'write-references'; +type FileSystemMode = 'readonly' | 'write-tsbuildinfo' | 'write-dts' | 'write-references'; function createControlledTypeScriptSystem( typescript: typeof ts, @@ -171,7 +171,9 @@ function createControlledTypeScriptSystem( function getWriteFileSystem(path: string) { if ( mode === 'write-references' || - (mode === 'write-tsbuildinfo' && path.endsWith('.tsbuildinfo')) + (mode === 'write-tsbuildinfo' && path.endsWith('.tsbuildinfo')) || + (mode === 'write-dts' && + ['.tsbuildinfo', '.d.ts', '.d.ts.map'].some((prefix) => path.endsWith(prefix))) ) { return realFileSystem; } diff --git a/test/e2e/TypeScriptSolutionBuilderApi.spec.ts b/test/e2e/TypeScriptSolutionBuilderApi.spec.ts index 919f51da..c990e86b 100644 --- a/test/e2e/TypeScriptSolutionBuilderApi.spec.ts +++ b/test/e2e/TypeScriptSolutionBuilderApi.spec.ts @@ -26,6 +26,7 @@ describe('TypeScript SolutionBuilder API', () => { it.each([ { async: false, typescript: '~3.6.0', mode: 'readonly' }, { async: true, typescript: '~3.8.0', mode: 'write-tsbuildinfo' }, + { async: true, typescript: '~3.8.0', mode: 'write-dts' }, { async: false, typescript: '~3.8.0', mode: 'write-references' }, ])('reports semantic error for %p', async ({ async, typescript, mode }) => { await sandbox.load([ @@ -143,6 +144,25 @@ describe('TypeScript SolutionBuilder API', () => { await sandbox.remove('packages/client/lib'); break; + case 'write-dts': + expect(await sandbox.exists('packages/shared/lib/tsconfig.tsbuildinfo')).toEqual(true); + expect(await sandbox.exists('packages/client/lib/tsconfig.tsbuildinfo')).toEqual(true); + expect(await sandbox.exists('packages/shared/lib')).toEqual(true); + expect(await sandbox.exists('packages/client/lib')).toEqual(true); + expect(await sandbox.exists('packages/shared/lib/index.d.ts')).toEqual(true); + expect(await sandbox.exists('packages/shared/lib/index.d.ts.map')).toEqual(true); + expect(await sandbox.exists('packages/client/lib/index.d.ts')).toEqual(true); + expect(await sandbox.exists('packages/client/lib/index.d.ts.map')).toEqual(true); + expect(await sandbox.exists('packages/shared/lib/index.js')).toEqual(false); + expect(await sandbox.exists('packages/client/lib/index.js')).toEqual(false); + + expect(await sandbox.read('packages/shared/lib/tsconfig.tsbuildinfo')).not.toEqual(''); + expect(await sandbox.read('packages/client/lib/tsconfig.tsbuildinfo')).not.toEqual(''); + + await sandbox.remove('packages/shared/lib'); + await sandbox.remove('packages/client/lib'); + break; + case 'write-references': expect(await sandbox.exists('packages/shared/lib/tsconfig.tsbuildinfo')).toEqual(true); expect(await sandbox.exists('packages/client/lib/tsconfig.tsbuildinfo')).toEqual(true); diff --git a/test/unit/typescript-reporter/TypeScriptReporterConfiguration.spec.ts b/test/unit/typescript-reporter/TypeScriptReporterConfiguration.spec.ts index f65a8c77..44cef185 100644 --- a/test/unit/typescript-reporter/TypeScriptReporterConfiguration.spec.ts +++ b/test/unit/typescript-reporter/TypeScriptReporterConfiguration.spec.ts @@ -74,6 +74,7 @@ describe('typescript-reporter/TypeScriptsReporterConfiguration', () => { [{ build: true }, { ...configuration, build: true }], [{ mode: 'readonly' }, { ...configuration, mode: 'readonly' }], [{ mode: 'write-tsbuildinfo' }, { ...configuration, mode: 'write-tsbuildinfo' }], + [{ mode: 'write-dts' }, { ...configuration, mode: 'write-dts' }], [{ mode: 'write-references' }, { ...configuration, mode: 'write-references' }], [ { configOverwrite: { compilerOptions: { strict: true }, include: ['src'] } },