Skip to content

Commit

Permalink
Only add export setter for non-esm exports (#8910)
Browse files Browse the repository at this point in the history
* Only add export setter for non-esm exports

* Fromatting

* Add integration test
  • Loading branch information
mattcompiles authored Mar 27, 2023
1 parent f65889e commit ce4a38b
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 35 deletions.
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
Loading

0 comments on commit ce4a38b

Please sign in to comment.