Skip to content

Commit e14784d

Browse files
authored
Support default interop for CommonJS macros (#9525)
1 parent a93af6a commit e14784d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

packages/core/integration-tests/test/macros.js

+22
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ describe('macros', function () {
8383
assert(res.includes('output="2a2300bbd7ea6e9a"'));
8484
});
8585

86+
it('should support default interop with CommonJS modules', async function () {
87+
await fsFixture(overlayFS, dir)`
88+
index.js:
89+
import test from "./macro.js" with { type: "macro" };
90+
output = test('hi');
91+
92+
macro.js:
93+
import {hashString} from '@parcel/rust';
94+
module.exports = function(s) {
95+
return hashString(s);
96+
}
97+
`;
98+
99+
let b = await bundle(path.join(dir, '/index.js'), {
100+
inputFS: overlayFS,
101+
mode: 'production',
102+
});
103+
104+
let res = await overlayFS.readFile(b.getBundles()[0].filePath, 'utf8');
105+
assert(res.includes('output="2a2300bbd7ea6e9a"'));
106+
});
107+
86108
it('should support namespace imports', async function () {
87109
await fsFixture(overlayFS, dir)`
88110
index.js:

packages/transformers/js/src/JSTransformer.js

+11
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ export default (new Transformer({
477477
let mod;
478478
try {
479479
mod = await options.packageManager.require(src, asset.filePath);
480+
481+
// Default interop for CommonJS modules.
482+
if (
483+
exportName === 'default' &&
484+
!mod.__esModule &&
485+
// $FlowFixMe
486+
Object.prototype.toString.call(config) !== '[object Module]'
487+
) {
488+
mod = {default: mod};
489+
}
490+
480491
if (!Object.hasOwnProperty.call(mod, exportName)) {
481492
throw new Error(`"${src}" does not export "${exportName}".`);
482493
}

0 commit comments

Comments
 (0)