|
| 1 | +import fs from 'fs'; |
| 2 | +import Module from 'module'; |
| 3 | +import { createFilesMatcher } from 'get-tsconfig'; |
| 4 | +import type { TransformOptions } from 'esbuild'; |
| 5 | +import { transformSync } from '../../utils/transform/index.js'; |
| 6 | +import { transformDynamicImport } from '../../utils/transform/transform-dynamic-import.js'; |
| 7 | +import { isESM } from '../../utils/esm-pattern.js'; |
| 8 | +import { shouldApplySourceMap, inlineSourceMap } from '../../source-map.js'; |
| 9 | +import { parent } from '../../utils/ipc/client.js'; |
| 10 | +import { tsconfig } from './utils.js'; |
| 11 | + |
| 12 | +const typescriptExtensions = [ |
| 13 | + '.cts', |
| 14 | + '.mts', |
| 15 | + '.ts', |
| 16 | + '.tsx', |
| 17 | + '.jsx', |
| 18 | +]; |
| 19 | + |
| 20 | +const transformExtensions = [ |
| 21 | + '.js', |
| 22 | + '.cjs', |
| 23 | + '.mjs', |
| 24 | +]; |
| 25 | + |
| 26 | +const fileMatcher = tsconfig && createFilesMatcher(tsconfig); |
| 27 | + |
| 28 | +// Clone Module._extensions with null prototype |
| 29 | +export const extensions = Object.assign(Object.create(null), Module._extensions); |
| 30 | + |
| 31 | +const defaultLoader = extensions['.js']; |
| 32 | + |
| 33 | +const transformer = ( |
| 34 | + module: Module, |
| 35 | + filePath: string, |
| 36 | +) => { |
| 37 | + // For tracking dependencies in watch mode |
| 38 | + if (parent?.send) { |
| 39 | + parent.send({ |
| 40 | + type: 'dependency', |
| 41 | + path: filePath, |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + const transformTs = typescriptExtensions.some(extension => filePath.endsWith(extension)); |
| 46 | + const transformJs = transformExtensions.some(extension => filePath.endsWith(extension)); |
| 47 | + if (!transformTs && !transformJs) { |
| 48 | + return defaultLoader(module, filePath); |
| 49 | + } |
| 50 | + |
| 51 | + let code = fs.readFileSync(filePath, 'utf8'); |
| 52 | + |
| 53 | + if (filePath.endsWith('.cjs')) { |
| 54 | + // Contains native ESM check |
| 55 | + const transformed = transformDynamicImport(filePath, code); |
| 56 | + if (transformed) { |
| 57 | + code = ( |
| 58 | + shouldApplySourceMap() |
| 59 | + ? inlineSourceMap(transformed) |
| 60 | + : transformed.code |
| 61 | + ); |
| 62 | + } |
| 63 | + } else if ( |
| 64 | + transformTs |
| 65 | + |
| 66 | + // CommonJS file but uses ESM import/export |
| 67 | + || isESM(code) |
| 68 | + ) { |
| 69 | + const transformed = transformSync( |
| 70 | + code, |
| 71 | + filePath, |
| 72 | + { |
| 73 | + tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'], |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + code = ( |
| 78 | + shouldApplySourceMap() |
| 79 | + ? inlineSourceMap(transformed) |
| 80 | + : transformed.code |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + module._compile(code, filePath); |
| 85 | +}; |
| 86 | + |
| 87 | +[ |
| 88 | + /** |
| 89 | + * Handles .cjs, .cts, .mts & any explicitly specified extension that doesn't match any loaders |
| 90 | + * |
| 91 | + * Any file requested with an explicit extension will be loaded using the .js loader: |
| 92 | + * https://github.com/nodejs/node/blob/e339e9c5d71b72fd09e6abd38b10678e0c592ae7/lib/internal/modules/cjs/loader.js#L430 |
| 93 | + */ |
| 94 | + '.js', |
| 95 | + |
| 96 | + /** |
| 97 | + * Loaders for implicitly resolvable extensions |
| 98 | + * https://github.com/nodejs/node/blob/v12.16.0/lib/internal/modules/cjs/loader.js#L1166 |
| 99 | + */ |
| 100 | + '.ts', |
| 101 | + '.tsx', |
| 102 | + '.jsx', |
| 103 | +].forEach((extension) => { |
| 104 | + extensions[extension] = transformer; |
| 105 | +}); |
| 106 | + |
| 107 | +/** |
| 108 | + * Loaders for explicitly resolvable extensions |
| 109 | + * (basically just .mjs because CJS loader has a special handler for it) |
| 110 | + * |
| 111 | + * Loaders for extensions .cjs, .cts, & .mts don't need to be |
| 112 | + * registered because they're explicitly specified and unknown |
| 113 | + * extensions (incl .cjs) fallsback to using the '.js' loader: |
| 114 | + * https://github.com/nodejs/node/blob/v18.4.0/lib/internal/modules/cjs/loader.js#L430 |
| 115 | + * |
| 116 | + * That said, it's actually ".js" and ".mjs" that get special treatment |
| 117 | + * rather than ".cjs" (it might as well be ".random-ext") |
| 118 | + */ |
| 119 | +Object.defineProperty(extensions, '.mjs', { |
| 120 | + value: transformer, |
| 121 | + |
| 122 | + // Prevent Object.keys from detecting these extensions |
| 123 | + // when CJS loader iterates over the possible extensions |
| 124 | + enumerable: false, |
| 125 | +}); |
0 commit comments