vite-userscript-plugin 1.10.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/virtual.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ declare module 'virtual:vite-userscript-plugin' {
2
+ export const scripts: {
3
+ name: string
4
+ version: string
5
+ file: string
6
+ }[]
7
+ }
package/dist/index.cjs DELETED
@@ -1,5 +0,0 @@
1
- "use strict";var re=Object.create;var v=Object.defineProperty;var se=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.getPrototypeOf,ie=Object.prototype.hasOwnProperty;var ae=(e,t)=>{for(var r in t)v(e,r,{get:t[r],enumerable:!0})},E=(e,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of ne(t))!ie.call(e,n)&&n!==r&&v(e,n,{get:()=>t[n],enumerable:!(s=se(t,n))||s.enumerable});return e};var j=(e,t,r)=>(r=e!=null?re(oe(e)):{},E(t||!e||!e.__esModule?v(r,"default",{value:e,enumerable:!0}):r,e)),le=e=>E(v({},"__esModule",{value:!0}),e);var ue={};ae(ue,{default:()=>K});module.exports=le(ue);var c=require("fs"),V=require("http"),p=require("path"),H=j(require("get-port"),1),W=j(require("open"),1),M=j(require("picocolors"),1),q=j(require("serve-handler"),1),z=require("vite"),B=require("websocket");var g=class{constructor(t){this.config=t;this.addHomepageMeta(),this.maxKeyLength=Math.max(...Object.keys(this.config).map(r=>r.length))+1}header=[];maxKeyLength;addHomepageMeta(){let t=this.config.homepage??this.config.homepageURL;t&&(this.config.updateURL=new URL(`${this.config.name}.meta.js`,t).href,this.config.downloadURL=new URL(`${this.config.name}.user.js`,t).href)}addSpaces(t){return" ".repeat(this.maxKeyLength-t.length)}addMetadata(t,r){r=Array.isArray(r)?r.join(" "):r===!0?"":r,this.header.push(`// @${t}${this.addSpaces(t)}${r}`)}generate(){for(let[t,r]of Object.entries(this.config))if(Array.isArray(r))r.forEach(s=>this.addMetadata(t,s));else{if(r===void 0)continue;this.addMetadata(t,r)}return["// ==UserScript==",...this.header,"// ==/UserScript=="].join(`
2
- `)}};var _=require("path"),D=require("url"),me={},L=(0,_.dirname)((0,D.fileURLToPath)(me.url)),$="vite-userscript-plugin",R='console.warn("__STYLE__")',N=new RegExp(/\.(t|j)sx?$/),C=new RegExp(/\.(s[ac]|c|le)ss$/),ce=["setValue","getValue","deleteValue","listValues","setClipboard","addStyle","addElement","addValueChangeListener","removeValueChangeListener","registerMenuCommand","unregisterMenuCommand","download","getTab","getTabs","saveTab","openInTab","notification","getResourceURL","getResourceText","xmlhttpRequest","log","info"],pe=["unsafeWindow","window.onurlchange","window.focus","window.close"],h=ce.map(e=>[`GM_${e}`,`GM.${e}`]).flat();h.push(...pe);var k=require("vite");function O(e){return[...new Set(Array.isArray(e)?e:e?[e]:[])]}async function y({minify:e,file:t,name:r,loader:s},n){let{code:d}=await(0,k.transformWithEsbuild)(t,r,{minify:e,loader:s,sourcemap:!1,legalComments:"none",...n});return d}function A(e){let t=[];for(let r of h)e.indexOf(r)!==-1&&t.push(r);return t}var U=class{styles=new Map;async add(t,r,s){let n=await y({name:t,file:r,minify:s,loader:"css"});return this.styles.set(t,n.replace(`
3
- `,"")),""}inject(){return this.styles.size?`GM_addStyle(\`${[...this.styles.values()].join("")}\`)`:void 0}merge(t){let r=[];for(let s of t){let n=this.styles.get(s);n&&r.push([s,n])}this.styles.clear(),r.forEach(s=>this.styles.set(...s))}},S=new U;function K(e){try{let t,r,s=null,n=e.fileName??e.header.name,d=(0,z.createLogger)("info",{prefix:`[${$}]`,allowClearScreen:!0}),w=(0,V.createServer)((o,i)=>(0,q.default)(o,i,{public:t.build.outDir})),I=B.server;return new I({httpServer:w}).on("request",o=>{s=o.accept(null,o.origin)}),{name:$,apply:"build",config(){return{build:{target:"esnext",minify:!1,lib:{name:n,entry:e.entry,formats:["iife"],fileName:()=>`${n}.js`},rollupOptions:{output:{extend:!0}}}}},async configResolved(o){t=o,r=o.build.watch??!1,e.entry=(0,p.resolve)(o.root,e.entry),Array.from(["match","require","include","exclude","resource","connect"]).forEach(i=>{let a=e.header[i];e.header[i]=O(a)}),e.server={port:await(0,H.default)(),open:!1,...e.server}},async transform(o,i){let a=o;return C.test(i)&&(a=await S.add(i,a,!r)),i.includes(e.entry)&&(a=o+R),{code:a,map:null}},generateBundle(o,i){for(let a of Object.values(i)){if(a.type==="asset")continue;let m=Object.keys(a.modules).filter(u=>C.test(u));m.length&&S.merge(m)}},async writeBundle(o,i){let{open:a,port:m}=e.server,u=o.sanitizeFileName(n),J=`${u}.user.js`,G=`${u}.proxy.user.js`,Y=`${u}.meta.js`;for(let[f]of Object.entries(i))if(N.test(f)){let x=t.root,b=t.build.outDir,P=(0,p.resolve)(x,b,f),Q=(0,p.resolve)(x,b,J),X=(0,p.resolve)(x,b,G),Z=(0,p.resolve)(x,b,Y),T=(0,p.resolve)(L,`ws-${u}.js`);try{let l=(0,c.readFileSync)(P,"utf8");if(l=l.replace(R,`${S.inject()}`),l=await y({minify:!r,file:l,name:f,loader:"js"},e.esbuildTransformOptions),e.header.grant=O(r?h:[...A(l),...e.header.grant??[]]),r){let ee=(0,c.readFileSync)((0,p.resolve)(L,"ws.js"),"utf8"),te=await y({minify:!r,file:ee.replace("__WS__",`ws://localhost:${m}`),name:T,loader:"js"},e.esbuildTransformOptions);(0,c.writeFileSync)(T,te),(0,c.writeFileSync)(X,new g({...e.header,require:[...e.header.require??[],"file://"+T,"file://"+P]}).generate())}let F=new g(e.header).generate();(0,c.writeFileSync)(P,l),(0,c.writeFileSync)(Z,F),(0,c.writeFileSync)(Q,`${F}
4
-
5
- ${l}`)}catch(l){console.log(l)}}if(r&&!w.listening){let f=`http://localhost:${m}`;w.listen(m,()=>{d.clearScreen("info"),d.info(M.default.bold(`${M.default.cyan(">>> [vite-userscript-plugin]")} ${M.default.gray(f)}`))}),a&&await(0,W.default)(`${f}/${G}`)}else r||(w.close(),process.exit(0))},buildEnd(){r&&(d.clearScreen("info"),s&&s.sendUTF(JSON.stringify({message:"reload"})))}}}catch(t){return console.error(t),{name:$}}}
package/dist/index.d.cts DELETED
@@ -1,187 +0,0 @@
1
- import { EsbuildTransformOptions, PluginOption } from 'vite';
2
-
3
- declare const GM: readonly ["setValue", "getValue", "deleteValue", "listValues", "setClipboard", "addStyle", "addElement", "addValueChangeListener", "removeValueChangeListener", "registerMenuCommand", "unregisterMenuCommand", "download", "getTab", "getTabs", "saveTab", "openInTab", "notification", "getResourceURL", "getResourceText", "xmlhttpRequest", "log", "info"];
4
- declare const GMwindow: readonly ["unsafeWindow", "window.onurlchange", "window.focus", "window.close"];
5
-
6
- type RunAt = 'document-start' | 'document-body' | 'document-end' | 'document-idle' | 'context-menu';
7
- type GMLiterals<T extends string> = [`GM_${T}` | `GM.${T}`];
8
- type GMWindow = typeof GMwindow[number];
9
- type Grants = GMWindow | GMLiterals<typeof GM[number]>[number];
10
- type HeaderConfig = {
11
- [property: string]: any;
12
- /**
13
- * @see https://www.tampermonkey.net/documentation.php#meta:name
14
- */
15
- name: string;
16
- /**
17
- * @see https://www.tampermonkey.net/documentation.php#meta:namespace
18
- */
19
- namespace?: string;
20
- /**
21
- * @see https://www.tampermonkey.net/documentation.php#meta:copyright
22
- */
23
- copyright?: string;
24
- /**
25
- * @see https://www.tampermonkey.net/documentation.php#meta:version
26
- */
27
- version: string;
28
- /**
29
- * @see https://www.tampermonkey.net/documentation.php#meta:description
30
- */
31
- description?: string;
32
- /**
33
- * @see https://www.tampermonkey.net/documentation.php#meta:icon
34
- */
35
- icon?: string;
36
- /**
37
- * @see https://www.tampermonkey.net/documentation.php#meta:icon
38
- */
39
- iconURL?: string;
40
- /**
41
- * @see https://www.tampermonkey.net/documentation.php#meta:icon
42
- */
43
- defaulticon?: string;
44
- /**
45
- * @see https://www.tampermonkey.net/documentation.php#meta:icon64
46
- */
47
- icon64?: string;
48
- /**
49
- * @see https://www.tampermonkey.net/documentation.php#meta:icon64
50
- */
51
- icon64URL?: string;
52
- /**
53
- * @see https://www.tampermonkey.net/documentation.php#meta:grant
54
- */
55
- grant?: Grants[];
56
- /**
57
- * @see https://www.tampermonkey.net/documentation.php#meta:author
58
- */
59
- author?: string;
60
- /**
61
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
62
- */
63
- homepage?: string;
64
- /**
65
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
66
- */
67
- homepageURL?: string;
68
- /**
69
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
70
- */
71
- website?: string;
72
- /**
73
- * @see https://www.tampermonkey.net/documentation.php#meta:homepage
74
- */
75
- source?: string;
76
- /**
77
- * @see https://www.tampermonkey.net/documentation.phpmeta:antifeature
78
- */
79
- antifeature?: [type: string, description: string][];
80
- /**
81
- * @see https://www.tampermonkey.net/documentation.php#meta:require
82
- */
83
- require?: string[] | string;
84
- /**
85
- * @see https://www.tampermonkey.net/documentation.php#meta:resource
86
- */
87
- resource?: [key: string, value: string][];
88
- /**
89
- * @see https://www.tampermonkey.net/documentation.php#meta:include
90
- */
91
- include?: string[] | string;
92
- /**
93
- * @see https://www.tampermonkey.net/documentation.php#meta:match
94
- * @see https://violentmonkey.github.io/api/metadata-block/#match--exclude-match
95
- */
96
- match: string[] | string;
97
- /**
98
- * @see https://violentmonkey.github.io/api/metadata-block/#match--exclude-match
99
- */
100
- 'exclude-match'?: string[] | string;
101
- /**
102
- * @see https://www.tampermonkey.net/documentation.php#meta:exclude
103
- */
104
- exclude?: string[] | string;
105
- /**
106
- * @see https://www.tampermonkey.net/documentation.php#meta:run_at
107
- */
108
- 'run-at'?: RunAt;
109
- /**
110
- * @see https://www.tampermonkey.net/documentation.phpmeta:sandbox
111
- */
112
- sandbox?: string;
113
- /**
114
- * @see https://www.tampermonkey.net/documentation.php#meta:connect
115
- */
116
- connect?: string[] | string;
117
- /**
118
- * @see https://www.tampermonkey.net/documentation.php#meta:noframes
119
- */
120
- noframes?: boolean;
121
- /**
122
- * @see https://www.tampermonkey.net/documentation.php#meta:updateURL
123
- */
124
- updateURL?: string;
125
- /**
126
- * @see https://www.tampermonkey.net/documentation.php#meta:downloadURL
127
- */
128
- downloadURL?: string;
129
- /**
130
- * @see https://www.tampermonkey.net/documentation.php#meta:supportURL
131
- */
132
- supportURL?: string;
133
- /**
134
- * @see https://www.tampermonkey.net/documentation.php#meta:webRequest
135
- */
136
- webRequest?: string[];
137
- /**
138
- * @see https://www.tampermonkey.net/documentation.php#meta:unwrap
139
- */
140
- unwrap?: boolean;
141
- };
142
- interface ServerConfig {
143
- /**
144
- * {@link https://github.com/sindresorhus/get-port}
145
- */
146
- port?: number;
147
- /**
148
- * @default false
149
- */
150
- open?: boolean;
151
- }
152
- interface UserscriptPluginConfig {
153
- /**
154
- * Path of userscript entry.
155
- */
156
- entry: string;
157
- /**
158
- * Userscript file name.
159
- */
160
- fileName?: string;
161
- /**
162
- * Userscript header config.
163
- *
164
- * @see https://www.tampermonkey.net/documentation.php
165
- */
166
- header: HeaderConfig;
167
- /**
168
- * Server config.
169
- */
170
- server?: ServerConfig;
171
- /**
172
- * Override default esbuild transform options.
173
- *
174
- * @default
175
- * ```json
176
- * {
177
- * "minify": true,
178
- * "legalComments": "none"
179
- * }
180
- * ```
181
- */
182
- esbuildTransformOptions?: Omit<EsbuildTransformOptions, 'format' | 'target' | 'loader' | 'sourcemap'>;
183
- }
184
-
185
- declare function UserscriptPlugin(config: UserscriptPluginConfig): PluginOption;
186
-
187
- export { UserscriptPluginConfig, UserscriptPlugin as default };
package/dist/ws.js DELETED
@@ -1 +0,0 @@
1
- function n(){let e=new WebSocket("__WS__");e.addEventListener("close",()=>{setTimeout(n,1e3)}),e.addEventListener("error",()=>{e.close()}),e.addEventListener("message",()=>{location.reload()})}n();
package/types/README.md DELETED
@@ -1,21 +0,0 @@
1
- # Userscript type definitions
2
-
3
- [![tampermonkey](https://img.shields.io/npm/v/@types/tampermonkey?label=%40types%2Ftampermonkey)](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/tampermonkey)
4
- [![greasemonkey](https://img.shields.io/npm/v/@types/greasemonkey?label=%40types%2Fgreasemonkey)](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/greasemonkey)
5
- [![violentmonkey](https://img.shields.io/npm/v/@violentmonkey/types?label=%40violentmonkey%2Ftypes)](https://github.com/violentmonkey/types)
6
-
7
- > Type declaration for `GM_*`, `GM.*` APIs in Tampermonkey, Greasemonkey and Violentmonkey.
8
-
9
- ## Installation
10
-
11
- ```sh
12
- npm install --save @types/tampermonkey
13
- ```
14
-
15
- ```sh
16
- npm install --save @types/greasemonkey
17
- ```
18
-
19
- ```sh
20
- npm install --save @violentmonkey/types
21
- ```