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): do not create fake named exports #427

Merged
merged 2 commits into from
May 29, 2020
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
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.