Skip to content

Commit a21db7d

Browse files
feat(typescript): allow override of forced noEmit and emitDeclarationOnly compiler options (#1242)
* add documentation * add noForceEmit option and apply in code * fix that issue with | undefined * tests WIP * MR feedback for README * fix tests
1 parent 160959d commit a21db7d

File tree

12 files changed

+124
-10
lines changed

12 files changed

+124
-10
lines changed

packages/typescript/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ typescript({
214214
});
215215
```
216216

217+
### `noForceEmit`
218+
219+
Type: `Boolean`<br>
220+
Default: `false`
221+
222+
Earlier version of `@rollup/plugin-typescript` required that the `compilerOptions` `noEmit` and `emitDeclarationOnly` both false to guarantee that source code was fed into the next plugin/output. This is no longer true. This option disables the plugin forcing the values of those options and instead defers to the values set in `tsconfig.json`.
223+
224+
`noForceEmit` can be very useful if you use with `@rollup/plugin-babel` and `@babel/preset-typescript`. Having `@rollup/plugin-typescript` only do typechecking / declarations with `"emitDeclarationOnly": true` while deferring to `@rollup/plugin-babel` for transpilation can dramatically reduce `rollup` build times for large projects.
225+
217226
### Typescript compiler options
218227

219228
Some of Typescript's [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) affect how Rollup builds files.
@@ -238,6 +247,7 @@ These compiler options are ignored by Rollup:
238247

239248
- `noEmitHelpers`, `importHelpers`: The `tslib` helper module always must be used.
240249
- `noEmit`, `emitDeclarationOnly`: Typescript needs to emit code for the plugin to work with.
250+
- _Note: While this was true for early iterations of `@rollup/plugin-typescript`, it is no longer. To override this behavior, and defer to `tsconfig.json` for these options, see the [`noForceEmit`](#noForceEmit) option_
241251
- `noResolve`: Preventing Typescript from resolving code may break compilation
242252

243253
### Importing CommonJS

packages/typescript/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
2525
filterRoot,
2626
include,
2727
outputToFilesystem,
28+
noForceEmit,
2829
transformers,
2930
tsconfig,
3031
tslib,
@@ -34,7 +35,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
3435
const emittedFiles = new Map<string, string>();
3536
const watchProgramHelper = new WatchProgramHelper();
3637

37-
const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions);
38+
const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions, noForceEmit);
3839
const filter = createFilter(include || ['*.ts+(|x)', '**/*.ts+(|x)'], exclude, {
3940
resolve: filterRoot ?? parsedOptions.options.rootDir
4041
});

packages/typescript/src/options/interfaces.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ export const DEFAULT_COMPILER_OPTIONS: PartialCompilerOptions = {
1313
skipLibCheck: true
1414
};
1515

16+
export const OVERRIDABLE_EMIT_COMPILER_OPTIONS: Partial<CompilerOptions> = {
17+
noEmit: false,
18+
emitDeclarationOnly: false
19+
};
20+
1621
export const FORCED_COMPILER_OPTIONS: Partial<CompilerOptions> = {
1722
// Always use tslib
1823
noEmitHelpers: true,
1924
importHelpers: true,
20-
// Typescript needs to emit the code for us to work with
21-
noEmit: false,
22-
emitDeclarationOnly: false,
2325
// Preventing Typescript from resolving code may break compilation
2426
noResolve: false
2527
};

packages/typescript/src/options/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const getPluginOptions = (options: RollupTypescriptOptions) => {
1919
exclude,
2020
include,
2121
filterRoot,
22+
noForceEmit,
2223
transformers,
2324
tsconfig,
2425
tslib,
@@ -34,6 +35,7 @@ export const getPluginOptions = (options: RollupTypescriptOptions) => {
3435
include,
3536
exclude,
3637
filterRoot,
38+
noForceEmit: noForceEmit || false,
3739
tsconfig,
3840
compilerOptions: { ...extra, ...compilerOptions } as PartialCompilerOptions,
3941
typescript: typescript || defaultTs,

packages/typescript/src/options/tsconfig.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DEFAULT_COMPILER_OPTIONS,
2222
EnumCompilerOptions,
2323
FORCED_COMPILER_OPTIONS,
24+
OVERRIDABLE_EMIT_COMPILER_OPTIONS,
2425
PartialCompilerOptions
2526
} from './interfaces';
2627
import { normalizeCompilerOptions, makePathsAbsolute } from './normalize';
@@ -38,6 +39,10 @@ export interface TypeScriptConfig {
3839
compileOnSave?: boolean | undefined;
3940
}
4041

42+
function makeForcedCompilerOptions(noForceEmit: boolean) {
43+
return { ...FORCED_COMPILER_OPTIONS, ...(noForceEmit ? {} : OVERRIDABLE_EMIT_COMPILER_OPTIONS) };
44+
}
45+
4146
/**
4247
* Finds the path to the tsconfig file relative to the current working directory.
4348
* @param relativePath Relative tsconfig path given by the user.
@@ -110,7 +115,8 @@ const configCache = new Map() as import('typescript').Map<ExtendedConfigCacheEnt
110115
export function parseTypescriptConfig(
111116
ts: typeof import('typescript'),
112117
tsconfig: RollupTypescriptOptions['tsconfig'],
113-
compilerOptions: PartialCompilerOptions
118+
compilerOptions: PartialCompilerOptions,
119+
noForceEmit: boolean
114120
): TypeScriptConfig {
115121
/* eslint-disable no-undefined */
116122
const cwd = process.cwd();
@@ -136,7 +142,7 @@ export function parseTypescriptConfig(
136142
},
137143
ts.sys,
138144
basePath,
139-
{ ...compilerOptions, ...FORCED_COMPILER_OPTIONS },
145+
{ ...compilerOptions, ...makeForcedCompilerOptions(noForceEmit) },
140146
tsConfigPath,
141147
undefined,
142148
undefined,
@@ -154,7 +160,7 @@ export function parseTypescriptConfig(
154160
},
155161
ts.sys,
156162
basePath,
157-
FORCED_COMPILER_OPTIONS,
163+
makeForcedCompilerOptions(noForceEmit),
158164
tsConfigPath,
159165
undefined,
160166
undefined,

packages/typescript/test/fixtures/incremental-single/tsconfig.tsbuildinfo

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@
391391
"signature": "ef91066d2057cca50511e3af2d4aa54e07913a15f9cee1748f256139318eb413",
392392
"affectsGlobalScope": false
393393
},
394+
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/types/lib/index.d.ts": {
395+
"version": "a17ba25a194979a88bfd925e849d76b63f68c4160b69a99c5d723eac5ffddaf0",
396+
"signature": "a17ba25a194979a88bfd925e849d76b63f68c4160b69a99c5d723eac5ffddaf0",
397+
"affectsGlobalScope": false
398+
},
394399
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/parser/typings/babel-parser.d.ts": {
395400
"version": "8678956904af215fe917b2df07b6c54f876fa64eb1f8a158e4ff38404cef3ff4",
396401
"signature": "8678956904af215fe917b2df07b6c54f876fa64eb1f8a158e4ff38404cef3ff4",
@@ -744,15 +749,15 @@
744749
"configFilePath": "./tsconfig.json",
745750
"noEmitHelpers": true,
746751
"importHelpers": true,
752+
"noResolve": false,
747753
"noEmit": false,
748754
"emitDeclarationOnly": false,
749-
"noResolve": false,
750755
"sourceMap": true,
751756
"inlineSources": true
752757
},
753758
"referencedMap": {
754759
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/parser/typings/babel-parser.d.ts": [
755-
"../../../../../node_modules/.pnpm/@babel+types@7.12.1/node_modules/@babel/types/lib/index.d.ts"
760+
"../../../../../node_modules/.pnpm/@babel+types@7.14.5/node_modules/@babel/types/lib/index.d.ts"
756761
],
757762
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@types/babel__core/index.d.ts": [
758763
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/parser/typings/babel-parser.d.ts",
@@ -1154,7 +1159,7 @@
11541159
},
11551160
"exportedModulesMap": {
11561161
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/parser/typings/babel-parser.d.ts": [
1157-
"../../../../../node_modules/.pnpm/@babel+types@7.12.1/node_modules/@babel/types/lib/index.d.ts"
1162+
"../../../../../node_modules/.pnpm/@babel+types@7.14.5/node_modules/@babel/types/lib/index.d.ts"
11581163
],
11591164
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@types/babel__core/index.d.ts": [
11601165
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/parser/typings/babel-parser.d.ts",
@@ -1557,6 +1562,7 @@
15571562
"semanticDiagnosticsPerFile": [
15581563
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/parser/typings/babel-parser.d.ts",
15591564
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/types/lib/index.d.ts",
1565+
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@babel/types/lib/index.d.ts",
15601566
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@types/babel__core/index.d.ts",
15611567
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@types/babel__generator/index.d.ts",
15621568
"../../../../../node_modules/.pnpm/@[email protected]/node_modules/@types/babel__template/index.d.ts",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const answer = 42;
2+
3+
export { answer };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"emitDeclarationOnly": true,
4+
"declaration": true,
5+
"outDir": "dist"
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const answer = 42;
2+
3+
export { answer };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"noEmit": true,
5+
"outDir": "dist"
6+
}
7+
}

0 commit comments

Comments
 (0)