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

Only add export setter for non-esm exports #8910

Merged
merged 4 commits into from
Mar 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const value = require('./value');
value.cjs = value.cjs + ' mutated';
value.esm = value.esm + ' mutated';


output = [value.cjs, value.esm, value];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const esm = 'ESM';

module.exports.cjs = 'CJS'
15 changes: 15 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4183,6 +4183,21 @@ describe('scope hoisting', function () {
assert.deepEqual(await run(b), 4);
});

it('supports mutations of the cjs exports by the importer from a mixed module', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/mutated-exports-mixed-module/index.js',
),
);

assert.deepEqual(await run(b), [
'CJS mutated',
'ESM',
{cjs: 'CJS mutated', esm: 'ESM'},
]);
});

it.skip('supports require.resolve calls for excluded modules', async function () {
let b = await bundle(
path.join(
Expand Down
8 changes: 5 additions & 3 deletions packages/packagers/js/src/ScopeHoistingPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,9 +1096,11 @@ ${code}
.map(exp => {
let resolved = this.getSymbolResolution(asset, asset, exp);
let get = this.buildFunctionExpression([], resolved);
let set = asset.meta.hasCJSExports
? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`)
: '';
let isEsmExport = !!asset.symbols.get(exp)?.meta?.isEsm;
let set =
!isEsmExport && asset.meta.hasCJSExports
? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`)
: '';
return `$parcel$export($${assetId}$exports, ${JSON.stringify(
exp,
)}, ${get}${set});`;
Expand Down