Skip to content

Commit

Permalink
fix(commonjs)!: do not create fake named exports (rollup#427)
Browse files Browse the repository at this point in the history
BREAKING CHANGE

* fix(commonjs): do not create fake named exports

* chore(commonjs): remove unneeded check
  • Loading branch information
lukastaegert authored and LarsDenBakker committed Sep 12, 2020
1 parent 94dcdbf commit 03e9776
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 41 deletions.
28 changes: 1 addition & 27 deletions packages/commonjs/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,6 @@ export function transformCommonjs(
!uses.require &&
(ignoreGlobal || !uses.global)
) {
if (Object.keys(namedExports).length) {
throw new Error(
`Custom named exports were specified for ${id} but it does not appear to be a CommonJS module`
);
}
// not a CommonJS module
return null;
}
Expand Down Expand Up @@ -578,22 +573,6 @@ export function transformCommonjs(
namedExportDeclarations.push(exportModuleExports);
}

const name = getName(id);

function addExport(x) {
const deconflicted = deconflict(scope, globals, name);

const declaration =
deconflicted === name
? `export var ${x} = ${moduleName}.${x};`
: `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;

namedExportDeclarations.push({
str: declaration,
name: x
});
}

const defaultExportPropertyAssignments = [];
let hasDefaultExport = false;

Expand Down Expand Up @@ -648,7 +627,6 @@ export function transformCommonjs(
str: declaration,
name
});
delete namedExports[name];
}

defaultExportPropertyAssignments.push(`${moduleName}.${name} = ${deconflicted};`);
Expand All @@ -663,12 +641,8 @@ export function transformCommonjs(
}
}

Object.keys(namedExports)
.filter((key) => !blacklist[key])
.forEach(addExport);

const defaultExport = /__esModule/.test(code)
? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});`
? `export default /*@__PURE__*/${HELPERS_NAME}.unwrapExports(${moduleName});`
: `export default ${moduleName};`;

const named = namedExportDeclarations
Expand Down
20 changes: 6 additions & 14 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ Generated by [AVA](https://ava.li).
exports.answer = 42;␊
});␊
␊
var answer$1 = unwrapExports(answer);␊
var answer_1 = answer.answer;␊
var answer$1 = /*@__PURE__*/unwrapExports(answer);␊
␊
var x = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), answer$1, {␊
'default': answer$1,␊
__moduleExports: answer,␊
answer: answer_1␊
__moduleExports: answer␊
}));␊
␊
t.truthy('answer' in x);␊
Expand Down Expand Up @@ -98,7 +96,6 @@ Generated by [AVA](https://ava.li).
module.exports = { fake: true };␊
}␊
});␊
var document_2 = document_1.fake;␊
␊
t.deepEqual(document_1, { real: true });␊
`,
Expand Down Expand Up @@ -3265,13 +3262,12 @@ Generated by [AVA](https://ava.li).
// to ensure module is wrapped␊
commonjsGlobal.foo = exports;␊
});␊
var encode_1 = encode.encodeURIComponent;␊
␊
/* eslint-disable */␊
␊
const foo = {␊
str: 'test string',␊
encodeURIComponent: encode_1␊
encodeURIComponent: encode.encodeURIComponent␊
};␊
␊
var encoded = foo.encodeURIComponent();␊
Expand Down Expand Up @@ -3399,9 +3395,8 @@ Generated by [AVA](https://ava.li).
module.exports = { named: 'foo' };␊
}␊
});␊
var x_1 = x.named;␊
␊
t.is(x_1, 'foo');␊
t.is(x.named, 'foo');␊
`,
}

Expand Down Expand Up @@ -3584,8 +3579,6 @@ Generated by [AVA](https://ava.li).
exports.default = Bar;␊
});␊
␊
unwrapExports(commonjsBar);␊
␊
var commonjsFoo = createCommonjsModule(function (module, exports) {␊
/* eslint-disable no-underscore-dangle */␊
␊
Expand All @@ -3594,10 +3587,9 @@ Generated by [AVA](https://ava.li).
exports.Bar = commonjsBar.default;␊
});␊
␊
unwrapExports(commonjsFoo);␊
var commonjsFoo_1 = commonjsFoo.Bar;␊
var commonjsFoo$1 = /*@__PURE__*/unwrapExports(commonjsFoo);␊
␊
t.is(new commonjsFoo_1().x, 42);␊
t.is(new commonjsFoo$1.Bar().x, 42);␊
`,
}

Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 03e9776

Please sign in to comment.