Skip to content

Commit

Permalink
fix: produce code which works when __esModule is already defined
Browse files Browse the repository at this point in the history
  • Loading branch information
bhovhannes committed Dec 16, 2022
1 parent 75e9ff4 commit c64666b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/commonjs/src/helpers.js
Expand Up @@ -35,6 +35,7 @@ export function getDefaultExportFromNamespaceIfNotNamed (n) {
}
export function getAugmentedNamespace(n) {
if (n.__esModule) return n;
var f = n.default;
if (typeof f == "function") {
var a = function a () {
Expand Down
13 changes: 13 additions & 0 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -3491,6 +3491,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3577,6 +3578,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3657,6 +3659,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3750,6 +3753,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3957,6 +3961,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4187,6 +4192,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4289,6 +4295,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4482,6 +4489,7 @@ Generated by [AVA](https://avajs.dev).
}␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4760,6 +4768,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4854,6 +4863,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -6163,6 +6173,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -6214,6 +6225,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -7167,6 +7179,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/commonjs/test/snapshots/test.js.md
Expand Up @@ -42,6 +42,7 @@ Generated by [AVA](https://avajs.dev).
}␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -199,6 +200,7 @@ Generated by [AVA](https://avajs.dev).
`'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -279,6 +281,7 @@ Generated by [AVA](https://avajs.dev).
`'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/test.js.snap
Binary file not shown.
28 changes: 28 additions & 0 deletions packages/commonjs/test/test.js
Expand Up @@ -2,6 +2,7 @@

const os = require('os');
const path = require('path');
const vm = require('vm');

const { nodeResolve } = require('@rollup/plugin-node-resolve');
const test = require('ava');
Expand Down Expand Up @@ -598,6 +599,33 @@ test('produces optimized code when importing esm without a default export', asyn
t.snapshot(await getCodeFromBundle(bundle));
});

test('produces working code when importing esm with __esModule property set', async (t) => {
const bundle = await rollup({
input: 'main.js',
plugins: [
commonjs(),
loader({
'main.js': 'const {y} = require("withEsModule.js"); module.exports = y + 1;',
'withEsModule.js': 'export const y = 1; export const __esModule=true;'
})
]
});
const name = 'bundleExecutionResult';
const code = await getCodeFromBundle(bundle, {
format: 'iife',
name
});

const context = {};
vm.createContext(context);
vm.runInContext(code, context);
t.is(
context[name],
2,
`Expected to get 2, but got ${context[name]} after running bundled code: \n${code}`
);
});

test('handles array destructuring assignment', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/array-destructuring-assignment/main.js',
Expand Down

0 comments on commit c64666b

Please sign in to comment.