lisonge / vite-plugin-monkey

A vite plugin server and build your.user.js for userscript engine like Tampermonkey, Violentmonkey, Greasemonkey, ScriptCat
MIT License
1.33k stars 70 forks source link

Uncaught ReferenceError: __MONKEY_WINDOW__ is not defined #32

Closed jianzhichun closed 1 year ago

jianzhichun commented 1 year ago

Uncaught ReferenceError: __MONKEY_WINDOW__ is not defined

lisonge commented 1 year ago

可否给出报错截图,你这个错误应该来自 https://unpkg.com/browse/vite-plugin-monkey@2.9.0/dist/client/index.mjs

貌似是路径分隔符的问题

lisonge commented 1 year ago

如果你想手动修复它,可以手动配置 define

// vite.config.ts
import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';

export default defineConfig(({ command }) => {
  return {
    define: {
      __MONKEY_WINDOW__:
        command == 'serve' ? `document.__monkeyWindow` : `window`,
    },
    plugins: [
      monkey({
        entry: 'src/main.ts',
        userscript: {
          icon: 'https://vitejs.dev/logo.svg',
          namespace: 'npm/vite-plugin-monkey',
          match: ['https://i.songe.li/*'],
          connect: [`httpbin.org`, `i.pximg.net`],
        },
      }),
    ],
  };
});
lisonge commented 1 year ago

可以试试安装 v2.9.1

lisonge commented 1 year ago

是插件没有作用到 vite 的预构建,我修复一下即可

lisonge commented 1 year ago

fixed by v2.9.2

pnpm add vite-plugin-monkey@2.9.2
jianzhichun commented 1 year ago

3q

lisonge commented 1 year ago

使用 define 后该问题仍然会概率出现,这属于 以前未发现的问题

<=v2.9.2 的注入模式使得 /@vite/client/src/main.ts 并行下载,无法保证正确的执行顺序

该问题已在 v2.9.3 解决

pnpm add vite-plugin-monkey@2.9.3