"this" parameter in iife transpiled another variable in production
See original GitHub issueDescribe 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing as Vite v3 does not use
rollup/plugin-commonjs
by default during build. This would still be an issue when settinglegacy.buildRollupPluginCommonjs
, and PRs are welcome to fix these, but we aren’t going to track this as a Vite issue moving forward.It’s simillar but slightly different. This one is a top-level this and that one is a this inside class.