Skip to content

Commit

Permalink
fix: parse tsconfig.josn error (#157)
Browse files Browse the repository at this point in the history
* fix: fix parse tsconfig error

* fix: fix test error

* test: update test case
  • Loading branch information
zhanba authored and sorrycc committed Nov 28, 2019
1 parent e54641b commit a365375
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
25 changes: 22 additions & 3 deletions packages/father-build/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import gulpIf from 'gulp-if';
import chalk from "chalk";
import getBabelConfig from './getBabelConfig';
import { IBundleOptions } from './types';
import * as ts from 'typescript';

interface IBabelOpts {
cwd: string;
Expand Down Expand Up @@ -90,17 +91,35 @@ export default async function(opts: IBabelOpts) {
}).code;
}

/**
* tsconfig.json is not valid json file
* https://github.com/Microsoft/TypeScript/issues/20384
*/
function parseTsconfig(path: string) {
const readFile = (path:string) => readFileSync(path, 'utf-8')
const result = ts.readConfigFile(path, readFile)
if (result.error) {
return
}
return result.config
}

function getTsconfigCompilerOptions(path: string) {
const config = parseTsconfig(path)
return config ? config.compilerOptions : undefined
}

function getTSConfig() {
const tsconfigPath = join(cwd, 'tsconfig.json');
const templateTsconfigPath = join(__dirname, '../template/tsconfig.json');

if (existsSync(tsconfigPath)) {
return JSON.parse(readFileSync(tsconfigPath, 'utf-8')).compilerOptions || {};
return getTsconfigCompilerOptions(tsconfigPath) || {};
}
if (rootPath && existsSync(join(rootPath, 'tsconfig.json'))) {
return JSON.parse(readFileSync(join(rootPath, 'tsconfig.json'), 'utf-8')).compilerOptions || {};
return getTsconfigCompilerOptions(join(rootPath, 'tsconfig.json')) || {};
}
return JSON.parse(readFileSync(templateTsconfigPath, 'utf-8')).compilerOptions || {};
return getTsconfigCompilerOptions(templateTsconfigPath) || {};
}

function createStream(src) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
// tsconfig is not valid josn file
"target": "esnext",
"moduleResolution": "node",
"jsx": "preserve",
Expand Down
2 changes: 1 addition & 1 deletion packages/father-build/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const successValidates = {
overridesByEntry: [{}],
doc: [{}],
typescriptOpts: [{}],
pkgs: ['a'],
pkgs: [[]],
};

Object.keys(successValidates).forEach(key => {
Expand Down

0 comments on commit a365375

Please sign in to comment.