-
Notifications
You must be signed in to change notification settings - Fork 80
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
部分依赖在油猴的环境中直接简单地用 @require 引入会产生冲突 #5
Comments
如何提供 |
@bzy-nya |
用 get + eval 或者 remote import ? |
嗯喵,我去康康 |
react-dom 依赖 react,但是它在油猴扩展内正常加载 虽然没有处理好还是会报错,但是在函数之前加上 |
因为 vue.js 是直接
但是 react.js 里是
然后这样的话油猴里 this.Vue 访问不到,但是 this.React 可以访问。 然后 element-plus 在 require 阶段就会报错(虽然咱感觉这个锅得油猴背)。 |
fixed -> https://github.com/lisonge/vite-plugin-monkey/blob/v1.1.2/CHANGELOG.md#112 |
Vue 的 cdn 使用 iife 格式, element-plus 的 cdn 使用 umd 格式,油猴脚本的环境不支持两者混用 解决方式1: 叫他们同时提供 iife 和 umd 的格式,或者像react/react-dom一样统一umd |
嗯嗯,谢谢了喵,这个 bug(or feature) 咱也给 TamperMonkey 那边提了 issues 但是没人回复所以就来这边了qwq |
对于方式2,可以直接用现有的 vite 做 // vite.config.ts
import { defineConfig } from 'vite';
// https://vitejs.dev/config/
export default defineConfig({
build: {
lib: {
name: 'ElementPlus',
entry: './node_modules/element-plus',
formats: ['iife'],
},
rollupOptions: {
external: ['vue'],
output: {
globals: {
vue: 'Vue',
},
exports: 'named',
},
},
},
}); |
can be fixed by manual build library |
突发奇想找到了一个解决方法,那就是在 库加载之前使用 @require 插入自定义代码 // ==UserScript==
// @name dev:example
// @namespace https://github.com/lisonge
// @version 1.0.1
// @require https://unpkg.com/vue@3.2.37//dist/vue.global.prod.js
// @require https://raw.githubusercontent.com/lisonge/src/main/js/monkey.js
// @require https://unpkg.com/element-plus@2.2.9//dist/index.full.js
// ==/UserScript==
// @grant none // 在此环境下也可行
console.log(`cdn load ok`); https://raw.githubusercontent.com/lisonge/src/main/js/monkey.js try{
this.Vue = this.Vue ?? Vue
}catch{}
try{
window.Vue = window.Vue ?? Vue
}catch{}
console.log({this:this, window, globalThis}); |
草,确实可以这样 |
我会在配置里扩展一下 |
Tampermonkey doese not support IIFE/UMD import at same time. See lisonge/vite-plugin-monkey#5
可以直接使用 dataurl 无需额外上传 cdn ,Tampermonkey ,Violentmonkey 我已测试过,均可行 {
vue: cdn.jsdelivr('Vue', 'dist/vue.global.prod.js').concat(
'data:application/javascript,' +
encodeURIComponent(
`;window.Vue=Vue;`,
),
)
} |
|
|
好的,没注意到下面的内容。。 |
例如
vue.js 会导出一个变量
Vue
,element 会寻找this.Vue
当成依赖,但是在油猴的环境中 this 被重定向到了 window 的一个 proxy(@grant none
就会指向空对象),所以就会发生报错说vue is not found
,即使 vue 变量已经在环境中了。所以能不能提供一个自定义依赖引入的方法呢?
The text was updated successfully, but these errors were encountered: