diff --git a/src/assets/JSAsset.js b/src/assets/JSAsset.js index 054ca7a7ed4..55220ff739a 100644 --- a/src/assets/JSAsset.js +++ b/src/assets/JSAsset.js @@ -97,7 +97,7 @@ class JSAsset extends Asset { await babel(this); // Inline environment variables - if (ENV_RE.test(this.contents)) { + if (this.options.target === 'browser' && ENV_RE.test(this.contents)) { await this.parseIfNeeded(); this.traverseFast(envVisitor); } diff --git a/test/javascript.js b/test/javascript.js index 50af16704a6..6ab9e58791d 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -415,10 +415,33 @@ describe('javascript', function() { assert.deepEqual(output(), false); }); - it('should insert environment variables', async function() { - let b = await bundle(__dirname + '/integration/env/index.js'); + it('should not insert environment variables on --target=node', async function() { + let b = await bundle(__dirname + '/integration/env/index.js', { + target: 'node' + }); + + let output = run(b); + assert.ok(output.toString().indexOf('process.env') > -1); + assert.equal(output(), 'test:test'); + }); + + it('should not insert environment variables on --target=electron', async function() { + let b = await bundle(__dirname + '/integration/env/index.js', { + target: 'electron' + }); + + let output = run(b); + assert.ok(output.toString().indexOf('process.env') > -1); + assert.equal(output(), 'test:test'); + }); + + it('should insert environment variables on --target=browser', async function() { + let b = await bundle(__dirname + '/integration/env/index.js', { + target: 'browser' + }); let output = run(b); + assert.ok(output.toString().indexOf('process.env') === -1); assert.equal(output(), 'test:test'); });