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

Invalid define value (must be an entity name or valid JSON syntax): {"BASE_URL":"./","MODE":"production","DEV":false,"PROD":true,"LEGACY":__VITE_IS_LEGACY__} #31

Closed
pfdgithub opened this issue May 23, 2023 · 3 comments

Comments

@pfdgithub
Copy link
Contributor

pfdgithub commented May 23, 2023

vite@4.3.8@vitejs/plugin-legacy@4.0.3下,启用 options.build 配置后,执行npm run build会报错:

Invalid define value (must be an entity name or valid JSON syntax): {"BASE_URL":"./","MODE":"production","DEV":false,"PROD":true,"LEGACY":__VITE_IS_LEGACY__}

生成了mockServer目录和文件,但mock-data.js文件中是空的。

可能与 babelrollup 版本有关?没往下查,可参考:
vitejs/vite#2442
babel/babel#13017
rollup/plugins#838

@pengzhanbo
Copy link
Owner

经过检查,发现是 @vitejs/plugin-legacy 插件会在 viteConfig.define 配置中插入{ "import.meta.env.LEGACY": '__VITE_IS_LEGACY__' } 。在mock插件中,也会使用 viteConfig.define 用于 插件内部 esbuild 的 define 配置。
但是 在 esbuild 内部 编译时, '__VITE_IS_LEGACY__' 会被替换为一个普通的代码片段 __VITE_IS_LEGACY__,由于代码未定义该变量,导致了编译错误。

@pengzhanbo
Copy link
Owner

由于 不好判断 viteConfig.define 中的配置,哪些是用户配置的,哪些是插件配置的,以及是否是 注入的待替换字符,目前的解决方式是 过滤掉 import.meta.env.LEGACY

pengzhanbo added a commit that referenced this issue May 23, 2023
@pengzhanbo
Copy link
Owner

pengzhanbo commented May 23, 2023

采用了新的思路, viteConfig.define 配置的字段,如果字符串值 无法被 JSON.parse 解析为 基础类型或 普通对象、普通数组,则将其过滤掉。 从而适配其他可能使用的插件传入的 define 或用户配置的 define 中存在的类似问题。

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

No branches or pull requests

2 participants