Skip to content

Commit a0bf236

Browse files
committed
feat: fix auto-grant, refactor configResolved
1 parent ef28a57 commit a0bf236

File tree

6 files changed

+57
-54
lines changed

6 files changed

+57
-54
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ interface UserscriptPluginConfig {
9191
* Server config.
9292
*/
9393
server?: ServerConfig;
94-
/**
95-
* Import all `@grant` in development mode.
96-
* @default true
97-
*/
98-
autoGrants?: boolean;
9994
}
10095
```
10196

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dev": "tsup --watch",
1717
"build": "tsup",
1818
"test": "vitest",
19-
"test:ui": "vitest --ui",
19+
"test:ui": "vitest --ui --watch",
2020
"examples:dev": "turbo run dev --filter='./examples/*'",
2121
"examples:build": "turbo run build --filter='./examples/*'",
2222
"format": "prettier --write --ignore-unknown **",

src/config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { UserConfig } from 'vite'
2+
import type { HeaderConfig } from './types.js'
3+
4+
interface ViteConfig {
5+
entry: string
6+
header: HeaderConfig
7+
}
8+
9+
export function userConfig({ entry, header }: ViteConfig): UserConfig {
10+
return {
11+
build: {
12+
lib: {
13+
entry,
14+
name: header.name,
15+
formats: ['iife'],
16+
fileName: () => `${header.name}.js`
17+
},
18+
rollupOptions: {
19+
output: {
20+
extend: true
21+
}
22+
}
23+
}
24+
}
25+
}

src/index.ts

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { PluginOption, ResolvedConfig, createLogger } from 'vite'
1111
import { server } from 'websocket'
1212
import type { connection } from 'websocket'
1313
import { banner } from './banner.js'
14+
import { userConfig } from './config.js'
1415
import { grants, regexpScripts, regexpStyles } from './constants.js'
1516
import css from './css.js'
1617
import { defineGrants, removeDuplicates, transform } from './helpers.js'
@@ -46,38 +47,28 @@ export default function UserscriptPlugin(
4647
name: 'vite-userscript-plugin',
4748
apply: 'build',
4849
config() {
49-
return {
50-
build: {
51-
lib: {
52-
entry: config.entry,
53-
name: config.header.name,
54-
formats: ['iife'],
55-
fileName: () => `${config.header.name}.js`
56-
},
57-
rollupOptions: {
58-
output: {
59-
extend: true
60-
}
61-
}
62-
}
63-
}
50+
return userConfig(config)
6451
},
65-
async configResolved(cfg) {
66-
pluginConfig = cfg
67-
isBuildWatch = (cfg.build.watch ?? false) as boolean
68-
69-
const { name, match, require, include, exclude, resource, connect } =
70-
config.header
71-
72-
config.entry = resolve(cfg.root, config.entry)
73-
config.header.name = sanitize(name)
74-
config.header.match = removeDuplicates(match)
75-
config.header.require = removeDuplicates(require)
76-
config.header.include = removeDuplicates(include)
77-
config.header.exclude = removeDuplicates(exclude)
78-
config.header.resource = removeDuplicates(resource)
79-
config.header.connect = removeDuplicates(connect)
80-
config.autoGrants = config.autoGrants ?? true
52+
async configResolved(userConfig) {
53+
pluginConfig = userConfig
54+
isBuildWatch = (userConfig.build.watch ?? false) as boolean
55+
config.entry = resolve(userConfig.root, config.entry)
56+
config.header.name = sanitize(config.header.name)
57+
58+
Array.from([
59+
'match',
60+
'require',
61+
'include',
62+
'exclude',
63+
'resource',
64+
'connect'
65+
]).forEach((key) => {
66+
const value = config.header[key]
67+
if (Array.isArray(value)) {
68+
config.header[key] = removeDuplicates(value)
69+
}
70+
})
71+
8172
config.server = {
8273
port: await getPort(),
8374
open: true,
@@ -134,16 +125,6 @@ export default function UserscriptPlugin(
134125
try {
135126
let source = readFileSync(outPath, 'utf8')
136127

137-
// prettier-ignore
138-
config.header.grant = removeDuplicates(
139-
isBuildWatch
140-
? grants
141-
: config.autoGrants ?? true
142-
? defineGrants(source)
143-
: [...(config.header.grant ?? []), 'GM_addStyle', 'GM_info']
144-
)
145-
// prettier-ignore-end
146-
147128
if (isBuildWatch) {
148129
const hotReloadFile = readFileSync(
149130
resolve(
@@ -180,6 +161,12 @@ export default function UserscriptPlugin(
180161
loader: 'js'
181162
})
182163

164+
config.header.grant = removeDuplicates(
165+
isBuildWatch
166+
? grants
167+
: [...defineGrants(source), ...(config.header.grant ?? [])]
168+
)
169+
183170
writeFileSync(outPath, source)
184171
writeFileSync(userFilePath, `${banner(config.header)}\n\n${source}`)
185172
} catch (err) {

src/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,4 @@ export interface UserscriptPluginConfig {
245245
* Server config.
246246
*/
247247
server?: ServerConfig
248-
249-
/**
250-
* Import all `@grant` in development mode.
251-
* @default true
252-
*/
253-
autoGrants?: boolean
254248
}

vitest.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineConfig } from 'vitest/config'
22

33
export default defineConfig({
4-
test: {}
4+
test: {
5+
watch: false
6+
}
57
})

0 commit comments

Comments
 (0)