Skip to content

Commit

Permalink
fix: hasOwnProperty (#9362)
Browse files Browse the repository at this point in the history
Co-authored-by: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
  • Loading branch information
gagdiez and mischnic committed Nov 12, 2023
1 parent d58e336 commit 7a68dc9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
113 changes: 73 additions & 40 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -6386,6 +6386,48 @@ describe('javascript', function () {
assert(!contents.includes('\ufffe'));
});

it(`should not wrap assets that are duplicated in different targets`, async function () {
const dir = path.join(__dirname, 'multi-target-duplicates');
overlayFS.mkdirp(dir);

await fsFixture(overlayFS, dir)`
shared/index.js:
export default 2;
packages/a/package.json:
{
"source": "index.js",
"module": "dist/module.js"
}
packages/a/index.js:
import shared from '../../shared';
export default shared + 2;
packages/b/package.json:
{
"source": "index.js",
"module": "dist/module.js"
}
packages/b/index.js:
import shared from '../../shared';
export default shared + 2;
`;

let b = await bundle(path.join(dir, '/packages/*'), {
inputFS: overlayFS,
});

for (let bundle of b.getBundles()) {
let contents = await outputFS.readFile(bundle.filePath, 'utf8');
assert(
!contents.includes('parcelRequire'),
'should not include parcelRequire',
);
}
});

for (let shouldScopeHoist of [false, true]) {
let options = {
defaultTargetOptions: {
Expand Down Expand Up @@ -7532,7 +7574,9 @@ describe('javascript', function () {
assert.equal(res.output, 123);
});

it('duplicate assets should share module scope', async function () {
it(`duplicate assets should share module scope ${
shouldScopeHoist ? 'with' : 'without'
} scope-hoisting`, async function () {
let b = await bundle(
[
path.join(
Expand All @@ -7552,46 +7596,35 @@ describe('javascript', function () {
assert.equal(await result.output, 2);
});

it('should not wrap assets that are duplicated in different targets', async function () {
const dir = path.join(__dirname, 'multi-target-duplicates');
overlayFS.mkdirp(dir);

await fsFixture(overlayFS, dir)`
shared/index.js:
export default 2;
packages/a/package.json:
{
"source": "index.js",
"module": "dist/module.js"
}
packages/a/index.js:
import shared from '../../shared';
export default shared + 2;
packages/b/package.json:
{
"source": "index.js",
"module": "dist/module.js"
}
packages/b/index.js:
import shared from '../../shared';
export default shared + 2;
`;

let b = await bundle(path.join(dir, '/packages/*'), {
inputFS: overlayFS,
});
it(`should work correctly with export called hasOwnProperty ${
shouldScopeHoist ? 'with' : 'without'
} scope-hoisting`, async () => {
await fsFixture(overlayFS, __dirname)`
js-export-all-hasOwnProperty
a.js:
export function hasOwnProperty() {
throw new Error("Shouldn't be called");
}
b.js:
module.exports = { other: 123 };
library.js:
export * from './a';
export * from './b';
index.js:
import * as x from './library';
output = sideEffectNoop(x).other;`;

for (let bundle of b.getBundles()) {
let contents = await outputFS.readFile(bundle.filePath, 'utf8');
assert(
!contents.includes('parcelRequire'),
'should not include parcelRequire',
);
}
let b = await bundle(
path.join(__dirname, 'js-export-all-hasOwnProperty/index.js'),
{
...options,
inputFS: overlayFS,
},
);
let res = await run(b, null, {require: false});
assert.equal(res.output, 123);
});
}
});
2 changes: 1 addition & 1 deletion packages/packagers/js/src/helpers.js
Expand Up @@ -112,7 +112,7 @@ function $parcel$export(e, n, v, s) {
const $parcel$exportWildcard = `
function $parcel$exportWildcard(dest, source) {
Object.keys(source).forEach(function(key) {
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
if (key === 'default' || key === '__esModule' || Object.prototype.hasOwnProperty.call(dest, key)) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/transformers/js/src/esmodule-helpers.js
Expand Up @@ -8,7 +8,11 @@ exports.defineInteropFlag = function (a) {

exports.exportAll = function (source, dest) {
Object.keys(source).forEach(function (key) {
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
if (
key === 'default' ||
key === '__esModule' ||
Object.prototype.hasOwnProperty.call(dest, key)
) {
return;
}

Expand Down

0 comments on commit 7a68dc9

Please sign in to comment.