Comparing version 8.0.2 to 8.0.3
@@ -17,3 +17,2 @@ "use strict"; | ||
var path_1 = require("path"); | ||
var os_1 = require("os"); | ||
var sourceMapSupport = require("source-map-support"); | ||
@@ -34,4 +33,5 @@ var yn_1 = require("yn"); | ||
function (key, fn) { | ||
var i = 0; | ||
return function (x) { | ||
debug(key, x); | ||
debug(key, x, ++i); | ||
return fn(x); | ||
@@ -46,2 +46,18 @@ }; | ||
/** | ||
* Track the project information. | ||
*/ | ||
var MemoryCache = /** @class */ (function () { | ||
function MemoryCache(rootFileNames) { | ||
if (rootFileNames === void 0) { rootFileNames = []; } | ||
this.rootFileNames = rootFileNames; | ||
this.fileContents = new Map(); | ||
this.fileVersions = new Map(); | ||
for (var _i = 0, rootFileNames_1 = rootFileNames; _i < rootFileNames_1.length; _i++) { | ||
var fileName = rootFileNames_1[_i]; | ||
this.fileVersions.set(fileName, 1); | ||
} | ||
} | ||
return MemoryCache; | ||
}()); | ||
/** | ||
* Default register options. | ||
@@ -127,19 +143,7 @@ */ | ||
].concat((options.ignoreDiagnostics || [])).map(Number); | ||
var memoryCache = { | ||
contents: Object.create(null), | ||
versions: Object.create(null), | ||
outputs: Object.create(null) | ||
}; | ||
var ignore = options.skipIgnore ? [] : (options.ignore || ['/node_modules/']).map(function (str) { return new RegExp(str); }); | ||
// Install source map support and read from memory cache. | ||
sourceMapSupport.install({ | ||
environment: 'node', | ||
retrieveFile: function (path) { | ||
return memoryCache.outputs[path]; | ||
} | ||
}); | ||
// Require the TypeScript compiler and configuration. | ||
var cwd = process.cwd(); | ||
var typeCheck = options.typeCheck === true || options.transpileOnly !== true; | ||
var compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd] }); | ||
var compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd, __dirname] }); | ||
var ts = require(compiler); | ||
@@ -151,9 +155,17 @@ var transformers = options.transformers || undefined; | ||
var configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics); | ||
var extensions = ['.ts', '.tsx']; | ||
var extensions = ['.ts']; | ||
var outputCache = new Map(); | ||
var diagnosticHost = { | ||
getNewLine: function () { return os_1.EOL; }, | ||
getNewLine: function () { return ts.sys.newLine; }, | ||
getCurrentDirectory: function () { return cwd; }, | ||
getCanonicalFileName: function (path) { return path; } | ||
}; | ||
var formatDiagnostics = options.pretty | ||
// Install source map support and read from memory cache. | ||
sourceMapSupport.install({ | ||
environment: 'node', | ||
retrieveFile: function (path) { | ||
return outputCache.get(path) || ''; | ||
} | ||
}); | ||
var formatDiagnostics = process.stdout.isTTY || options.pretty | ||
? ts.formatDiagnosticsWithColorAndContext | ||
@@ -169,12 +181,9 @@ : ts.formatDiagnostics; | ||
throw createTSError(configDiagnosticList); | ||
// Enable `allowJs` when flag is set. | ||
if (config.options.allowJs) { | ||
// Enable additional extensions when JSX or `allowJs` is enabled. | ||
if (config.options.jsx) | ||
extensions.push('.tsx'); | ||
if (config.options.allowJs) | ||
extensions.push('.js'); | ||
if (config.options.jsx && config.options.allowJs) | ||
extensions.push('.jsx'); | ||
} | ||
// Initialize files from TypeScript into project. | ||
for (var _i = 0, _a = config.fileNames; _i < _a.length; _i++) { | ||
var path = _a[_i]; | ||
memoryCache.versions[path] = 1; | ||
} | ||
/** | ||
@@ -209,29 +218,20 @@ * Get the extension for a transpiled file. | ||
if (typeCheck) { | ||
// Set the file contents into cache. | ||
var updateMemoryCache_1 = function (code, fileName) { | ||
if (memoryCache.contents[fileName] !== code) { | ||
memoryCache.contents[fileName] = code; | ||
memoryCache.versions[fileName] = (memoryCache.versions[fileName] || 0) + 1; | ||
} | ||
}; | ||
var memoryCache_1 = new MemoryCache(config.fileNames); | ||
// Create the compiler host for type checking. | ||
var serviceHost = { | ||
getScriptFileNames: function () { return Object.keys(memoryCache.versions); }, | ||
getScriptFileNames: function () { return memoryCache_1.rootFileNames; }, | ||
getScriptVersion: function (fileName) { | ||
var version = memoryCache.versions[fileName]; | ||
// We need to return `undefined` and not a string here because TypeScript will use | ||
// `getScriptVersion` and compare against their own version - which can be `undefined`. | ||
// If we don't return `undefined` it results in `undefined === "undefined"` and run | ||
// `createProgram` again (which is very slow). Using a `string` assertion here to avoid | ||
// TypeScript errors from the function signature (expects `(x: string) => string`). | ||
return version === undefined ? undefined : String(version); | ||
var version = memoryCache_1.fileVersions.get(fileName); | ||
return version === undefined ? '' : version.toString(); | ||
}, | ||
getScriptSnapshot: function (fileName) { | ||
var contents = memoryCache_1.fileContents.get(fileName); | ||
// Read contents into TypeScript memory cache. | ||
if (!Object.prototype.hasOwnProperty.call(memoryCache.contents, fileName)) { | ||
memoryCache.contents[fileName] = readFile(fileName); | ||
if (contents === undefined) { | ||
contents = readFile(fileName); | ||
if (contents === undefined) | ||
return; | ||
memoryCache_1.fileVersions.set(fileName, 1); | ||
memoryCache_1.fileContents.set(fileName, contents); | ||
} | ||
var contents = memoryCache.contents[fileName]; | ||
if (contents === undefined) | ||
return; | ||
return ts.ScriptSnapshot.fromString(contents); | ||
@@ -244,3 +244,5 @@ }, | ||
directoryExists: debugFn('directoryExists', ts.sys.directoryExists), | ||
getNewLine: function () { return os_1.EOL; }, | ||
realpath: debugFn('realpath', ts.sys.realpath), | ||
getNewLine: function () { return ts.sys.newLine; }, | ||
useCaseSensitiveFileNames: function () { return ts.sys.useCaseSensitiveFileNames; }, | ||
getCurrentDirectory: function () { return cwd; }, | ||
@@ -251,12 +253,23 @@ getCompilationSettings: function () { return config.options; }, | ||
}; | ||
var service_1 = ts.createLanguageService(serviceHost); | ||
var registry = ts.createDocumentRegistry(ts.sys.useCaseSensitiveFileNames, cwd); | ||
var service_1 = ts.createLanguageService(serviceHost, registry); | ||
// Set the file contents into cache manually. | ||
var updateMemoryCache_1 = function (contents, fileName) { | ||
var fileVersion = memoryCache_1.fileVersions.get(fileName) || 0; | ||
// Add to `rootFiles` when discovered for the first time. | ||
if (fileVersion === 0) | ||
memoryCache_1.rootFileNames.push(fileName); | ||
// Avoid incrementing cache when nothing has changed. | ||
if (memoryCache_1.fileContents.get(fileName) === contents) | ||
return; | ||
memoryCache_1.fileVersions.set(fileName, fileVersion + 1); | ||
memoryCache_1.fileContents.set(fileName, contents); | ||
}; | ||
getOutput = function (code, fileName, lineOffset) { | ||
if (lineOffset === void 0) { lineOffset = 0; } | ||
// Must set memory cache before attempting to read file. | ||
updateMemoryCache_1(code, fileName); | ||
var output = service_1.getEmitOutput(fileName); | ||
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`. | ||
var diagnostics = service_1.getCompilerOptionsDiagnostics() | ||
.concat(service_1.getSyntacticDiagnostics(fileName)) | ||
.concat(service_1.getSemanticDiagnostics(fileName)); | ||
var diagnostics = service_1.getSemanticDiagnostics(fileName) | ||
.concat(service_1.getSyntacticDiagnostics(fileName)); | ||
var diagnosticList = filterDiagnostics(diagnostics, ignoreDiagnostics); | ||
@@ -290,3 +303,3 @@ if (diagnosticList.length) | ||
var output = updateOutput(value, fileName, sourceMap, getExtension); | ||
memoryCache.outputs[fileName] = output; | ||
outputCache.set(fileName, output); | ||
return output; | ||
@@ -293,0 +306,0 @@ } |
{ | ||
"name": "ts-node", | ||
"version": "8.0.2", | ||
"version": "8.0.3", | ||
"description": "TypeScript execution environment and REPL for node.js, with source map support", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -17,7 +17,8 @@ # ![TypeScript Node](logo.svg) | ||
# Or globally. | ||
# Or globally with TypeScript. | ||
npm install -g ts-node | ||
npm install -g typescript | ||
``` | ||
**Tip:** Installing modules locally allows you to control and share the versions through `package.json`. | ||
**Tip:** Installing modules locally allows you to control and share the versions through `package.json`. TS Node will always resolve the compiler from `cwd` before checking relative to its own installation. | ||
@@ -91,2 +92,4 @@ ## Usage | ||
**Note:** If you are using the `--project <tsconfig.json>` command line argument as per the [Configuration Options](#configuration-options), and want to apply this same behavior when launching in VS Code, add an "env" key into the launch configuration: `"env": { "TS_NODE_PROJECT": "<tsconfig.json>" }`. | ||
## How It Works | ||
@@ -191,2 +194,9 @@ | ||
An alternative approach for definitions of third-party libraries are [triple-slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html). This may be helpful if you prefer not to change your TypeScript `compilerOptions` or structure your custom type definitions when using `typeRoots`. Below is an example of the triple-slash directive as a relative path within your project: | ||
```typescript | ||
/// <reference types="./types/untyped_js_lib" /> | ||
import UntypedJsLib from "untyped_js_lib" | ||
``` | ||
**Tip:** If you _must_ use `files`, enable `--files` flags or set `TS_NODE_FILES=true`. | ||
@@ -193,0 +203,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125214
1101
219