Closed ShivamS136 closed 9 months ago
import { defineConfig } from 'vite';
import solid from 'vite-plugin-solid';
import monkey from 'vite-plugin-monkey';
const comment = `
// ==UserScript==
// @name solid-ts
// @namespace npm/vite-plugin-monkey
// @version 0.0.0
// @author monkey
// @icon https://vitejs.dev/logo.svg
// @match https://www.google.com/
// @grant GM_addStyle
// ==/UserScript==
`.trim();
export default defineConfig({
plugins: [
solid(),
monkey({
entry: 'src/main.tsx',
format: {
generate() {
return comment;
},
},
}),
],
});
What does format do? How it'll help me in keeping my userscript metablock as it is in my .user.js
files? And what is the significance of comment
value?
Also, please confirm the use of solid
is irrelevant in the example code shared, isn't it?
How to use Userscript comments added in the file instead of userscript object
to use Userscript comment
import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';
import fs from 'node:fs/promises';
const fileText = await fs.readFile(srcFileName, 'utf-8');
const customUserScript =
fileText.match(/\/\/ ==UserScript==[\s\S]*\/\/ ==\/UserScript==/)?.[0] || ``;
export default defineConfig({
plugins: [
monkey({
entry: srcFileName,
format: {
generate() {
return customUserScript;
},
},
}),
],
});
That works like a charm.
Thanks a lot. I've spent more than 5 hours to achieve this via vite, rollup, terser and the vite-plugin-monkey but couldn't succeed.
Just a suggestion to add a more detail about the plugin config parameters for newbies like me. That would be very helpful.
I have a package in which I am having multiple userscript files and building those using
vite
andvite-plugin-monkey
. But now I want to extend the functionality of my package to have vanilla.user.js
files as well for some backward compatibility and user requirement purpose.I am able to build the file in my dist folder by simply copying the file into dist folder using
rsync
.But I want to take advantage of
vite dev
mode as well for these files. I want the HMR facility for my plain.user.js
files as well which has the// ==UserScript==
section already.So is there anyway that I can use
vite
and/orvite-plugin-monkey
so that I can achieve the same dev mode experience as I have with normal.js
files build viavite-plugin-monkey
?I tried with below config but it replaced my
// ==UserScript==
section with default values fromvite-plugin-monkey
and that's not what is expected.I tried using without
vite-plugin-monkey
as well but that didn't help as I can't see the downloadable output file after runningvite dev
and getting below error withvite build
.Error on build: