question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"this" parameter in iife transpiled another variable in production

See original GitHub issue

Describe the bug

There is no issue in dev environment but in production when using “this” as parameter in iife, “this” should mean window object but transpiled another variable. For example I am using device-uuid package and this package using iife with “this” parameter.

Example Usage

(function (exports) {

    var DeviceUUID = function(options) {
        ...
    }

    exports.DeviceUUID = DeviceUUID;
    return new DeviceUUID(navigator.userAgent);

})(this);

Reproduction

https://github.com/karatasebu/iife-this-issue

System Info

System:
    OS: macOS 12.2.1
    CPU: (8) x64 Apple M1
    Memory: 22.84 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.5.4 - ~/.nvm/versions/node/v14.19.0/bin/npm
    Watchman: 2022.03.07.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 100.0.4896.75
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-vue: ^2.3.0 => 2.3.1 
    vite: ^2.9.0 => 2.9.1

Used Package Manager

npm

Logs

TypeError: nl.DeviceUUID is not a constructor

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, Jun 28, 2022

Closing as Vite v3 does not use rollup/plugin-commonjs by default during build. This would still be an issue when setting legacy.buildRollupPluginCommonjs, and PRs are welcome to fix these, but we aren’t going to track this as a Vite issue moving forward.

1reaction
sapphi-redcommented, Apr 14, 2022

It’s simillar but slightly different. This one is a top-level this and that one is a this inside class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

06 Immediately-Invoked Function Expressions (IIFE)
We pass in the string 'Andrew' , which is stored in the function expression's name variable. It is then immediately invoked, alerting the...
Read more >
Passing arguments to an IIFE - javascript - Stack Overflow
The IIFE is immediately invoked. You are passing foo to it at the moment of invocation, and I suppose it's undefined.
Read more >
The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >
Understanding (all) JavaScript module formats and tools
It is just another IIFE. When the anonymous function is called, its argument is evaluated. The argument evaluation detects the environment ( ...
Read more >
the different ways of declaring a function + 5 best practices
It is declared quite differently from the general syntax because it uses a variable to denote the name of the function. This variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found