Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse tsconfig.json error #157

Merged
merged 3 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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