Skip to content

Commit dbdd032

Browse files
committed
✨ Add a new option, hotDev.
1 parent 09a08c3 commit dbdd032

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

lib/index.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@ const fileDependencies = new Set()
2323
module.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(/\.js$/, '') + '.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
}

lib/schemas/header.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/schemas/options.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@
3636
"renameExt": {
3737
"description": "To rename *.js files that are not *.user.js to become *.user.js",
3838
"type": "boolean"
39+
},
40+
"hotDev": {
41+
"description": "Use \"@require\" keyword in the meta block to include the script",
42+
"type": "object",
43+
"properties": {
44+
"baseUrl": {
45+
"description": "The base URL of the dev server",
46+
"typeof": "string"
47+
},
48+
"filename": {
49+
"typeof": "string"
50+
},
51+
"enable": {
52+
"anyOf": [
53+
{ "typeof": "function" },
54+
{ "type": "boolean" }
55+
]
56+
}
57+
}
3958
}
4059
},
4160
"additionalProperties": false

0 commit comments

Comments
 (0)