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

runtime下的window不对劲 #8021

Closed
cjz9032 opened this issue Nov 11, 2020 · 7 comments · Fixed by #8105
Closed

runtime下的window不对劲 #8021

cjz9032 opened this issue Nov 11, 2020 · 7 comments · Fixed by #8105
Labels
enhancement New feature or request
Milestone

Comments

@cjz9032
Copy link

cjz9032 commented Nov 11, 2020

这个特性解决了什么问题?

引入一些三方库如mobx-state-tree会报Symbol不存在,因为他会从window下拿取需要的属性

这个 API 长什么样?

taro-runtime的window应Proxy当前环境的global变量下的属性, 如Map,Symbol

@atzcl
Copy link
Contributor

atzcl commented Nov 23, 2020

遇到同样的问题

@atzcl
Copy link
Contributor

atzcl commented Nov 23, 2020

使用了 mobx 等库的,可以临时通过修改源码的方式修复

// node_modules/@tarojs/runtime/dist/runtime.esm.js

const window$1 = isBrowser ? win : {
    navigator,
    document: document$1
};

// 在这里加上修复代码
if (!isBrowser) {
    Reflect.ownKeys(global).forEach(property => {
        if (!Object.prototype.hasOwnProperty.call(window$1, property)) {
            window$1[property] = global[property];
        }
    })
}

@cjz9032
Copy link
Author

cjz9032 commented Nov 23, 2020

使用了 mobx 等库的,可以临时通过修改源码的方式修复

// node_modules/@tarojs/runtime/dist/runtime.esm.js

const window$1 = isBrowser ? win : {
    navigator,
    document: document$1
};

// 在这里加上修复代码
if (!isBrowser) {
    Reflect.ownKeys(global).forEach(key => {
        window$1[key] = global[key];
    })
}

他是用provider插件注入的, 我就修改了一下,不用改源码稍微方便点
chain.plugin('providerPlugin').tap((args) => { args[0].window = ['@/utils/adjust-taro-runtime', 'newWindow']; // adjust 里面保留了自身的window, 并注入你需要的属性 args[0].Taro = ['@tarojs/taro', 'default']; return args; });

@atzcl
Copy link
Contributor

atzcl commented Nov 23, 2020

@cjz9032 如果用这种方式的话,会导致某些用到了 window.navigatorwindow.document 的地方异常

@cjz9032
Copy link
Author

cjz9032 commented Nov 24, 2020

@cjz9032 如果用这种方式的话,会导致某些用到了 window.navigatorwindow.document 的地方异常

不会吧 只要吧 runtime也引入
export default { ...runtimeWindow, ...newWindow }

@atzcl
Copy link
Contributor

atzcl commented Nov 24, 2020

@cjz9032 如果用这种方式的话,会导致某些用到了 window.navigatorwindow.document 的地方异常

不会吧 只要吧 runtime也引入
export default { ...runtimeWindow, ...newWindow }

明白你的意思了,处理的核心是一样的,只是方式不同

@Chen-jj Chen-jj added this to the 3.0.19 milestone Dec 11, 2020
@Chen-jj
Copy link
Contributor

Chen-jj commented Sep 15, 2021

尴尬,微信环境好像有改动,现在 global("webpack/buildin/global") 获取不到这些属性了

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

Successfully merging a pull request may close this issue.

3 participants