Skip to content

Commit

Permalink
Fix nyc require.extensions issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed May 17, 2022
1 parent 26a26bb commit d4df204
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
3 changes: 3 additions & 0 deletions nyc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module.exports = {
all: true,
include: ['tests/node_modules/ts-node/**'],
exclude: ['**/*.d.ts', 'tests/node_modules/ts-node/node_modules/**'],
// Very important that nyc does not add additional `require.extensions` hooks.
// It affects module resolution behavior under test
extension: ['.js'],
excludeNodeModules: false,
excludeAfterRemap: false,
};
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Transpiler, TranspilerFactory } from './transpilers/types';
import {
cachedLookup,
createProjectLocalResolveHelper,
hasOwnProperty,
normalizeSlashes,
once,
parse,
Expand Down Expand Up @@ -1572,15 +1573,16 @@ function registerExtensions(
originalJsHandler: (m: NodeModule, filename: string) => any
) {
const exts = new Set(extensions);
// Only way to transform .mts and .cts is via the .js extension.
// Can't register those extensions cuz would allow omitting file extension; node requires ext for .cjs and .mjs
if (exts.has('.mts') || exts.has('.cts')) exts.add('.js');
// Filter extensions which should not be added to `require.extensions`
// They may still be handled via the `.js` extension handler.
exts.delete('.mts');
exts.delete('.cts');
exts.delete('.mjs');
exts.delete('.cjs');
// Can't add these extensions cuz would allow omitting file extension; node requires ext for .cjs and .mjs
// Unless they're already registered by something else; then we *must* hook them or else our transformer
// will not be called.
for(const cannotAdd of ['.mts', '.cts', '.mjs', '.cjs']) {
// Other file exts can still be transformed via the .js extension.
if(exts.has(cannotAdd) && !hasOwnProperty(require.extensions, cannotAdd)) {
exts.add('.js');
exts.delete(cannotAdd);
}
}

// TODO do we care about overriding moduleType for mjs? No, I don't think so.
// Could conditionally register `.mjs` extension when moduleType overrides are configured,
Expand Down
4 changes: 0 additions & 4 deletions src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,6 @@ export function getStream(stream: Readable, waitForPattern?: string | RegExp) {

//#region Reset node environment

// Delete any added by nyc that aren't in vanilla nodejs
for (const ext of Object.keys(require.extensions)) {
if (!['.js', '.json', '.node'].includes(ext)) delete require.extensions[ext];
}
const defaultRequireExtensions = captureObjectState(require.extensions);
// Avoid node deprecation warning for accessing _channel
const defaultProcess = captureObjectState(process, ['_channel']);
Expand Down

0 comments on commit d4df204

Please sign in to comment.