Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[babel-preset-babili 0.0.9] Confusion/bug eliminating unreachable code that uses require #361

Closed
avaragado opened this issue Jan 4, 2017 · 1 comment

Comments

@avaragado
Copy link

avaragado commented Jan 4, 2017

I'm building an app using electron 1.4.13. I'm using babel-preset-env to target babel to the current node release (7.3.0) for transpilation of electron's main process code. (Main process code doesn't need to be bundled into a single file and can use require.)

In the main process code, I want to use NODE_ENV to include/exclude code based on the build target. I see there are two ways to do this: babel-plugin-transform-node-env-inline and babel-plugin-transform-inline-environment-variables.

(I'm not sure why there are two methods. This is a side issue, but a point of confusion.)

I'm seeing a problem using either method: code that should be removed as unreachable is left in the source if it contains require.

The .babelrc in use (here with babel-plugin-transform-node-env-inline):

{
    "presets": [
        [
            "env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "plugins": [
        "transform-node-env-inline"
    ],
    "env": {
        "production": {
            "presets": ["babili"]
        }
    }
}

Build method (ttt is a temp test directory):

NODE_ENV=production babel src/ttt --out-dir out/ttt

Test file src/ttt/ttt.js (the required file exists):

console.log('pre');

if (false) {
    const ccc = require('../../config');
    console.log(`1 ${ccc}`);
}

if ('x' !== 'x') {
    const ccc = require('../../config');
    console.log(`2 ${ccc}`);
}

if (process.env.NODE_ENV === 'x') {
    const ccc = require('../../config');
    console.log(`3 ${ccc}`);
}

if (process.env.NODE_ENV === 'x') {
    console.log('4');
}

console.log('post');

Since every test is false, with either inlining plugin I expect to see "use strict" plus two console.log calls for "pre" and "post".

Actual output (with babel-plugin-transform-node-env-inline), reformatted for readability:

'use strict';
if (console.log('pre'), !1) {
  const a = require('../../config');
  console.log(`3 ${a}`)
}
!1, console.log('post');

With babel-plugin-transform-inline-environment-variables instead:

'use strict';
if (console.log('pre'), 'x' === 'production') {
  const a = require('../../config');
  console.log(`3 ${a}`)
}
!1, console.log('post');

Am I doing anything wrong?

@boopathi
Copy link
Member

This is fixed in #386 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants