(ts: typeof typescript, p: string)
| 11 | * @see https://github.com/ionic-team/stencil/blob/HEAD/src/compiler/sys/node-require.ts |
| 12 | */ |
| 13 | export const requireTS = (ts: typeof typescript, p: string): unknown => { |
| 14 | const id = resolve(p); |
| 15 | |
| 16 | delete require.cache[id]; |
| 17 | |
| 18 | require.extensions['.ts'] = (module: NodeModuleWithCompile, fileName: string) => { |
| 19 | let sourceText = readFileSync(fileName, 'utf8'); |
| 20 | |
| 21 | if (fileName.endsWith('.ts')) { |
| 22 | const tsResults = ts.transpileModule(sourceText, { |
| 23 | fileName, |
| 24 | compilerOptions: { |
| 25 | module: ts.ModuleKind.CommonJS, |
| 26 | moduleResolution: ts.ModuleResolutionKind.NodeJs, |
| 27 | esModuleInterop: true, |
| 28 | strict: true, |
| 29 | target: ts.ScriptTarget.ES2017, |
| 30 | }, |
| 31 | reportDiagnostics: true, |
| 32 | }); |
| 33 | sourceText = tsResults.outputText; |
| 34 | } else { |
| 35 | // quick hack to turn a modern es module |
| 36 | // into and old school commonjs module |
| 37 | sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1'); |
| 38 | } |
| 39 | |
| 40 | module._compile?.(sourceText, fileName); |
| 41 | }; |
| 42 | |
| 43 | const m = require(id); // eslint-disable-line @typescript-eslint/no-var-requires |
| 44 | |
| 45 | delete require.extensions['.ts']; |
| 46 | |
| 47 | return m; |
| 48 | }; |
| 49 | |
| 50 | export function resolveNode(root: string, ...pathSegments: string[]): string | null { |
| 51 | try { |
no test coverage detected