-
Notifications
You must be signed in to change notification settings - Fork 293
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
add --tsconfig-path
option for CLI & support extends
property in tsconfig.json
#914
Open
nokazn
wants to merge
8
commits into
vercel:main
Choose a base branch
from
nokazn:feature/#457/specify-tsconfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
67f25b5
fix: load tsconfig.json in official TypeScript way
nokazn 8d22593
feat: add `--tsconfig-path` option as a CLI arg to specify path to `t…
nokazn 3302d61
fix: convert & normalize enum key in compilerOptions at `tsconfig.json`
nokazn bec1a97
test: add test patterns for `--tsconfig-path` CLI option
nokazn 6e22085
Merge branch 'main' into feature/#457/specify-tsconfig
nokazn 5d85cd2
refactor: modify way of parsing `tsconfig.json`
nokazn c0752a1
Merge branch 'main' into feature/#457/specify-tsconfig
nokazn dcc5c8c
Merge branch 'main' into feature/#457/specify-tsconfig
nokazn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
const ts = require('typescript'); | ||
const { join, dirname, resolve } = require('path'); | ||
const fs = require('fs'); | ||
const { paramCase } = require('param-case'); | ||
|
||
/** | ||
* @typedef {object} LoadTsconfigInit | ||
* @property {string} base | ||
* @property {string} start | ||
* @property {string} filename | ||
*/ | ||
|
||
/** | ||
* @description Adapted from https://github.com/vercel/vercel/blob/18bec983aefbe2a77bd14eda6fca59ff7e956d8b/packages/build-utils/src/fs/run-user-scripts.ts#L289-L310 | ||
* @param {LoadTsconfigInit} | ||
* @returns {string | null} | ||
*/ | ||
function walkParentDirs({ base, start, filename }) { | ||
let parent = ''; | ||
|
||
for (let current = start; base.length <= current.length; current = parent) { | ||
const fullPath = join(current, filename); | ||
|
||
if (fs.existsSync(fullPath)) { | ||
return fullPath; | ||
} | ||
|
||
parent = dirname(current); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param {ts.CompilerOptions} options | ||
* @param {string | undefined} key | ||
* @param {(value: string) => string} [callback] | ||
* @returns {string | undefined} | ||
*/ | ||
function convertEnumCompilerOptions(enumCompilerOptions, key, callback) { | ||
if (key == null) { | ||
return undefined; | ||
} | ||
const value = enumCompilerOptions[key]; | ||
return typeof callback === 'function' ? callback(value) : value; | ||
} | ||
|
||
/** | ||
* @param {string} value | ||
* @returns {string} | ||
*/ | ||
function toLowerCase(value) { | ||
return value.toLowerCase(); | ||
} | ||
|
||
/** | ||
* @param {ts.NewLineKind} newLine | ||
* @returns {string | undefined} | ||
*/ | ||
function normalizeNewLineOption(newLine) { | ||
switch (newLine) { | ||
case ts.NewLineKind.CarriageReturnLineFeed: | ||
return 'crlf'; | ||
case ts.NewLineKind.LineFeed: | ||
return 'lf'; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* @param {ts.ModuleResolutionKind} moduleResolution | ||
* @returns {string | undefined} | ||
*/ | ||
function normalizeModuleResolutionOption(moduleResolution) { | ||
switch (moduleResolution) { | ||
case ts.ModuleResolutionKind.Classic: | ||
return 'classic'; | ||
case ts.ModuleResolutionKind.NodeJs: | ||
return 'node'; | ||
case ts.ModuleResolutionKind.Node12: | ||
return 'node12'; | ||
case ts.ModuleResolutionKind.NodeNext: | ||
return 'nodenext'; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* @param {ts.CompilerOptions} options | ||
* @returns {ts.CompilerOptions} | ||
*/ | ||
function normalizeCompilerOptions(options) { | ||
if (options.importsNotUsedAsValues != null) { | ||
options.importsNotUsedAsValues = convertEnumCompilerOptions( | ||
ts.ImportsNotUsedAsValues, | ||
options.importsNotUsedAsValues, | ||
toLowerCase, | ||
); | ||
} | ||
if (options.jsx != null) { | ||
options.jsx = convertEnumCompilerOptions(ts.JsxEmit, options.jsx, paramCase); | ||
} | ||
if (options.module != null) { | ||
options.module = convertEnumCompilerOptions(ts.ModuleKind, options.module, toLowerCase); | ||
} | ||
if (options.moduleResolution != null) { | ||
options.moduleResolution = normalizeModuleResolutionOption(options.moduleResolution); | ||
} | ||
if (options.newLine != null) { | ||
options.newLine = normalizeNewLineOption(options.newLine); | ||
} | ||
if (options.target != null) { | ||
options.target = convertEnumCompilerOptions(ts.ScriptTarget, options.target, toLowerCase); | ||
} | ||
return options; | ||
} | ||
|
||
/** | ||
* @param {string | undefined} configPath | ||
* @param {LoadTsconfigInit} | ||
* @returns {ts.CompilerOptions} | ||
*/ | ||
exports.loadTsconfigOptions = function (configPath, { base, start, filename }) { | ||
// throw error if `configPath` does not exist | ||
const tsconfig = configPath != null ? resolve(configPath) : walkParentDirs({ base, start, filename }); | ||
if (tsconfig == null) { | ||
return {}; | ||
} | ||
const content = ts.readConfigFile(tsconfig, ts.sys.readFile); | ||
if (content.error != null || content.config == null) { | ||
return {}; | ||
} | ||
const { options } = ts.parseJsonConfigFileContent(content.config, ts.sys, dirname(tsconfig)); | ||
return normalizeCompilerOptions(options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* throws error (TS7006: an implicit 'any' type) if strict options is set to true, or otherwise passes compilation | ||
*/ | ||
function something(args) { | ||
return args; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"strict": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem very future proof.
Is there a way to have TypeScript convert this for us?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@styfle
I see.
I cloudn't find a way to get complete config objects by TypeScript, but I found tsconfck from microsoft/TypeScript#44573.
Does this library look future proof, do you think? If you think it's OK, I will rewrite
src/utils/load-tsconfig-options.js
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrited this & related parts by 5d85cd2, which uses tsconfck.
Would be glad to review it.