TrySound / rollup-plugin-terser

Rollup plugin to minify generated bundle
MIT License
609 stars 55 forks source link

Can't figure out how to use mangle.reserved #43

Closed mblataric closed 5 years ago

mblataric commented 5 years ago

Hi,

I am trying out terser plugin, but I need one variable name not to be changed.

First I thought rollup-plugin-typescript2 might be the issue (maybe incorrectly transpile ts file), but I removed it from process (I prepared JS files manually).

In entry JS file I have following code:

let svg = new LogonSvg(c);
svg.InitSvg();

Then my rollup.config.js looks like this:

// import typescript from "rollup-plugin-typescript2"
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import { terser } from "rollup-plugin-terser";
import pkg from "./package.json"

export default {
    input: "./wwwroot/js/svgts.js",
    output: [
        {
            file: pkg.module,
            format: "esm",
        },
    ],
    external: [
        ...Object.keys(pkg.dependencies || {}),
        ...Object.keys(pkg.peerDependencies || {}),
    ], plugins: [
        resolve({
            // resolve: require("rollup-plugin-node-resolve"),
            // the fields to scan in a package.json to determine the entry point
            // if this list contains "browser", overrides specified in "pkg.browser"
            // will be used
            mainFields: ["module", "main"], // Default: ["module", "main"]
        }),

        commonjs({
            include: "node_modules/**"
        }),

        // typescript({
        //     typescript: require("typescript"),
        //     tsconfig: "./tsconfig.json"
        // }),

        (process.env.NODE_ENV === "production" && terser({
            mangle: { reserved: ['svg'] }
        }))
    ],
}

As you can see, I would like svg variable to remain unchanged, but after terser is finished, there is no svg variable anywhere, instead class is initialised like this:

new class {
    constructor(t) {
           ......
        )
    }
    InitSvg() { ...

and then at the end:

(f).InitSvg();

If I do not use terser plugin, rollup leaves the svg variable and code is like in typescript.

Any idea how to keep original svg declaration? Thanks, Mario

TrySound commented 5 years ago

I don't have an idea. This is related to inlining. Mange options is can only affect renaming. This plugin is a thing wrapper around terser. It's not responsible to any optimisation in a code.

TrySound commented 5 years ago

By the way what are you trying to achieve? If you want svg to be a global you can use window.svg = instead.

mblataric commented 5 years ago

I don't have an idea. This is related to inlining. Mange options is can only affect renaming. This plugin is a thing wrapper around terser. It's not responsible to any optimisation in a code.

I understand, I thought maybe not all options are properly passed to terser.

By the way what are you trying to achieve? If you want svg to be a global you can use window.svg = instead.

I want to be able to access this variable in html, for instance onclick="svg.SomeFunction(). I found working solution with:

            compress: {
                collapse_vars: false
            },

but I guess window.svg could be even better since I get collapsed vars as well, thanks.