lisonge / vite-plugin-monkey

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

`@require`库在dev中未定义,但在build中定义 #113

Closed LeviOP closed 10 months ago

LeviOP commented 10 months ago

我正在尝试在vite-plugin-monkey中使用GM_config库。

src/main.ts

// @ts-nocheck
import { monkeyWindow } from "$";

console.log(window.GM_config);
console.log(monkeyWindow.GM_config);
console.log(GM_config);

vite.config.ts

import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    monkey({
      entry: 'src/main.ts',
      userscript: {
        namespace: 'npm/vite-plugin-monkey',
        match: ['https://www.google.com/'],
        require: ["https://openuserjs.org/src/libs/sizzle/GM_config.js"]
      },
    }),
  ],
});

在dev:

undefined
undefined
Uncaught ReferenceError: GM_config is not defined

在build:

undefined
undefined
function config() //我所期望的

我该如何访问GM_config? (在dev或build)

lisonge commented 10 months ago
// vite.config.ts
import { defineConfig } from 'vite';
import monkey, { util } from 'vite-plugin-monkey';

export default defineConfig({
  plugins: [
    monkey({
      entry: 'src/main.tsx',
      userscript: {
        require: [
          'https://openuserjs.org/src/libs/sizzle/GM_config.js',
          util.dataUrl(`window.GM_config=GM_config`),
        ],
      },
    }),
  ],
});
// main.tsx
import { monkeyWindow } from '$';
console.log(monkeyWindow.GM_config);
LeviOP commented 10 months ago

谢谢