Skip to content

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

@ar5had

Description

@ar5had

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions