Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve top-level when running terser for IIFE #900

Merged
merged 2 commits into from Jun 16, 2023

Conversation

sapphi-red
Copy link
Contributor

When globalName: 'globalFoo' is set and the output format is iife, the output for the following input was not correct.
The variable globalFoo is not declared in the output.

// input
export default 'foo'

// output (formatted)
'use strict'
;(() => {
  var e = Object.defineProperty,
    t = Object.getOwnPropertyDescriptor,
    r = Object.getOwnPropertyNames,
    o = Object.prototype.hasOwnProperty,
    a = {}
  ;((t, r) => {
    for (var o in r) e(t, o, { get: r[o], enumerable: !0 })
  })(a, { default: () => n })
  var f,
    n = 'foo'
  ;(f = a),
    ((a, f, n, c) => {
      if ((f && 'object' == typeof f) || 'function' == typeof f)
        for (let l of r(f))
          o.call(a, l) ||
            l === n ||
            e(a, l, {
              get: () => f[l],
              enumerable: !(c = t(f, l)) || c.enumerable,
            })
    })(e({}, '__esModule', { value: !0 }), f)
})()

This PR fixes this issue by avoid setting toplevel: true when the format is iife and globalName option is set.

// new output (formatted)
'use strict'
var globalFoo = (() => {
  var e = Object.defineProperty,
    r = Object.getOwnPropertyDescriptor,
    t = Object.getOwnPropertyNames,
    o = Object.prototype.hasOwnProperty,
    a = {}
  ;((r, t) => {
    for (var o in t) e(r, o, { get: t[o], enumerable: !0 })
  })(a, { default: () => l })
  var n,
    l = 'foo'
  return (
    (n = a),
    ((a, n, l, f) => {
      if ((n && 'object' == typeof n) || 'function' == typeof n)
        for (let u of t(n))
          o.call(a, u) ||
            u === l ||
            e(a, u, {
              get: () => n[u],
              enumerable: !(f = r(n, u)) || f.enumerable,
            })
      return a
    })(e({}, '__esModule', { value: !0 }), n)
  )
})()

@codesandbox
Copy link

codesandbox bot commented May 5, 2023

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@vercel
Copy link

vercel bot commented May 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tsup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 16, 2023 6:33am

@egoist egoist merged commit 2c0435a into egoist:dev Jun 16, 2023
8 checks passed
@github-actions
Copy link

🎉 This PR is included in version 7.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@sapphi-red sapphi-red deleted the fix/terser-iife-preserve-top-level branch June 16, 2023 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants