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

依赖中代码出现匹配编译器宏导致报错 #4524

Closed
enpitsuLin opened this issue Sep 11, 2023 · 5 comments
Closed

依赖中代码出现匹配编译器宏导致报错 #4524

enpitsuLin opened this issue Sep 11, 2023 · 5 comments

Comments

@enpitsuLin
Copy link

初步排除应该是使用了 ol 其中的依赖引用到了不同版本的 pako 这个库, 而 uniapp 中也用到了(可能不是这个原因)

然后在预处理这个库的代码中匹配到了类似 # ifdef 的宏 但是没有结束语句, 然后最后会报 Unbalanced delimiter found in string

workaround

用户可以打个 patch 跳过对 node_modules 中依赖的预处理, 这个修复方法应该是蛮合理的,官方看看可以不可以合并得了

diff --git a/dist/plugin/esbuild/esbuildPrePlugin.js b/dist/plugin/esbuild/esbuildPrePlugin.js
index b49d7de5e927de355a0320078ee6c63a63cbd8a6..531751c41c585e1e8c849d988772c96b5b3d0477 100644
--- a/dist/plugin/esbuild/esbuildPrePlugin.js
+++ b/dist/plugin/esbuild/esbuildPrePlugin.js
@@ -17,7 +17,7 @@ function esbuildPrePlugin() {
                 if (ext === 'mjs')
                     ext = 'js';
                 let contents = fs_1.default.readFileSync(id, 'utf-8');
-                if (contents.includes('#endif')) {
+                if (contents.includes('#endif')&&!id.includes('node_modules')) {
                     contents = (0, uni_cli_shared_1.preJs)(contents);
                 }
                 return {
@enpitsuLin
Copy link
Author

或者官方好好重写下这个编译宏的逻辑,但我觉得不大可能了,有遇到类似 Unbalanced delimiter found in string 的错误可以看看是不是依赖中匹配到了这个字符串

@Otto-J
Copy link
Member

Otto-J commented May 21, 2024

感谢反馈,你提到了如果 endif 结束逻辑可以忽略 node_modules ,并且提到了 ol 这个库。可以进一步解释,忽略 node_modules 和 ol 这个库的关系吗?
有业务场景允许用户通过 npm 的内容直接作为 page/components 页面

@enpitsuLin
Copy link
Author

时间有点太久了 原先的项目应该是用ol渲染地图,然后它的依赖中有 https://github.com/nodeca/pako 这个库,然后里面有 uni-app 编译宏作注释的,虽然也是成一对的,但不知道为什么没正常处理会报错,然后就patch解决了

https://github.com/nodeca/pako/blob/62cb729e7813176ce2d2694b89c8724680fca383/lib/zlib/deflate.js#L550

@Lazybin
Copy link

Lazybin commented May 31, 2024

我也遇到了这个问题Unbalanced delimiter found in string 请问是怎么解决的

@Lazybin
Copy link

Lazybin commented Jun 5, 2024

感谢反馈,你提到了如果 endif 结束逻辑可以忽略 node_modules ,并且提到了 ol 这个库。可以进一步解释,忽略 node_modules 和 ol 这个库的关系吗? 有业务场景允许用户通过 npm 的内容直接作为 page/components 页面

我按enpitsuLin所说的对esbuildPrePlugin.js做了修改,但是在app打包测试的时候又出现了这个错误。我也用了有pako依赖的包。
报这个错误:
[plugin:commonjs--resolver] Unbalanced delimiter found in string

请问能解决下吗?

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

3 participants