-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.js
34 lines (29 loc) · 1.04 KB
/
mod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const bookmarkletPlugin = {
name: 'bookmarklet',
setup(build) {
const options = build.initialOptions
if(options.write == true) {
throw new Error("`write` must be set to false for this plugin to work correctly.")
}
if(options.format != "iife") {
throw new Error("`format` must be set to iife for this plugin to work correctly.")
}
if(options.minify != true) {
throw new Error("`minify` must be set to true for your bookmarklet to work as expected.")
}
build.onEnd(async (result) => {
if(result.outputFiles == null) {
throw new Error("Unable to access outputFiles. This is likely due to `write` being set to true.")
}
const encoder = new TextEncoder()
const js = result.outputFiles.find(f => f.path.match(/\.js$/))
const modified = encodeURI("javascript:void " + js.text)
js.contents = encoder.encode(modified)
await Deno.writeTextFile(js.path, js.text)
})
build.onDispose(() => {
Deno.exit(0)
})
},
}
export default bookmarkletPlugin;