'Rollup tree-shakes imported variables, causing important code to be lost
I'm using rollup to bundle the build script for one of my projects. In the builds (their source is ts), I use node's worker_threads module to parallelize some work. I'm using an import of isMainThread (which is a boolean from the worker_threads module) to check whether or not to use the worker logic or the main thread logic. However, when building the build script from its source, rollup is removing the else statement. It seems to be checking the isMainThread variable during its tree-shaking process, and deciding that isMainThread will always be true, and so the else statement isn't needed. How can I change that logic?
Source:
if (isMainThread) {
const { dev, watch } = getOptions(process.argv);
if (watch) watcher(dev);
else singleBuild(dev);
} else {
worker();
}
Output:
if (isMainThread) {
const { dev, watch } = getOptions(process.argv);
if (watch)
watcher();
else
singleBuild();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
