Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v8.3.0
Choose a base ref
...
head repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v8.4.0
Choose a head ref
  • 4 commits
  • 5 files changed
  • 4 contributors

Commits on Sep 15, 2019

  1. Introduce transformers program support (#879)

    Mikhail Bashurov authored and blakeembrey committed Sep 15, 2019

    Verified

    This commit was signed with the committer’s verified signature.
    adamrusted Adam Rusted
    Copy the full SHA
    12ff53d View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ebbcf39 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    b01b629 View commit details
  4. 8.4.0

    blakeembrey committed Sep 15, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    3efdea4 View commit details
Showing with 59 additions and 32 deletions.
  1. +3 −1 README.md
  2. +1 −1 package-lock.json
  3. +1 −1 package.json
  4. +9 −0 src/index.spec.ts
  5. +45 −29 src/index.ts
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -114,6 +114,8 @@ You can set options by passing them before the script path, via programmatic usa
ts-node --compiler ntypescript --project src/tsconfig.json hello-world.ts
```

**Note:** [`ntypescript`](https://github.com/TypeStrong/ntypescript#readme) is an example of a TypeScript compatible `compiler`.

### CLI Options

Supports `--print`, `--eval` and `--require` from [node.js CLI options](https://nodejs.org/api/cli.html).
@@ -140,7 +142,7 @@ _Environment variable denoted in parentheses._

### Programmatic Only Options

* `transformers` An array of transformers to pass to TypeScript
* `transformers` `_ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)` An object with transformers or a function that accepts a program and returns an transformers object to pass to TypeScript. Function isn't available with `transpileOnly` flag
* `readFile` Custom TypeScript-compatible file reading function
* `fileExists` Custom TypeScript-compatible file existence function

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-node",
"version": "8.3.0",
"version": "8.4.0",
"description": "TypeScript execution environment and REPL for node.js, with source map support",
"main": "dist/index.js",
"types": "dist/index.d.ts",
9 changes: 9 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -288,6 +288,15 @@ describe('ts-node', function () {
})
})

it('should import ts before js when TS_NODE_PREFER_TS_EXTS env is present', function (done) {
exec(`${BIN_EXEC} tests/import-order/compiled`, { env: { ...process.env, TS_NODE_PREFER_TS_EXTS: 'true' } }, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('Hello, TypeScript!\n')

return done()
})
})

it('should ignore .d.ts files', function (done) {
exec(`${BIN_EXEC} tests/import-order/importer`, function (err, stdout) {
expect(err).to.equal(null)
74 changes: 45 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ export interface Options {
ignoreDiagnostics?: Array<number | string>
readFile?: (path: string) => string | undefined
fileExists?: (path: string) => boolean
transformers?: _ts.CustomTransformers
transformers?: _ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)
}

/**
@@ -274,32 +274,23 @@ export function register (opts: Options = {}): Register {
/**
* Create the basic required function using transpile mode.
*/
let getOutput = function (code: string, fileName: string, lineOffset = 0): SourceOutput {
const result = ts.transpileModule(code, {
fileName,
transformers,
compilerOptions: config.options,
reportDiagnostics: true
})

const diagnosticList = result.diagnostics ?
filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
[]

if (diagnosticList.length) reportTSError(configDiagnosticList)

return [result.outputText, result.sourceMapText as string]
}

let getTypeInfo = function (_code: string, _fileName: string, _position: number): TypeInfo {
throw new TypeError(`Type information is unavailable without "--type-check"`)
}
let getOutput: (code: string, fileName: string, lineOffset: number) => SourceOutput
let getTypeInfo: (_code: string, _fileName: string, _position: number) => TypeInfo

// Use full language services when the fast option is disabled.
if (typeCheck) {
const memoryCache = new MemoryCache(config.fileNames)
const cachedReadFile = cachedLookup(debugFn('readFile', readFile))

const getCustomTransformers = () => {
if (typeof transformers === 'function') {
const program = service.getProgram()
return program ? transformers(program) : undefined
}

return transformers
}

// Create the compiler host for type checking.
const serviceHost: _ts.LanguageServiceHost = {
getScriptFileNames: () => Array.from(memoryCache.fileVersions.keys()),
@@ -331,14 +322,14 @@ export function register (opts: Options = {}): Register {
getCurrentDirectory: () => cwd,
getCompilationSettings: () => config.options,
getDefaultLibFileName: () => ts.getDefaultLibFilePath(config.options),
getCustomTransformers: () => transformers
getCustomTransformers: getCustomTransformers
}

const registry = ts.createDocumentRegistry(ts.sys.useCaseSensitiveFileNames, cwd)
const service = ts.createLanguageService(serviceHost, registry)

// Set the file contents into cache manually.
const updateMemoryCache = function (contents: string, fileName: string) {
const updateMemoryCache = (contents: string, fileName: string) => {
const fileVersion = memoryCache.fileVersions.get(fileName) || 0

// Avoid incrementing cache when nothing has changed.
@@ -348,7 +339,7 @@ export function register (opts: Options = {}): Register {
memoryCache.fileContents.set(fileName, contents)
}

getOutput = function (code: string, fileName: string, lineOffset: number = 0) {
getOutput = (code: string, fileName: string) => {
updateMemoryCache(code, fileName)

const output = service.getEmitOutput(fileName)
@@ -379,7 +370,7 @@ export function register (opts: Options = {}): Register {
return [output.outputFiles[1].text, output.outputFiles[0].text]
}

getTypeInfo = function (code: string, fileName: string, position: number) {
getTypeInfo = (code: string, fileName: string, position: number) => {
updateMemoryCache(code, fileName)

const info = service.getQuickInfoAtPosition(fileName, position)
@@ -388,10 +379,35 @@ export function register (opts: Options = {}): Register {

return { name, comment }
}
} else {
if (typeof transformers === 'function') {
throw new TypeError('Transformers function is unavailable in "--transpile-only"')
}

getOutput = (code: string, fileName: string): SourceOutput => {
const result = ts.transpileModule(code, {
fileName,
transformers,
compilerOptions: config.options,
reportDiagnostics: true
})

const diagnosticList = result.diagnostics ?
filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
[]

if (diagnosticList.length) reportTSError(configDiagnosticList)

return [result.outputText, result.sourceMapText as string]
}

getTypeInfo = () => {
throw new TypeError('Type information is unavailable in "--transpile-only"')
}
}

// Create a simple TypeScript compiler proxy.
function compile (code: string, fileName: string, lineOffset?: number) {
function compile (code: string, fileName: string, lineOffset = 0) {
const [value, sourceMap] = getOutput(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)
outputCache.set(fileName, output)
@@ -401,7 +417,7 @@ export function register (opts: Options = {}): Register {
const register: Register = { cwd, compile, getTypeInfo, extensions, ts }

// Register the extensions.
registerExtensions(opts, extensions, ignore, register, originalJsHandler)
registerExtensions(options.preferTsExts, extensions, ignore, register, originalJsHandler)

return register
}
@@ -430,7 +446,7 @@ function reorderRequireExtension (ext: string) {
* Register the extensions to support when importing files.
*/
function registerExtensions (
opts: Options,
preferTsExts: boolean | null | undefined,
extensions: string[],
ignore: RegExp[],
register: Register,
@@ -441,7 +457,7 @@ function registerExtensions (
registerExtension(ext, ignore, register, originalJsHandler)
}

if (opts.preferTsExts) {
if (preferTsExts) {
// tslint:disable-next-line
const preferredExtensions = new Set([...extensions, ...Object.keys(require.extensions)])