Skip to content

Commit

Permalink
(fix): parse tsconfig extends, trailing commas, and comments (jaredpa…
Browse files Browse the repository at this point in the history
…lmer#489)

- use ts.readConfigFile to properly parse a commas and comments from
  tsconfig instead of just trying to readJSON
- use ts.parseJsonConfigFileContent to properly parse `extends` and
  get recursive compilerOptions

- add tests for all of the above use cases
  - add them to build-withTsconfig which changes declarationDir;
    should only work if TSDX properly parses the tsconfig

NOTE: this is only necessary for internal, TSDX-specific parsing of
tsconfig.json. rollup-plugin-typescript2 already handles these options,
but internal options like esModuleInterop etc are also used & parsed
independently of rpts2
  • Loading branch information
agilgur5 authored and paul-vd committed Dec 1, 2020
1 parent a8ba5c4 commit efbe221
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
24 changes: 14 additions & 10 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import sourceMaps from 'rollup-plugin-sourcemaps';
import typescript from 'rollup-plugin-typescript2';
import ts from 'typescript';

import { extractErrors } from './errors/extractErrors';
import { babelPluginTsdx } from './babelPluginTsdx';
import { TsdxOptions } from './types';
import * as fs from 'fs-extra';

const errorCodeOpts = {
errorMapFilePath: paths.appErrorsJson,
Expand Down Expand Up @@ -45,10 +46,15 @@ export async function createRollupConfig(
entryFileNames = `${inputKeys[0]}.${outputSuffix}`;
}

let tsconfigJSON;
try {
tsconfigJSON = await fs.readJSON(opts.tsconfig || paths.tsconfigJson);
} catch (e) {}
const tsconfigPath = opts.tsconfig || paths.tsconfigJson;
// borrowed from https://github.com/facebook/create-react-app/pull/7248
const tsconfigJSON = ts.readConfigFile(tsconfigPath, ts.sys.readFile).config;
// borrowed from https://github.com/ezolenko/rollup-plugin-typescript2/blob/42173460541b0c444326bf14f2c8c27269c4cb11/src/parse-tsconfig.ts#L48
const tsCompilerOptions = ts.parseJsonConfigFileContent(
tsconfigJSON,
ts.sys,
'./'
).options;

return {
// Tell Rollup the entry point to the package
Expand Down Expand Up @@ -93,7 +99,7 @@ export async function createRollupConfig(
// (i.e. import * as namespaceImportObject from...) that are accessed dynamically.
freeze: false,
// Respect tsconfig esModuleInterop when setting __esModule.
esModule: tsconfigJSON ? tsconfigJSON.esModuleInterop : false,
esModule: tsCompilerOptions ? tsCompilerOptions.esModuleInterop : false,
name: opts.name || safeVariableName(opts.name),
sourcemap: true,
globals: { react: 'React', 'react-native': 'ReactNative' },
Expand Down Expand Up @@ -140,7 +146,7 @@ export async function createRollupConfig(
},
},
typescript({
typescript: require('typescript'),
typescript: ts,
cacheRoot: `./node_modules/.cache/tsdx/${opts.format}/`,
tsconfig: opts.tsconfig,
tsconfigDefaults: {
Expand Down Expand Up @@ -171,9 +177,7 @@ export async function createRollupConfig(
},
},
check: !opts.transpileOnly,
useTsconfigDeclarationDir: Boolean(
tsconfigJSON?.compilerOptions?.declarationDir
),
useTsconfigDeclarationDir: Boolean(tsCompilerOptions?.declarationDir),
}),
babelPluginTsdx({
exclude: 'node_modules/**',
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra';
import path from 'path';
import camelCase from 'camelcase';

import { PackageJson } from './types';

// Remove the package name scope if it exists
Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/build-withTsconfig/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
"module": "ESNext",
"lib": ["dom", "esnext"],
"declaration": true,
"declarationDir": "typings",
"declarationMap": true,
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
},
"include": ["src", "types"], // test parsing of trailing comma & comment
}
30 changes: 2 additions & 28 deletions test/fixtures/build-withTsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
{
"compilerOptions": {
"module": "ESNext",
"lib": ["dom", "esnext"],
"declaration": true,
"declarationDir": "typings",
"declarationMap": true,
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
},
"include": ["src", "types"]
// ensure that extends works (trailing comma & comment too)
"extends": "./tsconfig.base.json",
}

0 comments on commit efbe221

Please sign in to comment.