* Compiles files to JavaScript. * * @param {string[]} files * @param {ts.CompilerOptions} options
(config: any, sourceDir: string)
| 26 | * @param {ts.CompilerOptions} options |
| 27 | */ |
| 28 | function createCompiler(config: any, sourceDir: string) { |
| 29 | return function compile(files: string[], targetDIR: string, options: Record<string, unknown>, possibleExtensions: string[]) { |
| 30 | const compilerOptions = { ...config.compilerOptions, ...options }; |
| 31 | const { baseUrl, paths } = compilerOptions; |
| 32 | const pathVerifyResult = verifyPaths(paths); |
| 33 | if (Array.isArray(pathVerifyResult)) { |
| 34 | const messages = pathVerifyResult.join('\n'); |
| 35 | throw new Error(messages); |
| 36 | } |
| 37 | const { exactPaths, wildCardPaths } = rankPaths(paths); |
| 38 | const host = ts.createCompilerHost(compilerOptions); |
| 39 | host.writeFile = function (fileName, contents, _writeByteOrderMark, onError, _sourceFiles) { |
| 40 | const isDts = fileName.endsWith('.d.ts'); |
| 41 | const relativeToSourceDir = relative(sourceDir, fileName); |
| 42 | const subDir = join(targetDIR, dirname(relativeToSourceDir)); |
| 43 | |
| 44 | mkdirSync(subDir, { recursive: true }); |
| 45 | |
| 46 | let path = join(targetDIR, relativeToSourceDir); |
| 47 | |
| 48 | if (!isDts && !fileName.endsWith('.map')) { |
| 49 | const astTree = parse(contents, { |
| 50 | ecmaVersion: 'latest', |
| 51 | sourceType: 'module', |
| 52 | ranges: true, |
| 53 | locations: false |
| 54 | }); |
| 55 | |
| 56 | switch (compilerOptions.module) { |
| 57 | case ts.ModuleKind.CommonJS: { |
| 58 | const requireStatements: any[] = []; |
| 59 | recursiveDescend( |
| 60 | astTree as any, |
| 61 | (node: any) => { |
| 62 | let flag = false; |
| 63 | if (node?.type === 'CallExpression') { |
| 64 | const callee = node?.callee; |
| 65 | if (callee?.type === 'Identifier' && callee?.name === 'require') { |
| 66 | flag = true; |
| 67 | } |
| 68 | } |
| 69 | if (flag && node?.arguments) { |
| 70 | for (const args of node?.arguments) { |
| 71 | if (args?.type === 'Literal') { |
| 72 | return node; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | }, |
| 77 | requireStatements |
| 78 | ); |
| 79 | const finalRequires = requireStatements |
| 80 | .filter((_node: any) => _node?.arguments) |
| 81 | .reduce((collector: any, _node: any) => { |
| 82 | const _requireStatements = _node.arguments.filter((args: any) => args?.type === 'Literal'); |
| 83 | collector.push(..._requireStatements); |
| 84 | return collector; |
| 85 | }, []); |
no test coverage detected