Skip to content

Commit

Permalink
fix(cjs): only hide transformers when namespaced
Browse files Browse the repository at this point in the history
fixes #584
  • Loading branch information
privatenumber authored Jun 8, 2024
1 parent 4503421 commit 9e647a5
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/cjs/api/module-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,41 @@ export const createExtensions = (
'.ts',
'.tsx',
'.jsx',

/**
* Loaders for extensions .cjs, .cts, & .mts don't need to be
* registered because they're explicitly specified. And unknown
* extensions (incl .cjs) fallsback to using the '.js' loader:
* https://github.com/nodejs/node/blob/v18.4.0/lib/internal/modules/cjs/loader.js#L430
*
* That said, it's actually ".js" and ".mjs" that get special treatment
* rather than ".cjs" (it might as well be ".random-ext")
*/
'.mjs',
].forEach((extension) => {
const descriptor = Object.getOwnPropertyDescriptor(extensions, extension);
Object.defineProperty(extensions, extension, {
value: transformer,

/**
* Prevent Object.keys from detecting these extensions
* when CJS loader iterates over the possible extensions
* https://github.com/nodejs/node/blob/v22.2.0/lib/internal/modules/cjs/loader.js#L609
* Registeration needs to be enumerable for some 3rd party libraries
* https://github.com/gulpjs/rechoir/blob/v0.8.0/index.js#L21 (used by Webpack CLI)
*
* If the extension already exists, inherit its enumerable property
* If not, only expose if it's not namespaced
*/
enumerable: false,
enumerable: descriptor?.enumerable || !namespace,
});
});

/**
* Loaders for extensions .cjs, .cts, & .mts don't need to be
* registered because they're explicitly specified. And unknown
* extensions (incl .cjs) fallsback to using the '.js' loader:
* https://github.com/nodejs/node/blob/v18.4.0/lib/internal/modules/cjs/loader.js#L430
*
* That said, it's actually ".js" and ".mjs" that get special treatment
* rather than ".cjs" (it might as well be ".random-ext")
*/
Object.defineProperty(extensions, '.mjs', {
value: transformer,

/**
* Prevent Object.keys from detecting these extensions
* when CJS loader iterates over the possible extensions
* https://github.com/nodejs/node/blob/v22.2.0/lib/internal/modules/cjs/loader.js#L609
*/
enumerable: false,
});

return extensions;
};

0 comments on commit 9e647a5

Please sign in to comment.