@@ -11,6 +11,7 @@ import { PluginOption, ResolvedConfig, createLogger } from 'vite'
1111import { server } from 'websocket'
1212import type { connection } from 'websocket'
1313import { banner } from './banner.js'
14+ import { userConfig } from './config.js'
1415import { grants , regexpScripts , regexpStyles } from './constants.js'
1516import css from './css.js'
1617import { 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 ) {
0 commit comments