Skip to content

Commit

Permalink
WIP add ability to compose register(create())
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Sep 26, 2021
1 parent a44e759 commit a550e0b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -1225,6 +1242,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
}

return {
[TS_NODE_SERVICE_BRAND]: true,
ts,
config,
compile,
Expand Down

0 comments on commit a550e0b

Please sign in to comment.