Skip to content

Commit

Permalink
feat: implement write-dts mode (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangodong authored Nov 29, 2021
1 parent b1c0d9a commit 61f7cdf
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
4 changes: 2 additions & 2 deletions src/ForkTsCheckerWebpackPluginOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-reporter/TypeScriptReporterConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-reporter/TypeScriptReporterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeScriptDiagnosticsOptions>;
extensions?: {
vue?: TypeScriptVueExtensionOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/TypeScriptSolutionBuilderApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'] } },
Expand Down

0 comments on commit 61f7cdf

Please sign in to comment.