Skip to content

Commit 9e647a5

Browse files
authoredJun 8, 2024··
fix(cjs): only hide transformers when namespaced
fixes #584
1 parent 4503421 commit 9e647a5

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed
 

‎src/cjs/api/module-extensions.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,41 @@ export const createExtensions = (
110110
'.ts',
111111
'.tsx',
112112
'.jsx',
113-
114-
/**
115-
* Loaders for extensions .cjs, .cts, & .mts don't need to be
116-
* registered because they're explicitly specified. And unknown
117-
* extensions (incl .cjs) fallsback to using the '.js' loader:
118-
* https://github.com/nodejs/node/blob/v18.4.0/lib/internal/modules/cjs/loader.js#L430
119-
*
120-
* That said, it's actually ".js" and ".mjs" that get special treatment
121-
* rather than ".cjs" (it might as well be ".random-ext")
122-
*/
123-
'.mjs',
124113
].forEach((extension) => {
114+
const descriptor = Object.getOwnPropertyDescriptor(extensions, extension);
125115
Object.defineProperty(extensions, extension, {
126116
value: transformer,
127117

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

129+
/**
130+
* Loaders for extensions .cjs, .cts, & .mts don't need to be
131+
* registered because they're explicitly specified. And unknown
132+
* extensions (incl .cjs) fallsback to using the '.js' loader:
133+
* https://github.com/nodejs/node/blob/v18.4.0/lib/internal/modules/cjs/loader.js#L430
134+
*
135+
* That said, it's actually ".js" and ".mjs" that get special treatment
136+
* rather than ".cjs" (it might as well be ".random-ext")
137+
*/
138+
Object.defineProperty(extensions, '.mjs', {
139+
value: transformer,
140+
141+
/**
142+
* Prevent Object.keys from detecting these extensions
143+
* when CJS loader iterates over the possible extensions
144+
* https://github.com/nodejs/node/blob/v22.2.0/lib/internal/modules/cjs/loader.js#L609
145+
*/
146+
enumerable: false,
147+
});
148+
137149
return extensions;
138150
};

0 commit comments

Comments
 (0)
Please sign in to comment.