Skip to content

Instantly share code, notes, and snippets.

@jackpopp
Created October 12, 2018 14:05
Show Gist options
  • Save jackpopp/938554ff09811981f11cda9f2e1cbf78 to your computer and use it in GitHub Desktop.
Save jackpopp/938554ff09811981f11cda9f2e1cbf78 to your computer and use it in GitHub Desktop.
Code block 49.js for - test.md
if (node.type === 'IfStatement') {
// This allows us to generate different bundles based on the NODE_ENV value, allowing development and production builds
// needs to be a binary expression
if (node.test.type !== 'BinaryExpression') return;
// check if either left or right are member expressions that look for the node env
// check if either the left or right string literal, if it is lets build and compare the two
const { left, right, operator } = node.test;
if ((left.type === 'MemberExpression' || right.type === 'MemberExpression') && (left.type === 'Literal' || right.type === 'Literal')) {
// the member expression and literal can be on either side of the test so figure that out and assign to variables
const member = right.type === 'MemberExpression' ? right : left;
const literal = right.type === 'Literal' ? right : left;
// figure out if the member expression is actually referencing process.env.NODE_ENV if not return early
if (member.object.type !== 'MemberExpression' || member.object.object === undefined || member.object.object.name !== 'process') return;
if (member.object.property.name !== 'env' && member.property.name !== 'NODE_ENV') return;
// evaluate test here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment