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

Unused destructured variables are stripped in production build despite changing terser config parameters #1190

Open
ar5had opened this issue Nov 27, 2024 · 0 comments

Comments

@ar5had
Copy link

ar5had commented Nov 27, 2024

Description

I am using TSDX to build my project. In production, when TSDX builds the package, it strips out all unused variables. However, in some cases, it is intentional to have unused variables. For example:

export const useCustomInput = (
    {
        customProp,
        ...props
    }
) => {
    ...
    useInput(props);
    ...
}

In my TSDX configuration file, I have set the following options:

const commonjs = require('rollup-plugin-commonjs');
const external = require('rollup-plugin-peer-deps-external');
const { terser } = require('rollup-plugin-terser');

module.exports = {
    rollup(config, opts) {
        config.plugins.push(
            external(),
            commonjs(),
            terser({
                compress: {
                    // Prevent unused destructured variables from being removed
                    pure_getters: false,
                    unused: false,
                },
            })
        );
        
        return config;
    }
};

Current Behavior

In the current build, the props passed to useInput includes customProp because it is stripped out as an unused variable, causing the props object to contain it.

export const useCustomInput = (
    {
        ...props
    }
) => {
    ...
    useInput(props);
    ...
}

Expected Behavior

I expect customProp to be still destructured so that the correct props are passed to useInput.

When I manually set the pure_getters option to false or strict in node_modules/tsdx/dist/createRollupConfig.js, the customProp is correctly retained and not stripped out.

Your environment

  System:
    OS: macOS 14.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 5.87 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.volta/tools/image/node/20.11.1/bin/node
    npm: 10.2.4 - ~/.volta/tools/image/node/20.11.1/bin/npm
    Watchman: 2023.11.13.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 91.1.26.74
    Chrome: 131.0.6778.86
    Firefox: 130.0
    Safari: 17.5
  npmPackages:
    typescript: ^4.9.5 => 4.9.5 
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

No branches or pull requests

1 participant