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: hasOwnProperty #9362

Merged
merged 5 commits into from
Nov 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 73 additions & 40 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading