Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): convert module.exports with __esModule property(#939) #942

Merged
merged 5 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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;
});
}
10 changes: 10 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,15 @@ export default function transformCommonjs(
} else if (!firstTopLevelModuleExportsAssignment) {
firstTopLevelModuleExportsAssignment = node;
}
if (node.right.type === 'ObjectExpression') {
if (defaultIsModuleExports === 'auto') {
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