Skip to content

Commit

Permalink
Remove eval usage. Fixes CSP cases. (#1133)
Browse files Browse the repository at this point in the history
  • Loading branch information
CvX authored and devongovett committed May 1, 2018
1 parent 56a481b commit da98958
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/core/parcel/src/builtins/hmr-runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var OVERLAY_ID = '__parcel__error__overlay__';

var global = (1, eval)('this');
var OldModule = module.bundle.Module;

function Module(moduleName) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel/src/builtins/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ parcelRequire = (function (modules, cache, entry, globalName) {

var module = cache[name] = new newRequire.Module(name);

modules[name][0].call(module.exports, localRequire, module, module.exports);
modules[name][0].call(module.exports, localRequire, module, module.exports, this);
}

return cache[name].exports;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel/src/visitors/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const VARS = {
asset.addDependency('process');
return 'var process = require("process");';
},
global: () => 'var global = (1,eval)("this");',
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const global = {};
module.exports = function () {
return !!global.document;
};
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 packages/core/parcel/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 da98958

Please sign in to comment.