Skip to content

minify() with enclose option appears to break source map #1333

Open

Description

@dqh-au

version 5.16.1

If I pass the enclose: true option to minify() (along with sourceMap: true), the generated source map file will be empty:

{"version":3,"file":"<source file name>","sources":[],"sourcesContent":[],"names":[],"mappings":""}

Activity

  1. fabiosantoscode commented on Jan 19, 2023

    @fabiosantoscode
    Collaborator

    I can't reproduce with this call:

    console.log(await minify('let x = 1234; alert(x); alert(x); alert(x)', {
      enclose: true,
      sourceMap: true,
    }))
    {
      "code": "!function(){let t=1234;alert(t),alert(t),alert(t)}();",
      "map": "{\"version\":3,\"names\":[\"x\",\"alert\"],\"sources\":[null,\"0\"],\"mappings\":\"YCAA,IAAIA,EAAI,KAAMC,MAAMD,GAAIC,MAAMD,GAAIC,MAAMD,E\"}",
      "decoded_map": { ... }
    }
  2. dqh-au commented on Jan 20, 2023

    @dqh-au
    Author

    This gets weirder and weirder. I think this might be an incompatibility between Terser's decoded_map and Rollup. minify() does indeed produce a source map for my test case, but Rollup does not seem to like it.

    Interestingly, your code snippet triggers the same problem when I place it in my source file.

    One thing I noticed is that our test cases share a map sources of [null, '0']. I haven't pinpointed the code yet, but I think the null source is resulting in an off by one error inside Rollup. Changing the source code input can result in a sources of ['0'], and when this happens Rollup emits a source map with content.

    Is a null source valid? I am new to this, but It does seem odd that a single source is producing two sources in the source map. I'm not sure what the right spec is, but https://sourcemaps.info/spec.html doesn't say anything about a null source.

    Another thing I noticed while testing is that the enclose option doesn't always result in wrapping the output in an life. For example:

    let result =
        await minify(
            'const log = console.log.bind(console); log("")',
            {
                enclose: true,
                sourceMap: true,
            });
    console.log(result.code);
    console.log('');
    console.log(result.map);
    

    outputs:

    console.log.bind(console)("");
    
    {"version":3,"names":["console","log","bind"],"sources":["0"],"mappings":"AAAYA,QAAQC,IAAIC,KAAKF,QAAUC,CAAI"}
    

    Whereas if I change the source to 'const log = console.log.bind(console); const warn = console.warn.bind(console); log("")', the output is:

    !function(){const o=console.log.bind(console);console.warn.bind(console);o("")}();
    
    {"version":3,"names":["log","console","bind","warn"],"sources":[null,"0"],"mappings":"YCAA,MAAMA,EAAMC,QAAQD,IAAIE,KAAKD,SAAuBA,QAAQE,KAAKD,KAAKD,SAAUD,EAAI,G"}
    
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