Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
fix: Quantum didn't handle replacement of if else conditions
Browse files Browse the repository at this point in the history
Handle the following case:

if (a){} else if ( process.env.NODE_ENV == "production") {}

Quantum didn't replace these conditions and that lead to an error
  • Loading branch information
nchanged committed Oct 3, 2017
1 parent b5230fb commit de1eed3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/quantum/core/AstUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const ES6_TYPES = new Set([

export function matchesIfStatementProcessEnv(node): string {
if (node.type && node.type === "IfStatement") {
// prevent detecting if else statement
if (node.$parent && node.$parent.type === "IfStatement") {
return;
}
if (node.test && node.test.type === "BinaryExpression") {
if (node.test.left) {
const variableName = matchesNodeEnv(node.test.left);
Expand Down
25 changes: 25 additions & 0 deletions src/quantum/tests/ProcessEnvReplacement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,29 @@ export class ProcessEnvReplacement {
});
}

"Should replace in if else statement"() {
return createOptimisedBundleEnv({
stubs: true,
options: {
treeshake: false,
},
project: {
plugins: [EnvPlugin({ foo: "foo" })],
files: {
"index.ts": `
if (a) {
} else if (process.env.NODE_ENV !== 'production') {
}
}
`
},
instructions: "index.ts",
},
}).then((result) => {
const contents = result.contents["index.js"];
should(contents).findString("else if ('production' !== 'production')");
});
}

}

0 comments on commit de1eed3

Please sign in to comment.