/
register.ts
35 lines (33 loc) · 1.23 KB
/
register.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { readDefaultTsConfig } from '@swc-node/register/read-default-tsconfig';
import { register } from '@swc-node/register/register';
import { join } from 'path';
/**
* Optionally, if swc-node and tsconfig-paths are available in the current workspace, apply the require
* register hooks so that .ts files can be used for writing custom workspace projects.
*
* If ts-node and tsconfig-paths are not available, the user can still provide an index.js file in
* the root of their project and the fundamentals will still work (but
* workspace path mapping will not, for example).
*/
export const registerTsProject = (
path: string,
configFilename = 'tsconfig.json'
) => {
try {
const tsConfig = readDefaultTsConfig(join(path, configFilename));
register(tsConfig);
/**
* Load the ts config from the source project
*/
const tsconfigPaths = require('tsconfig-paths');
const tsConfigResult = tsconfigPaths.loadConfig(path);
/**
* Register the custom workspace path mappings with node so that workspace libraries
* can be imported and used within project
*/
return tsconfigPaths.register({
baseUrl: tsConfigResult.absoluteBaseUrl,
paths: tsConfigResult.paths,
});
} catch (err) {}
};