From a550e0b5881f0c510ac50f5717a0a5a253b07862 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 25 Sep 2021 22:55:22 -0400 Subject: [PATCH] WIP add ability to compose register(create()) --- src/index.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index e1db16cac..382866269 100644 --- a/src/index.ts +++ b/src/index.ts @@ -427,10 +427,14 @@ export class TSError extends BaseError { } } +const TS_NODE_SERVICE_BRAND = Symbol('TS_NODE_SERVICE_BRAND'); + /** * Primary ts-node service, which wraps the TypeScript API and can compile TypeScript to JavaScript */ export interface Service { + /** @internal */ + [TS_NODE_SERVICE_BRAND]: true; ts: TSCommon; config: _ts.ParsedCommandLine; options: RegisterOptions; @@ -477,12 +481,25 @@ export function getExtensions(config: _ts.ParsedCommandLine) { return { tsExtensions, jsExtensions }; } +/** + * Create a new TypeScript compiler instance and register it onto node.js + */ +export function register(opts?: RegisterOptions): Service; /** * Register TypeScript compiler instance onto node.js */ -export function register(opts: RegisterOptions = {}): Service { +export function register(service: Service): Service; +export function register( + serviceOrOpts: Service | RegisterOptions | undefined +): Service { + // Is this a Service or a RegisterOptions? + let service = serviceOrOpts as Service; + if (!(serviceOrOpts as Service)?.[TS_NODE_SERVICE_BRAND]) { + // Not a service; is options + service = create((serviceOrOpts ?? {}) as RegisterOptions); + } + const originalJsHandler = require.extensions['.js']; - const service = create(opts); const { tsExtensions, jsExtensions } = getExtensions(service.config); const extensions = [...tsExtensions, ...jsExtensions]; @@ -1225,6 +1242,7 @@ export function create(rawOptions: CreateOptions = {}): Service { } return { + [TS_NODE_SERVICE_BRAND]: true, ts, config, compile,