Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/commonjs/src/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,17 @@ function getDefinePropertyCallName(node, targetName) {
export function isShorthandProperty(parent) {
return parent && parent.type === 'Property' && parent.shorthand;
}

export function hasDefineEsmProperty(node) {
return node.properties.some((property) => {
if (
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.key.name === '__esModule' &&
isTruthy(property.value)
) {
return true;
}
return false;
});
}
13 changes: 13 additions & 0 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import MagicString from 'magic-string';
import {
getKeypath,
isDefineCompiledEsm,
hasDefineEsmProperty,
isFalsy,
isReference,
isShorthandProperty,
Expand Down Expand Up @@ -147,6 +148,18 @@ export default function transformCommonjs(
} else if (!firstTopLevelModuleExportsAssignment) {
firstTopLevelModuleExportsAssignment = node;
}

if (defaultIsModuleExports === false) {
shouldWrap = true;
} else if (defaultIsModuleExports === 'auto') {
if (node.right.type === 'ObjectExpression') {
if (hasDefineEsmProperty(node.right)) {
shouldWrap = true;
}
} else if (defaultIsModuleExports === false) {
shouldWrap = true;
}
}
} else if (exportName === KEY_COMPILED_ESM) {
if (programDepth > 3) {
shouldWrap = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: 'auto'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { __esModule: true, default: { foo: 'bar' }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as commonjsHelpers from "_commonjsHelpers.js";
import { __module as inputModule, exports as input } from "\u0000fixtures/form/defaultIsModuleExports-auto-reassign-exports-__esModule/input.js?commonjs-module"

(function (module) {
module.exports = { __esModule: true, default: { foo: 'bar' }}
}(inputModule));

export default /*@__PURE__*/commonjsHelpers.getDefaultExportFromCjs(input);
export { input as __moduleExports };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: 'auto'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { default: { foo: 'bar' }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as commonjsHelpers from "_commonjsHelpers.js";

var input = { default: { foo: 'bar' }}

export default input;
export { input as __moduleExports };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: false
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { __esModule: true, default: { foo: 'bar' }};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as commonjsHelpers from "_commonjsHelpers.js";
import { __module as inputModule, exports as input } from "\u0000fixtures/form/defaultIsModuleExports-false-reassign-exports-__esModule/input.js?commonjs-module"

(function (module) {
module.exports = { __esModule: true, default: { foo: 'bar' }};
}(inputModule));

export default input.default;
export { input as __moduleExports };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: false
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { default: { foo: 'bar' }};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as commonjsHelpers from "_commonjsHelpers.js";
import { __module as inputModule, exports as input } from "\u0000fixtures/form/defaultIsModuleExports-false-reassign-exports-no-__esModule/input.js?commonjs-module"

(function (module) {
module.exports = { default: { foo: 'bar' }};
}(inputModule));

export default input.default;
export { input as __moduleExports };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { __esModule: true, default: { foo: 'bar' }};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as commonjsHelpers from "_commonjsHelpers.js";

var input = { __esModule: true, default: { foo: 'bar' }};

export default input;
export { input as __moduleExports };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { default: { foo: 'bar' }};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as commonjsHelpers from "_commonjsHelpers.js";

var input = { default: { foo: 'bar' }};

export default input;
export { input as __moduleExports };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"default": () => { console.log('beep') },
__esModule: true
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fn from './lib'

fn()
24 changes: 24 additions & 0 deletions packages/commonjs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@ test('converts a CommonJS module with custom file extension', async (t) => {
t.is((await executeBundle(bundle, t)).exports, 42);
});

test('import CommonJS module with esm property should get default export ', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/cjs-with-esm-property/main.js',
plugins: [
commonjs({
defaultIsModuleExports: 'auto'
})
]
});
const result = await executeBundle(bundle, t);
t.is(result.error, undefined);

const bundle2 = await rollup({
input: 'fixtures/samples/cjs-with-esm-property/main.js',
plugins: [
commonjs({
defaultIsModuleExports: true
})
]
});
const result2 = await executeBundle(bundle2, t);
t.is(result2.error.message, 'lib is not a function');
});

test('identifies named exports from object literals', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/named-exports-from-object-literal/main.js',
Expand Down