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

[Bug] type.ts里的js加载都到闭包里去了 #10934

Closed
Vantown opened this issue Apr 10, 2023 · 9 comments
Closed

[Bug] type.ts里的js加载都到闭包里去了 #10934

Vantown opened this issue Apr 10, 2023 · 9 comments

Comments

@Vantown
Copy link

Vantown commented Apr 10, 2023

3.x放在document.ejs里的很多方法包括jssdk都是通过window.xx调用的,现在换成plugin.ts以后,这些库都被放到闭包里去了,这样外面无法调用了,还有什么别的方式可行吗?
image

@github-actions
Copy link

由于缺乏足够的信息,我们暂时关闭了该 Issue。请修改(不要回复) Issue 提供最小重现以重新开启。谢谢。

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 10, 2023
@fz6m
Copy link
Contributor

fz6m commented Apr 10, 2023

umi 3 不维护了,建议升级到 umi 4

@Vantown
Copy link
Author

Vantown commented Apr 10, 2023

umi 3 不维护了,建议升级到 umi 4

是4.x啊,我是用的4.x的plugin.ts加载的,那不没document.ejs了吗?我现在就是用的4.x的plugin.ts加载的以前在document.ejs里声明的一些变量和方法,但是打包的时候全到闭包里去了

@Vantown
Copy link
Author

Vantown commented Apr 10, 2023

umi 3 不维护了,建议升级到 umi 4

仔细看了看发现一个规律,如果加载的js里有全局变量的话,就会被套到闭包里去,有什么办法能关闭这种策略吗?

@fz6m
Copy link
Contributor

fz6m commented Apr 10, 2023

闭包是指啥,修改产物 html 吗,用 headScripts 、scripts 、links 等选项添加字符串就可以了,参考 这里

@Vantown
Copy link
Author

Vantown commented Apr 10, 2023

闭包是指啥,修改产物 html 吗,用 headScripts 、scripts 、links 等选项添加字符串就可以了,参考 这里

就像我第一张截图那样pathname本来是在window上,后面还有很多变量和方法,现在打包的时候这些被套在一个闭包里了
!(function(){const path = ....})();
像我现在放在modifyHTML里是可以拿到publicPath的这样实现了以前的document.ejs里的<%= context.config.publicPath %>打包不同环境不用引用的问题,要是放到headScript里了之后,除了在不同打包config文件里配置不同的引用地址还有别的方式吗?

@Vantown
Copy link
Author

Vantown commented Apr 10, 2023

lADPKH7Rzoyv_gTNBaDNB4A_1920_1440
放到headScriptheadScripts里以后打包的时候还是被改动了,左侧的是原始代码,右侧是打包以后的,里面的没动,最外层套了个匿名函数

@fz6m
Copy link
Contributor

fz6m commented Apr 11, 2023

这个问题在 #10904 被解决,但还没发版,等待本周四下一次发版吧,全局变量可以明确指定 window.xxx 使用。

@Vantown
Copy link
Author

Vantown commented Apr 11, 2023

这个问题在 #10904 被解决,但还没发版,等待本周四下一次发版吧,全局变量可以明确指定 window.xxx 使用。

好的好的,多谢

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

No branches or pull requests

2 participants