Skip to content

Commit

Permalink
fix: fixed incorrect base path of tsconfig.json if it's inside a subd…
Browse files Browse the repository at this point in the history
…irectory
  • Loading branch information
JasonHK committed May 9, 2020
1 parent 46ee07f commit 0cfbf70
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/util/get-parsed-command-line/get-parsed-command-line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ensureAbsolute} from "../path/path-util";
import {dirname, ensureAbsolute} from "../path/path-util";
import {D_TS_EXTENSION, DEFAULT_TSCONFIG_FILE_NAME} from "../../constant/constant";
import {ParsedCommandLineResult} from "./parsed-command-line-result";
import {
Expand Down Expand Up @@ -122,20 +122,11 @@ export function getParsedCommandLine(options: GetParsedCommandLineOptions): Pars
throw new ReferenceError(`The given tsconfig: '${tsconfigPath}' doesn't exist!`);
}

originalCompilerOptions = typescript.parseJsonConfigFileContent(
typescript.parseConfigFileTextToJson(tsconfigPath, tsconfigContent).config,
fileSystem,
cwd,
{},
tsconfigPath
).options;
parsedCommandLine = typescript.parseJsonConfigFileContent(
typescript.parseConfigFileTextToJson(tsconfigPath, tsconfigContent).config,
fileSystem,
cwd,
forcedCompilerOptions,
tsconfigPath
);
const tsconfigJson = typescript.parseConfigFileTextToJson(tsconfigPath, tsconfigContent).config;
const basePath = tsconfigPath ? dirname(tsconfigPath) : cwd;

originalCompilerOptions = typescript.parseJsonConfigFileContent(tsconfigJson, fileSystem, basePath, {}, tsconfigPath).options;
parsedCommandLine = typescript.parseJsonConfigFileContent(tsconfigJson, fileSystem, basePath, forcedCompilerOptions, tsconfigPath);

// If an extension hook has been provided. Make sure to still apply the forced CompilerOptions
if (isTsConfigResolver(tsconfig)) {
Expand Down
67 changes: 67 additions & 0 deletions test/parse-tsconfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import test from "ava";
import {generateRollupBundle} from "./setup/setup-rollup";
import {formatCode} from "./util/format-code";

test("Correctly parse TypeScript config files within sub-directories. #1", async t => {
const bundle = await generateRollupBundle(
[
{
entry: true,
fileName: "virtual-src/index.ts",
text: `\
export function noop(): void {}
`
},
{
entry: false,
fileName: "virtual-configs/tsconfig.json",
text: `\
{
"compilerOptions": {
"target": "es2018",
"module": "esnext",
"moduleResolution": "node",
"baseUrl": "../",
"rootDir": "../virtual-src",
"outDir": "../virtual-dist",
"declaration": true,
"declarationMap": true,
"listEmittedFiles": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"sourceMap": true,
"strict": true
},
"include": [
"../virtual-src/**/*.ts"
],
"exclude": [
"../node_modules",
"../virtual-dist"
]
}
`
}
],
{
debug: false,
tsconfig: "virtual-configs/tsconfig.json"
}
);

const {
js: [file]
} = bundle;

t.deepEqual(
formatCode(file.code),
formatCode(`\
function noop() {}
export { noop };
`)
);
});

0 comments on commit 0cfbf70

Please sign in to comment.