Skip to content

Commit

Permalink
fix: global env problem with node properties morph
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-ka committed Dec 10, 2017
1 parent cf6902a commit 355c63b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/visitors/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
let key = types.toComputedKey(node);
if (types.isStringLiteral(key)) {
let val = types.valueToNode(process.env[key.value]);
replace(ancestors[ancestors.length - 2], node, val);
morph(node, val);
delete node.object; // prevent traversing into this node
asset.isAstDirty = true;
}
Expand Down Expand Up @@ -105,12 +105,12 @@ function matchesPattern(member, match) {
return true;
}

// Replaces the key in `parent` whose value is `from` with `to`
function replace(parent, from, to) {
for (let key in parent) {
if (parent[key] === from) {
parent[key] = to;
break;
}
// replace object properties
function morph(object, newProperties) {
for (let key in object) {
delete object[key];
}
for (let key in newProperties) {
object[key] = newProperties[key];
}
}
4 changes: 1 addition & 3 deletions test/integration/env/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
module.exports = function () {
return process.env.NODE_ENV;
};
module.exports = (x => x)(process.env.NODE_ENV);
12 changes: 7 additions & 5 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ describe('javascript', function() {
assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'bundle-loader.js', 'bundle-url.js'],
childBundles: [{
assets: ['local.js'],
childBundles: []
}]
childBundles: [
{
assets: ['local.js'],
childBundles: []
}
]
});

let output = run(b).default;
Expand Down Expand Up @@ -167,7 +169,7 @@ describe('javascript', function() {
let b = await bundle(__dirname + '/integration/env/index.js');

let output = run(b);
assert.equal(output(), 'test');
assert.equal(output, 'test');
});

it('should support adding implicit dependencies', async function() {
Expand Down

0 comments on commit 355c63b

Please sign in to comment.