Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: Lazy require 'typescript' module. (#72)
Browse files Browse the repository at this point in the history
* [fix] Lazy require 'typescript' module.

* [refactor] Be slightly less lazy when requiring 'typescript'

* [fix] Pretend like I know what I am doing in TypeScript.

* chore: clean up imports
  • Loading branch information
indexzero authored and jdx committed Jan 31, 2019
1 parent daf9ca4 commit 49ed0bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@types/indent-string": "^3.2.0",
"@types/lodash": "^4.14.120",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.19",
"@types/node": "^10.12.20",
"@types/proxyquire": "^1.3.28",
"@types/wrap-ansi": "^3.0.0",
"chai": "^4.2.0",
Expand Down
15 changes: 10 additions & 5 deletions src/ts-node.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import * as fs from 'fs'
import * as path from 'path'
import * as TSNode from 'ts-node'
import {parseConfigFileTextToJson} from 'typescript'

import Debug from './debug'
const debug = Debug()

let typescript: typeof import('typescript')
try {
typescript = require('typescript')
} catch (ex) {
debug('Cannot find typescript', ex)
}

const tsconfigs: {[root: string]: TSConfig} = {}
const rootDirs: string[] = []
const typeRoots = [`${__dirname}/../node_modules/@types`]

const debug = Debug()

export interface TSConfig {
compilerOptions: {
rootDir?: string
Expand Down Expand Up @@ -60,8 +65,8 @@ function registerTSNode(root: string) {

function loadTSConfig(root: string): TSConfig | undefined {
const tsconfigPath = path.join(root, 'tsconfig.json')
if (fs.existsSync(tsconfigPath)) {
const tsconfig = parseConfigFileTextToJson(
if (fs.existsSync(tsconfigPath) && typescript) {
const tsconfig = typescript.parseConfigFileTextToJson(
tsconfigPath,
fs.readFileSync(tsconfigPath, 'utf8')
).config
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@^10.12.19":
version "10.12.19"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.19.tgz#ca1018c26be01f07e66ac7fefbeb6407e4490c61"
integrity sha512-2NVovndCjJQj6fUUn9jCgpP4WSqr+u1SoUZMZyJkhGeBFsm6dE46l31S7lPUYt9uQ28XI+ibrJA1f5XyH5HNtA==
"@types/node@*", "@types/node@^10.12.20":
version "10.12.20"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.20.tgz#f79f959acd3422d0889bd1ead1664bd2d17cd367"
integrity sha512-9spv6SklidqxevvZyOUGjZVz4QRXGu2dNaLyXIFzFYZW0AGDykzPRIUFJXTlQXyfzAucddwTcGtJNim8zqSOPA==

"@types/proxyquire@^1.3.28":
version "1.3.28"
Expand Down

0 comments on commit 49ed0bd

Please sign in to comment.