Skip to content

Commit

Permalink
Requeue inserted globals when hoisting (#3082)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and devongovett committed May 24, 2019
1 parent 6496ceb commit 47d91b6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const test = require('./b');

output = !test("abc");
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;(function () {
module.exports = test

function test(data) {
if (typeof Buffer === 'function' && typeof Buffer.isBuffer === 'function' && Buffer.isBuffer(data)) {
return true;
}
return false;
}
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"browserslist": ["last 1 Chrome versions"]
}
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,5 +1263,17 @@ describe('scope hoisting', function() {
let output = await run(b);
assert.deepEqual(output, 2);
});

it('should also hoist inserted polyfills of globals', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/globals-polyfills/a.js'
)
);

let output = await run(b);
assert.equal(output, true);
});
});
});
6 changes: 4 additions & 2 deletions packages/shared/scope-hoisting/src/hoist.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ const VISITOR = {

let globalCode = globals.get(path.node.name);
if (globalCode) {
path.scope
let decl = path.scope
.getProgramParent()
.path.unshiftContainer('body', [template(globalCode.code)()]);
.path.unshiftContainer('body', [template(globalCode.code)()])[0];

path.requeue(decl);

globals.delete(path.node.name);
}
Expand Down

0 comments on commit 47d91b6

Please sign in to comment.