Skip to content

properties are not mangled if it's in a deferred top level object #397

@gdh1995

Description

@gdh1995

Bug report or Feature request? Bug report

Version (complete output of terser -V or specific git commit) from v3.14.1 to the latest v4.1.2

code to run

#!/usr/bin/env node
"use strict";
var opts = {
  "mangle": {
    "properties": true,
    "toplevel": true
  },
  nameCache: { vars: {}, props: {} }
};

var terser = require('terser');
terser.minify('var Bar = {};', opts);
var result = terser.minify('var Foo = { foo: function() { return Bar.bar() } };', opts);
var expected = "var r={o:function(){return a.v()}};";
console.log("[task] test uglifying external properties",
    "\n[Current ]", result.code, "\n[Expected]", expected);

expected result

var r={o:function(){return a.v()}};

real result

var r={o:function(){return a.bar()}};

why this is a bug

In a real case, if a Bar is defined in some later files, I want to pre-define Bar to make terser uglifiy this name, and I also want to uglify all its properties. But I don't like to pass all input files to terser during one calling to .minify

  • I'm using Gulp, and the file which defines Bar is in another task
  • currently, I add an extra step to call terser.minify("var Bar = {};") by myself, and abort its output
    • so that the gulp task's output keeps smallest.

This usage works well under terser v3.10.*, and something becames wrong since terser v3.14.1 .

I've read #219 , but I think my usage is still worth a new if-branch.

debugging

I find a commit in v3.14.1 seems to blame:

https://github.com/terser-js/terser/blob/b4dda6d2ccdc94ac42f6dbf459af50ac343f9454/lib/propmangle.js#L168-L173

When I reverted it to a simple add(node.property);, this bug disappeared.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions