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

Ignore noop dependencies #1166

Merged
merged 5 commits into from May 3, 2018
Merged

Ignore noop dependencies #1166

merged 5 commits into from May 3, 2018

Conversation

fathyb
Copy link
Contributor

@fathyb fathyb commented Apr 10, 2018

Ignore dependencies in a falsy branch, example if(false) or if('').

This is needed when consuming Webpack generated UMD modules. For example in react-data-grid:

if (false) {
    var REACT_ELEMENT_TYPE = (typeof Symbol === 'function' &&
    Symbol.for &&
    Symbol.for('react.element')) ||
    0xeac7;

    var isValidElement = function(object) {
    return typeof object === 'object' &&
        object !== null &&
        object.$$typeof === REACT_ELEMENT_TYPE;
    };

    // By explicitly using `prop-types` you are opting into new development behavior.
    // http://fb.me/prop-types-in-prod
    var throwOnDirectAccess = true;
    module.exports = require('./factoryWithTypeCheckers')(isValidElement, throwOnDirectAccess);
}

Triggers this error :

C:\~\node_modules\react-data-grid\dist\react-data-grid.js:93:28: Cannot resolve dependency './factoryWithTypeCheckers'
  91 | a  // http://fb.me/prop-types-in-prod\react-data-
  92 |    var throwOnDirectAccess = true;
> 93 |    module.exports = require('./factoryWithTypeCheckers')(isValidElement, throwOnDirectAccess);
     |                             ^
  94 |  } else {
  95 |    // By explicitly using `prop-types` you are opting into new production behavior.
  96 |    // http://fb.me/prop-types-in-prod

Reported by Jindřich Pergler

@devongovett
Copy link
Member

What about things that evaluate to false but aren't exactly false. e.g. if (process.env.NODE_ENV === "production")? The environment variable should already be replaced, so it would be if ("development" === "production") for example.

@mischnic
Copy link
Member

mischnic commented Apr 11, 2018

Isn't that equivalent to this code (test/integration/optional-dep/index.js)?

if('') {
  require('if-falsy-optional-dep');
}

@fathyb
Copy link
Contributor Author

fathyb commented Apr 11, 2018

@devongovett sounds good, would this only apply to process.env.NODE_ENV or other globals? I feel like __dirname, __filename and process.env is enough but your call 🙂

@mischnic I'm not sure to understand

@mischnic
Copy link
Member

mischnic commented Apr 11, 2018

I was referring to @devongovett 's comment. "development" === "production" should evaluate to false just as '' does.

The replacement is already done here:

MemberExpression(node, asset) {
// Inline environment variables accessed on process.env
if (matchesPattern(node.object, 'process.env')) {
let key = types.toComputedKey(node);
if (types.isStringLiteral(key)) {
let val = types.valueToNode(process.env[key.value]);
morph(node, val);
asset.isAstDirty = true;
asset.cacheData.env[key.value] = process.env[key.value];
}
}

@fathyb
Copy link
Contributor Author

fathyb commented Apr 11, 2018

@mischnic Oh, gotcha. The replacement is done before src/visitors/globals is executed (collectDependencies is called before transform) so it does not evaluate it currently :

collectDependencies() {
walk.ancestor(this.ast, collectDependencies, this);
}
async pretransform() {
await babel(this);
}
async transform() {
if (this.options.target === 'browser') {
if (this.dependencies.has('fs') && FS_RE.test(this.contents)) {
await this.parseIfNeeded();
this.traverse(fsVisitor);
}
if (GLOBAL_RE.test(this.contents)) {
await this.parseIfNeeded();
walk.ancestor(this.ast, insertGlobals, this);
}
}

@jpergler
Copy link

jpergler commented May 1, 2018

Hi @fathyb, will there be any progress with this issue? Thank you.

@devongovett
Copy link
Member

Updated to move environment variable inlining to a separate stage prior to dependency collection, and now evaluate the if statement test to determine whether it is true or false rather than only supporting literals.

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

Successfully merging this pull request may close these issues.

None yet

4 participants