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

UMD方式引入时,二维码组件报错,导致浏览器不兼容 ReferenceError: naive is not defined #5908

Open
5 tasks done
rysonliu opened this issue Apr 28, 2024 · 3 comments
Labels
untriaged need to sort

Comments

@rysonliu
Copy link

描述错误

在使用较低,但不是很低版本的浏览器,并且用UMD方式引入时,浏览器不兼容 ReferenceError: naive is not defined

复现步骤

1.使用较低版本chrome,例如:https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Win_x64/800850/
2.用上述版本浏览器访问官方文档的UMD演示页面:https://jsbin.com/mutewosewu/edit?html,console,output
3.js会有报错,页面也无法正常渲染
4.访问官方文档的非UMD方式导入的二维码组件页面是正常的:https://www.naiveui.com/zh-CN/os-theme/components/qr-code

最小复现链接

https://jsbin.com/mutewosewu/edit?html,console,output

系统信息

chrome:87.0.4242.0
naiveUI:2.38.1
vue:latest

使用的包管理器

pnpm

验证

@rysonliu rysonliu added the untriaged need to sort label Apr 28, 2024
@rysonliu
Copy link
Author

本地报错截图显示是二维码组件部分的代码引起的:

微信图片_20240428191021

@lisonge
Copy link

lisonge commented May 1, 2024

是 umd 的 build.target 没有设置或为 esnext 引起的

const umdConfig = defineConfig({
output: {
name: 'naive',
format: 'umd',
exports: 'named',
globals: {
vue: 'Vue'
}
}
})

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks#browser_compatibility

根据 Static_initialization_blocks 的兼容性 chrome94,这意味着 naive-ui 的 umd 链接只能在 chrome94 以上工作


ui 库的 umd 这种能独立直接使用的格式实际上应该设置较低的转译目标,并且应该自包含 polyfill

@rysonliu
Copy link
Author

rysonliu commented May 1, 2024

是 umd 的 build.target 没有设置或为 esnext 引起的

const umdConfig = defineConfig({
output: {
name: 'naive',
format: 'umd',
exports: 'named',
globals: {
vue: 'Vue'
}
}
})

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks#browser_compatibility

根据 Static_initialization_blocks 的兼容性 chrome94,这意味着 naive-ui 的 umd 链接只能在 chrome94 以上工作

感谢答复!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged need to sort
Projects
None yet
Development

No branches or pull requests

2 participants