MCPcopy
hub / github.com/TypeStrong/ts-node / registerExtensions

Function registerExtensions

src/index.ts:1559–1595  ·  view source on GitHub ↗

* Register the extensions to support when importing files.

(
  preferTsExts: boolean | null | undefined,
  extensions: string[],
  service: Service,
  originalJsHandler: (m: NodeModule, filename: string) => any
)

Source from the content-addressed store, hash-verified

1557 * Register the extensions to support when importing files.
1558 */
1559function registerExtensions(
1560 preferTsExts: boolean | null | undefined,
1561 extensions: string[],
1562 service: Service,
1563 originalJsHandler: (m: NodeModule, filename: string) => any
1564) {
1565 const exts = new Set(extensions);
1566 // Can't add these extensions cuz would allow omitting file extension; node requires ext for .cjs and .mjs
1567 // Unless they're already registered by something else (nyc does this):
1568 // then we *must* hook them or else our transformer will not be called.
1569 for (const cannotAdd of ['.mts', '.cts', '.mjs', '.cjs']) {
1570 if (exts.has(cannotAdd) && !hasOwnProperty(require.extensions, cannotAdd)) {
1571 // Unrecognized file exts can be transformed via the `.js` handler.
1572 exts.add('.js');
1573 exts.delete(cannotAdd);
1574 }
1575 }
1576
1577 // Register new extensions.
1578 for (const ext of exts) {
1579 registerExtension(ext, service, originalJsHandler);
1580 }
1581
1582 if (preferTsExts) {
1583 const preferredExtensions = new Set([
1584 ...exts,
1585 ...Object.keys(require.extensions),
1586 ]);
1587
1588 // Re-sort iteration order of Object.keys()
1589 for (const ext of preferredExtensions) {
1590 const old = Object.getOwnPropertyDescriptor(require.extensions, ext);
1591 delete require.extensions[ext];
1592 Object.defineProperty(require.extensions, ext, old!);
1593 }
1594 }
1595}
1596
1597/**
1598 * Register the extension for node.

Callers 1

registerFunction · 0.85

Calls 3

hasOwnPropertyFunction · 0.90
registerExtensionFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…