Skip to content

Commit 7ff63db

Browse files
committed
chore: rename metadata to header
1 parent 4f605e1 commit 7ff63db

File tree

6 files changed

+57
-47
lines changed

6 files changed

+57
-47
lines changed

README.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default defineConfig({
3939
plugins: [
4040
Userscript({
4141
entry: 'src/index.ts',
42-
metadata: {
42+
header: {
4343
name,
4444
version,
4545
match: [
@@ -67,22 +67,35 @@ export default defineConfig({
6767
## Plugin Configuration
6868

6969
```ts
70-
export interface UserscriptPluginConfig {
71-
/**
72-
* Path of userscript entry.
73-
*/
74-
entry: string
75-
76-
/**
77-
* Userscript header config.
78-
*/
79-
metadata: MetadataConfig
80-
81-
/**
82-
* Import all `@grant` in development mode.
83-
* @default true
84-
*/
85-
autoGrants?: boolean
70+
interface ServerConfig {
71+
/**
72+
* {@link https://github.com/sindresorhus/get-port}
73+
*/
74+
port?: number;
75+
/**
76+
* @default true
77+
*/
78+
open?: boolean;
79+
}
80+
81+
interface UserscriptPluginConfig {
82+
/**
83+
* Path of userscript entry.
84+
*/
85+
entry: string;
86+
/**
87+
* Userscript header config.
88+
*/
89+
header: HeaderConfig;
90+
/**
91+
* Server config.
92+
*/
93+
server?: ServerConfig;
94+
/**
95+
* Import all `@grant` in development mode.
96+
* @default true
97+
*/
98+
autoGrants?: boolean;
8699
}
87100
```
88101

examples/basic/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineConfig({
66
plugins: [
77
Userscript({
88
entry: 'src/index.ts',
9-
metadata: {
9+
header: {
1010
name,
1111
version,
1212
match: [

examples/jsx/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineConfig({
88
Redom(),
99
Userscript({
1010
entry: 'src/index.tsx',
11-
metadata: {
11+
header: {
1212
name,
1313
version,
1414
match: 'https://example.com'

src/banner.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { MetadataConfig } from './types.js'
1+
import type { HeaderConfig } from './types.js'
22

3-
export function banner(config: MetadataConfig): string {
4-
const metadata: string[] = []
3+
export function banner(config: HeaderConfig): string {
4+
const header: string[] = []
55
const configKeys = Object.keys(config)
66
const maxKeyLength = Math.max(...configKeys.map((key) => key.length)) + 1
77

@@ -13,7 +13,7 @@ export function banner(config: MetadataConfig): string {
1313
const isBoolean = typeof value === 'boolean'
1414
if (isBoolean && !value) return
1515
value = !isBoolean ? addSpaces(key) + value.toString() : ''
16-
metadata.push(`// @${key}${value}`)
16+
header.push(`// @${key}${value}`)
1717
}
1818

1919
for (const [key, value] of Object.entries(config)) {
@@ -27,7 +27,7 @@ export function banner(config: MetadataConfig): string {
2727

2828
return [
2929
'// ==UserScript==',
30-
...metadata,
30+
...header,
3131
'// ==/UserScript=='
3232
].join('\n')
3333
}

src/index.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export default function UserscriptPlugin(
5050
build: {
5151
lib: {
5252
entry: config.entry,
53-
name: config.metadata.name,
53+
name: config.header.name,
5454
formats: ['iife'],
55-
fileName: () => `${config.metadata.name}.js`
55+
fileName: () => `${config.header.name}.js`
5656
},
5757
rollupOptions: {
5858
output: {
@@ -67,16 +67,16 @@ export default function UserscriptPlugin(
6767
isBuildWatch = (cfg.build.watch ?? false) as boolean
6868

6969
const { name, match, require, include, exclude, resource, connect } =
70-
config.metadata
70+
config.header
7171

7272
config.entry = resolve(cfg.root, config.entry)
73-
config.metadata.name = sanitize(name)
74-
config.metadata.match = removeDuplicates(match)
75-
config.metadata.require = removeDuplicates(require)
76-
config.metadata.include = removeDuplicates(include)
77-
config.metadata.exclude = removeDuplicates(exclude)
78-
config.metadata.resource = removeDuplicates(resource)
79-
config.metadata.connect = removeDuplicates(connect)
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)
8080
config.autoGrants = config.autoGrants ?? true
8181
config.server = {
8282
port: await getPort(),
@@ -115,32 +115,32 @@ export default function UserscriptPlugin(
115115
},
116116
async writeBundle(_, bundle) {
117117
const { open, port } = config.server!
118-
const proxyFilename = `${config.metadata.name}.proxy.user.js`
118+
const proxyFilename = `${config.header.name}.proxy.user.js`
119119

120120
for (const [fileName] of Object.entries(bundle)) {
121121
if (regexpScripts.test(fileName)) {
122122
const rootDir = pluginConfig.root
123123
const outDir = pluginConfig.build.outDir
124-
const userFilename = `${config.metadata.name}.user.js`
124+
const userFilename = `${config.header.name}.user.js`
125125

126126
const outPath = resolve(rootDir, outDir, fileName)
127127
const proxyFilePath = resolve(rootDir, outDir, proxyFilename)
128128
const userFilePath = resolve(rootDir, outDir, userFilename)
129129
const hotReloadPath = resolve(
130130
dirname(fileURLToPath(import.meta.url)),
131-
`hot-reload-${config.metadata.name}.js`
131+
`hot-reload-${config.header.name}.js`
132132
)
133133

134134
try {
135135
let source = readFileSync(outPath, 'utf8')
136136

137137
// prettier-ignore
138-
config.metadata.grant = removeDuplicates(
138+
config.header.grant = removeDuplicates(
139139
isBuildWatch
140140
? grants
141141
: config.autoGrants ?? true
142142
? defineGrants(source)
143-
: [...(config.metadata.grant ?? []), 'GM_addStyle', 'GM_info']
143+
: [...(config.header.grant ?? []), 'GM_addStyle', 'GM_info']
144144
)
145145
// prettier-ignore-end
146146

@@ -163,9 +163,9 @@ export default function UserscriptPlugin(
163163
writeFileSync(
164164
proxyFilePath,
165165
banner({
166-
...config.metadata,
166+
...config.header,
167167
require: [
168-
...config.metadata.require!,
168+
...config.header.require!,
169169
'file://' + hotReloadPath,
170170
'file://' + outPath
171171
]
@@ -181,10 +181,7 @@ export default function UserscriptPlugin(
181181
})
182182

183183
writeFileSync(outPath, source)
184-
writeFileSync(
185-
userFilePath,
186-
`${banner(config.metadata)}\n\n${source}`
187-
)
184+
writeFileSync(userFilePath, `${banner(config.header)}\n\n${source}`)
188185
} catch (err) {
189186
console.log(err)
190187
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type RunAt =
1515

1616
export type Grants = typeof grants[number]
1717

18-
export type MetadataConfig = {
18+
export type HeaderConfig = {
1919
[property: string]: string | boolean | number | string[] | undefined
2020

2121
/**
@@ -239,7 +239,7 @@ export interface UserscriptPluginConfig {
239239
/**
240240
* Userscript header config.
241241
*/
242-
metadata: MetadataConfig
242+
header: HeaderConfig
243243

244244
/**
245245
* Server config.

0 commit comments

Comments
 (0)