Skip to content

Name cache renames an undeclared global in a module #1753

Open

Description

@thomasbachem

Bug report or Feature request?

Bug report

Version (complete output of terser -V or specific git commit)

terser 5.51.2

Complete CLI command or minify() options used

terser a.js --module --mangle --name-cache cache.json
terser b.js --module --mangle --name-cache cache.json

terser input

// a.js
import process from "node:process";
export const pid = process.pid;
// b.js
export const platform = process.platform;

terser output or error

// a.js
import o from"node:process";export const pid=o.pid;
// b.js
export const platform=o.platform;

Running b.js throws ReferenceError: o is not defined.

Expected result

b.js keeps process.platform. It never declares process, so that is the global.

With a name cache, an undeclared global takes whatever name the cache holds for it. For scripts that is the point, since they share one global scope. In a module the toplevel scope is not the global scope, so the name a.js cached for its import can never be what process means in b.js. #165 was the same problem for import and export names, fixed in #166.

Sharing one cache across modules is what --mangle-props needs to keep property names consistent, and the variable names come along. It then takes one module importing a name that another module uses as a global, like process, crypto, or Buffer from the node: modules.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions