diff --git a/packages/core/integration-tests/test/integration/formats/commonjs-external/default-new.js b/packages/core/integration-tests/test/integration/formats/commonjs-external/default-new.js new file mode 100644 index 00000000000..8b4d503bd49 --- /dev/null +++ b/packages/core/integration-tests/test/integration/formats/commonjs-external/default-new.js @@ -0,0 +1,3 @@ +import Foo from "test"; + +new Foo(); diff --git a/packages/core/integration-tests/test/integration/formats/commonjs-external/node_modules/test/index.js b/packages/core/integration-tests/test/integration/formats/commonjs-external/node_modules/test/index.js new file mode 100644 index 00000000000..5f3ae2e97c8 --- /dev/null +++ b/packages/core/integration-tests/test/integration/formats/commonjs-external/node_modules/test/index.js @@ -0,0 +1,7 @@ +function Foo(v) { + if (!(this instanceof Foo)) { + throw new Error("Called without new"); + } +} + +module.exports = Foo; diff --git a/packages/core/integration-tests/test/integration/formats/commonjs-external/node_modules/test/package.json b/packages/core/integration-tests/test/integration/formats/commonjs-external/node_modules/test/package.json new file mode 100644 index 00000000000..1545befa161 --- /dev/null +++ b/packages/core/integration-tests/test/integration/formats/commonjs-external/node_modules/test/package.json @@ -0,0 +1,4 @@ +{ + "name": "test", + "version": "0.0.0" +} diff --git a/packages/core/integration-tests/test/integration/formats/commonjs-external/package.json b/packages/core/integration-tests/test/integration/formats/commonjs-external/package.json index 0bd14c76a75..e734a965125 100644 --- a/packages/core/integration-tests/test/integration/formats/commonjs-external/package.json +++ b/packages/core/integration-tests/test/integration/formats/commonjs-external/package.json @@ -6,6 +6,7 @@ "node": ">= 10.x" }, "dependencies": { - "lodash": "*" + "lodash": "*", + "test": "*" } } diff --git a/packages/core/integration-tests/test/output-formats.js b/packages/core/integration-tests/test/output-formats.js index ad4c5d8cf51..ec37e93c7d0 100644 --- a/packages/core/integration-tests/test/output-formats.js +++ b/packages/core/integration-tests/test/output-formats.js @@ -168,6 +168,19 @@ describe('output formats', function() { assert.equal((await run(b)).bar, 3); }); + it('should support commonjs output with external modules (default import new call)', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/formats/commonjs-external/default-new.js', + ), + ); + + let dist = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8'); + assert(dist.includes('$parcel$interopDefault')); + await run(b); + }); + it('should support commonjs output with external modules (multiple specifiers)', async function() { let b = await bundle( path.join( @@ -178,7 +191,7 @@ describe('output formats', function() { let dist = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8'); assert(dist.includes('= require("lodash")')); - assert(dist.includes('= $parcel$interopDefault(')); + assert(dist.includes('= ($parcel$interopDefault(')); assert(/var {add: \s*\$.+?\$add\s*} = lodash/); assert.equal((await run(b)).bar, 6); }); diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index 1e6d7e29a7c..e7ca74cebf6 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -580,7 +580,7 @@ ${code} if (imported === '*') { replacement = renamed; } else if (imported === 'default') { - replacement = `$parcel$interopDefault(${renamed})`; + replacement = `($parcel$interopDefault(${renamed}))`; this.usedHelpers.add('$parcel$interopDefault'); } else { replacement = this.getPropertyAccess(renamed, imported);