目的:
CommonJS 和 UMD 兼容性。将
CommonJS和UMD规范转化为ES模块规范,以便浏览器支持。性能。将
lodash这样的具有大量内置模块的包,转化为单个模块。
TIP
笔者实际测试了下,发现无论是开发环境,还是生产环境,vite 对于 import { debounce } from 'lodash' 这样的语法,其实是将整个 lodash 包打成了一个依赖。并没有做 tree shaking。
- 转化第三方包。譬如浏览器对于
import { debounce } from 'lodash'是无法解析的。报错:Uncaught TypeError: Failed to resolve module specifier "lodash". Relative references must start with either "/", "./", or "../"。