question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

define doesn't seem to trigger code elimination?

See original GitHub issue

Using this command npx tsup ./x.ts --define.process.platform \"linux\" --minify and the following file.

const _platform = process.platform;
const _linux = (_platform === 'linux');
const _darwin = (_platform === 'darwin');
const _windows = (_platform === 'win32');
const _freebsd = (_platform === 'freebsd');
const _openbsd = (_platform === 'openbsd');
const _netbsd = (_platform === 'netbsd');
const _sunos = (_platform === 'sunos');

if (_platform === 'linux') console.log('linux');

// The following should be removed?
if (_windows) console.log('windows');
if (_freebsd || _darwin) console.log('freebsd or mac?');

We end up with this.

var o="linux";var n=o==="darwin",s=o==="win32",e=o==="freebsd";o==="linux"&&console.log("linux");s&&console.log("windows");(e||n)&&console.log("freebsd or mac?");

I was expecting the following since all of the vars could be resolved to true/false and then can be removed resulting in only the linux code.

console.log("linux");

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
egoistcommented, Oct 20, 2021

You should inline the expression:

if (process.platform === 'windows') {
  console.log('windows')
}
1reaction
egoistcommented, Oct 20, 2021

Yeah, I guess it’s more complex than it seems for minifiers to implement optimizations like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dead-code elimination - Wikipedia
In compiler theory, dead-code elimination is a compiler optimization to remove code which does not affect the program results. Removing such code has ......
Read more >
Why can't dead code detection be fully solved by a compiler?
"Unconditional dead code can be detected and removed by advanced compilers." This does not seem likely. Code deadness can depend on the outcome ......
Read more >
Debugging - Definition - The Economic Times
Definition: Debugging is the process of detecting and removing of existing and potential errors (also called as 'bugs') in a software code that...
Read more >
25.3.1 Trigger Syntax and Examples - MySQL :: Developer Zone
Trigger names exist in the schema namespace, meaning that all triggers must have unique names within a schema. Triggers in different schemas can...
Read more >
Error codes in Device Manager in Windows - Microsoft Support
From Start, search for device manager and select Device Manager from the results. Right-click the device in the list. Select Uninstall from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found