@@ -23,16 +23,25 @@ const fileDependencies = new Set()
2323module . exports = class WebpackUserscript {
2424 /**
2525 * @typedef WebpackUserscriptOptions
26- * @property {object } headers the header object
26+ * @property {object|string|((data: object) => object) } headers the header object
2727 * @property {boolean } metajs to generate *.meta.js
2828 * @property {boolean } pretty to prettify the header block
2929 * @property {boolean } renameExt to rename *.js files that are not *.user.js to become *.user.js
30+ * @property {object } hotDev Use \"@require\" keyword in the meta block to include the script
3031 */ /**
31- * @param {WebpackUserscriptOptions } [options]
32+ * @param {WebpackUserscriptOptions|string|((data: object) => object) } [options]
3233 */
3334 constructor ( options = { } ) {
3435 validateOptions ( optionsSchema , options , PLUGIN_NAME )
3536
37+ options . hotDev = Object . assign ( {
38+ baseUrl : 'http://localhost:8080/' ,
39+ filename : '[basename]-dev.user.js' ,
40+ enable : process . env . WEBPACK_DEV_SERVER === 'true'
41+ } , options . hotDev )
42+ options . hotDev . enable = typeof options . hotDev . enable === 'function'
43+ ? options . hotDev . enable ( ) : options . hotDev . enable
44+
3645 this . options = Object . assign (
3746 { } ,
3847 DEFAULT_CONFIG ,
@@ -98,13 +107,8 @@ module.exports = class WebpackUserscript {
98107 ...packageInfoObj
99108 }
100109
101- const headerString = userscriptMeta . stringify (
102- interpolate (
103- headerProvider ( data ) ,
104- data
105- ) ,
106- this . options . pretty
107- )
110+ const headerObj = interpolate ( headerProvider ( data ) , data )
111+ const headerString = userscriptMeta . stringify ( headerObj , this . options . pretty )
108112
109113 const outputFile = this . options . renameExt && ! file . endsWith ( '.user.js' )
110114 ? file . replace ( / \. j s $ / , '' ) + '.user.js'
@@ -122,12 +126,24 @@ module.exports = class WebpackUserscript {
122126 fileSource
123127 )
124128
129+ let hotDevHeaderString = ''
130+ if ( this . options . hotDev . enable ) {
131+ const hotDevBaseUrl = interpolate ( this . options . hotDev . baseUrl , data )
132+ const hotDevFilename = interpolate ( this . options . hotDev . filename , data )
133+ hotDevHeaderString = userscriptMeta . stringify ( {
134+ ...headerObj ,
135+ require : `${ hotDevBaseUrl . replace ( / \/ $ / , '' ) } /${ outputFile } `
136+ } , this . options . pretty )
137+ compilation . assets [ hotDevFilename ] = new RawSource ( hotDevHeaderString )
138+ }
139+
125140 if ( this . options . metajs ) {
126141 const basename = file . endsWith ( '.user.js' ) ? path . basename ( file , '.user.js' )
127142 : file . endsWith ( '.js' ) ? path . basename ( file , '.js' )
128143 : file
129144 const metaFile = basename + '.meta.js'
130- compilation . assets [ metaFile ] = new RawSource ( headerString )
145+ compilation . assets [ metaFile ] = new RawSource (
146+ this . options . hotDev . enable ? hotDevHeaderString : headerString )
131147 }
132148 }
133149 }
0 commit comments