Skip to content

Commit be8e2e1

Browse files
committed
Expose create function based on register
1 parent 06c7751 commit be8e2e1

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

src/index.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'path'
44
import semver = require('semver')
55
import ts = require('typescript')
66
import proxyquire = require('proxyquire')
7-
import { register, VERSION } from './index'
7+
import { register, create, VERSION } from './index'
88

99
const TEST_DIR = join(__dirname, '../tests')
1010
const PROJECT = join(TEST_DIR, semver.gte(ts.version, '2.5.0') ? 'tsconfig.json5' : 'tsconfig.json')
@@ -486,4 +486,13 @@ describe('ts-node', function () {
486486
})
487487
})
488488
})
489+
490+
describe('create', () => {
491+
it('should create generic compiler instances', () => {
492+
const service = create({ compilerOptions: { target: 'es5' }, skipProject: true })
493+
const output = service.compile('const x = 10', 'test.ts')
494+
495+
expect(output).to.contain('var x = 10;')
496+
})
497+
})
489498
})

src/index.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { relative, basename, extname, resolve, dirname, join } from 'path'
1+
import { relative, basename, extname, resolve, dirname } from 'path'
22
import sourceMapSupport = require('source-map-support')
33
import yn from 'yn'
44
import { BaseError } from 'make-error'
@@ -10,20 +10,13 @@ import * as _ts from 'typescript'
1010
*/
1111
export const REGISTER_INSTANCE = Symbol.for('ts-node.register.instance')
1212

13-
/**
14-
* Expose `ts-node` instance information globally for consumers.
15-
*/
16-
export interface RegisterInstance {
17-
config: _ts.ParsedCommandLine
18-
}
19-
2013
/**
2114
* Expose `REGISTER_INSTANCE` information on node.js `process`.
2215
*/
2316
declare global {
2417
namespace NodeJS {
2518
interface Process {
26-
[REGISTER_INSTANCE]?: RegisterInstance
19+
[REGISTER_INSTANCE]?: Register
2720
}
2821
}
2922
}
@@ -76,9 +69,9 @@ export interface TSCommon {
7669
export const VERSION = require('../package.json').version
7770

7871
/**
79-
* Registration options.
72+
* Options for creating a new TypeScript compiler instance.
8073
*/
81-
export interface Options {
74+
export interface CreateOptions {
8275
cwd?: string
8376
scope?: boolean | null
8477
pretty?: boolean | null
@@ -99,6 +92,13 @@ export interface Options {
9992
transformers?: _ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)
10093
}
10194

95+
/**
96+
* Options for registering a TypeScript compiler instance globally.
97+
*/
98+
export interface RegisterOptions extends CreateOptions {
99+
preferTsExts?: boolean | null
100+
}
101+
102102
/**
103103
* Track the project information.
104104
*/
@@ -122,7 +122,7 @@ export interface TypeInfo {
122122
/**
123123
* Default register options.
124124
*/
125-
export const DEFAULTS: Options = {
125+
export const DEFAULTS: RegisterOptions = {
126126
cwd: process.env.TS_NODE_CWD,
127127
scope: yn(process.env.TS_NODE_SCOPE),
128128
files: yn(process.env['TS_NODE_FILES']),
@@ -196,8 +196,8 @@ export class TSError extends BaseError {
196196
*/
197197
export interface Register {
198198
cwd: string
199-
extensions: string[]
200199
ts: TSCommon
200+
config: _ts.ParsedCommandLine
201201
enabled (enabled?: boolean): boolean
202202
ignored (fileName: string): boolean
203203
compile (code: string, fileName: string, lineOffset?: number): string
@@ -220,12 +220,32 @@ function cachedLookup <T> (fn: (arg: string) => T): (arg: string) => T {
220220
}
221221

222222
/**
223-
* Register TypeScript compiler.
223+
* Register TypeScript compiler instance onto node.js
224224
*/
225-
export function register (opts: Options = {}): Register {
225+
export function register (opts: RegisterOptions = {}): Register {
226226
const options = Object.assign({}, DEFAULTS, opts)
227227
const originalJsHandler = require.extensions['.js'] // tslint:disable-line
228+
const service = create(options)
229+
const extensions = ['.ts']
228230

231+
// Enable additional extensions when JSX or `allowJs` is enabled.
232+
if (service.config.options.jsx) extensions.push('.tsx')
233+
if (service.config.options.allowJs) extensions.push('.js')
234+
if (service.config.options.jsx && service.config.options.allowJs) extensions.push('.jsx')
235+
236+
// Expose registered instance globally.
237+
process[REGISTER_INSTANCE] = service
238+
239+
// Register the extensions.
240+
registerExtensions(options.preferTsExts, extensions, service, originalJsHandler)
241+
242+
return service
243+
}
244+
245+
/**
246+
* Create TypeScript compiler instance.
247+
*/
248+
export function create (options: CreateOptions = {}): Register {
229249
const ignoreDiagnostics = [
230250
6059, // "'rootDir' is expected to contain all source files."
231251
18002, // "The 'files' list in config file is empty."
@@ -248,7 +268,6 @@ export function register (opts: Options = {}): Register {
248268
const fileExists = options.fileExists || ts.sys.fileExists
249269
const config = readConfig(cwd, ts, fileExists, readFile, options)
250270
const configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics)
251-
const extensions = ['.ts']
252271
const outputCache = new Map<string, string>()
253272

254273
const diagnosticHost: _ts.FormatDiagnosticsHost = {
@@ -289,11 +308,6 @@ export function register (opts: Options = {}): Register {
289308
// Render the configuration errors.
290309
if (configDiagnosticList.length) reportTSError(configDiagnosticList)
291310

292-
// Enable additional extensions when JSX or `allowJs` is enabled.
293-
if (config.options.jsx) extensions.push('.tsx')
294-
if (config.options.allowJs) extensions.push('.js')
295-
if (config.options.jsx && config.options.allowJs) extensions.push('.jsx')
296-
297311
/**
298312
* Get the extension for a transpiled file.
299313
*/
@@ -450,15 +464,8 @@ export function register (opts: Options = {}): Register {
450464
let active = true
451465
const enabled = (enabled?: boolean) => enabled === undefined ? active : (active = !!enabled)
452466
const ignored = (fileName: string) => !active || !isScoped(fileName) || shouldIgnore(fileName, ignore)
453-
const register: Register = { cwd, compile, getTypeInfo, extensions, ts, ignored, enabled }
454-
455-
// Expose registered instance globally.
456-
process[REGISTER_INSTANCE] = { config }
457-
458-
// Register the extensions.
459-
registerExtensions(options.preferTsExts, extensions, register, originalJsHandler)
460467

461-
return register
468+
return { cwd, ts, config, compile, getTypeInfo, ignored, enabled }
462469
}
463470

464471
/**
@@ -563,7 +570,7 @@ function readConfig (
563570
ts: TSCommon,
564571
fileExists: (path: string) => boolean,
565572
readFile: (path: string) => string | undefined,
566-
options: Options
573+
options: CreateOptions
567574
): _ts.ParsedCommandLine {
568575
let config: any = { compilerOptions: {} }
569576
let basePath = normalizeSlashes(cwd)

0 commit comments

Comments
 (0)