Skip to content

Commit

Permalink
Avoid processing files from config by default
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jun 22, 2018
1 parent f260591 commit 33cb1b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

> TypeScript execution and REPL for node.js, with source map support. **Works with `typescript@>=2.0`**.
**Tip:** `ts-node` differs slightly from `tsc`. It will not load files from `tsconfig.json` by default. Instead, `ts-node` starts from the input file and discovers the rest of the project tree through imports and references.

## Installation

```sh
Expand Down
30 changes: 18 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export interface TypeInfo {
* Default register options.
*/
export const DEFAULTS: Options = {
files: yn(process.env['TS_NODE_FILES'], { default: true }),
files: yn(process.env['TS_NODE_FILES']),
cache: yn(process.env['TS_NODE_CACHE'], { default: true }),
pretty: yn(process.env['TS_NODE_PRETTY']),
cacheDirectory: process.env['TS_NODE_CACHE_DIRECTORY'],
Expand Down Expand Up @@ -231,10 +231,18 @@ export function register (opts: Options = {}): Register {
const config = readConfig(cwd, ts, fileExists, readFile, compilerOptions, project, skipProject)
const configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics)
const extensions = ['.ts', '.tsx']
const fileNames = options.files ? config.fileNames : []

const cachedir = join(
resolve(cwd, cacheDirectory),
getCompilerDigest({ version: ts.version, typeCheck, ignoreDiagnostics, config, compiler })
getCompilerDigest({
version: ts.version,
options: config.options,
fileNames,
typeCheck,
ignoreDiagnostics,
compiler
})
)

const diagnosticHost: _ts.FormatDiagnosticsHost = {
Expand Down Expand Up @@ -262,12 +270,8 @@ export function register (opts: Options = {}): Register {
extensions.push('.jsx')
}

// Add all files into the file hash.
if (options.files) {
for (const fileName of config.fileNames) {
memoryCache.versions[fileName] = 1
}
}
// Initialize files from TypeScript into project.
for (const path of fileNames) memoryCache.versions[path] = 1

/**
* Get the extension for a transpiled file.
Expand Down Expand Up @@ -442,7 +446,7 @@ function registerExtension (
/**
* Do post-processing on config options to support `ts-node`.
*/
function fixConfig (ts: TSCommon, config: any) {
function fixConfig (ts: TSCommon, config: _ts.ParsedCommandLine) {
// Delete options that *should not* be passed through.
delete config.options.out
delete config.options.outFile
Expand Down Expand Up @@ -474,7 +478,7 @@ function readConfig (
compilerOptions?: object,
project?: string | null,
noProject?: boolean | null
) {
): _ts.ParsedCommandLine {
let config = { compilerOptions: {} }
let basePath = normalizeSlashes(cwd)
let configFileName: string | undefined = undefined
Expand All @@ -489,7 +493,9 @@ function readConfig (
const result = ts.readConfigFile(configFileName, readFile)

// Return diagnostics.
if (result.error) return { errors: [result.error] }
if (result.error) {
return { errors: [result.error], fileNames: [], options: {} }
}

config = result.config
basePath = normalizeSlashes(dirname(configFileName))
Expand Down Expand Up @@ -586,7 +592,7 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
function getCacheName (sourceCode: string, fileName: string) {
return crypto.createHash('sha256')
.update(extname(fileName), 'utf8')
.update('\x001\x00', 'utf8') // Store "cache version" in hash.
.update('\x00', 'utf8')
.update(sourceCode, 'utf8')
.digest('hex')
}
Expand Down

4 comments on commit 33cb1b5

@felixfbecker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After updating to v7 I get errors like this:

TSError: ⨯ Unable to compile TypeScript:
src/util/url.test.tsx(3,33): error TS7016: Could not find a declaration file for module 'whatwg-url'. '/Users/felix/go/src/github.com/sourcegraph/sourcegraph/web/node_modules/whatwg-url/lib/public-api.js' implicitly has an 'any' type.
  Try `npm install @types/whatwg-url` if it exists or add a new declaration (.d.ts) file containing `declare module 'whatwg-url';`

This is because the typings are in custom declaration files that are now not anymore auto-loaded. How to solve this?

I would like to avoid triple-slash references or listing out these files on the command line. tsconfig should be the single source of truth

@blakeembrey
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use paths or types. It’s mentioned in the README. This was the change recommended by TypeScript and agreed with it, the fact that your definitions previously worked and don’t now is the reason it was a major release.

@felixfbecker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it to work after changing include to typeRoots, changing all definitions to be in their own folder, named after the package, and the file being named index.d.ts (anything else wouldn't work).

I would be nice if that note to the release notes as a migration hint - when getting automatic update PRs, the release notes are included in the PR so that helps a lot. The next thing I found was this commit, after which I still wasn't sure how to fix it. Thanks for your help.

@blakeembrey
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.