Named import umd module returns null when the module uses the export let
syntax
#14353
Open
7 tasks done
Labels
feat: deps optimizer
Esbuild Dependencies Optimization
p3-minor-bug
An edge case that only affects very specific usage (priority)
Describe the bug
I used the vue-amap library in my project. I found that when I imported it using
import { lazyAMapApiLoaderInstance } from 'vue-amap'
, the value oflazyAMapApiLoaderInstance
was alwaysnull
instead of the latest value.The source code of
lazyAMapApiLoaderInstance
uses theexport let
syntax to delay initialization:ref: https://stackoverflow.com/a/32558929
Reproduction
https://stackblitz.com/edit/vitejs-vite-tdtcgx?file=src%2Fmain.js
Steps to reproduce
Use Vite and Vue CLI to start the project respectively. You can see from the log that the result in Vite is
null
, while the result in Vue CLI is the expectedobject
.Then build and run in production mode :
You can see that the behavior in production mode is the same as running in Vue CLI .
The source code after Vite transform is as follows:
However, the source code after Webpack transform is as follows:
The difference is that Webpack will convert
lazyAMapApiLoaderInstance
tovue_amap__WEBPACK_IMPORTED_MODULE_0__["lazyAMapApiLoaderInstance"]
, while Vite simply defines a local variable to take the value, soconsole.log
cannot read the latest value.So I think the behavior of Vite should be consistent with Webpack, at least the development mode and production mode should be consistent in Vite.
There are two ways to solve this problem:
Rename variable bindings like Webpack .
If simply use RegExp to replace them, it may affect other comments, strings, and local variables with the same name.
If AST is introduced for parsing, things may become much more complicated. I'm not familiar with AST either.
This should be documented, in this case the namespace import should be used instead of the named import as shown below. Also throwing warning in transformCjsImport .
vite/packages/vite/src/node/plugins/importAnalysis.ts
Line 1002 in c796959
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: