Skip to content

Commit

Permalink
Handle re-declaration of the global variable in a module
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed May 1, 2018
1 parent 607a4d4 commit 84be07b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,11 @@ class JSPackager extends Packager {
}

async writeModule(id, code, deps = {}, map) {
let wrapped =
(this.first ? '' : ',') +
id +
':[function(require,module,exports,global) {\n' +
(code || '') +
'\n},' +
JSON.stringify(deps) +
']';
let wrapped = this.first ? '' : ',';
wrapped +=
id + ':[function(require,module,exports) {\n' + (code || '') + '\n},';
wrapped += JSON.stringify(deps);
wrapped += ']';

this.first = false;
await this.write(wrapped);
Expand Down
1 change: 1 addition & 0 deletions src/visitors/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const VARS = {
asset.addDependency('process');
return 'var process = require("process");';
},
global: () => 'var global = arguments[3];',
__dirname: asset =>
`var __dirname = ${JSON.stringify(Path.dirname(asset.name))};`,
__filename: asset => `var __filename = ${JSON.stringify(asset.name)};`,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/global-redeclare/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const global = {};
module.exports = function () {
return !!global.document;
};
5 changes: 5 additions & 0 deletions test/integration/global-redeclare/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "parcel-test-global-redeclare",
"private": true,
"browserslist": ["last 2 Chrome versions"]
}
7 changes: 7 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ describe('javascript', function() {
});
});

it('should handle re-declaration of the global constant', async function() {
let b = await bundle(__dirname + '/integration/global-redeclare/index.js');

let output = run(b);
assert.deepEqual(output(), false);
});

it('should insert environment variables', async function() {
let b = await bundle(__dirname + '/integration/env/index.js');

Expand Down

0 comments on commit 84be07b

Please sign in to comment.