-
Notifications
You must be signed in to change notification settings - Fork 14
/
rollup.config.mjs
119 lines (113 loc) · 3.01 KB
/
rollup.config.mjs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import dataurl from "dataurl";
import fs from "fs";
import usm from "userscript-meta-cli";
import glob from "tiny-glob";
import copy from 'rollup-plugin-copy-glob';
import cjs from "rollup-plugin-cjs-es";
import iife from "rollup-plugin-iife";
import json from "@rollup/plugin-json";
import output from "rollup-plugin-write-output";
import terser from "@rollup/plugin-terser";
import resolve from "@rollup/plugin-node-resolve";
import inject from "@rollup/plugin-inject";
function commonPlugins(cache) {
return [
resolve(),
json(),
cjs({nested: true, cache})
];
}
export default async () => [
{
input: await glob("src/extension/*.js"),
output: {
format: "es",
dir: "dist-extension/js"
},
plugins: [
copy([
{
files: "src/static/**/*",
dest: "dist-extension"
}
]),
...commonPlugins(),
inject({
exclude: ["**/*/browser-polyfill.js"],
browser: "webextension-polyfill"
}),
iife(),
output([
{
test: /(options|dialog)\.js$/,
target: "dist-extension/$1.html",
handle: (content, {htmlScripts}) => content.replace("</body>", `${htmlScripts}</body>`)
},
{
test: /background\.js$/,
target: "dist-extension/manifest.json",
handle: (content, {scripts}) => {
content.background.scripts = scripts;
return content;
}
},
{
test: /content\.js$/,
target: "dist-extension/manifest.json",
handle: (content, {scripts}) => {
content.content_scripts[0].js = scripts;
content.content_scripts[0].exclude_globs = usm.getMeta().exclude;
return content;
}
}
]),
terser({module: false})
]
},
{
input: {
"linkify-plus-plus.user": "src/userscript/index.js"
},
output: {
format: "es",
banner: metaDataBlock(),
dir: "dist"
},
plugins: [
cleanMessages(),
...commonPlugins(false),
]
}
];
function metaDataBlock() {
const meta = usm.getMeta();
meta.icon = dataurl.format({
data: fs.readFileSync("src/static/icon.svg"),
mimetype: "image/svg+xml"
});
return usm.stringify(meta);
}
function cleanMessages() {
return {
name: "clean-messages",
transform: (code, id) => {
if (!/messages.json/.test(id)) return;
const message = JSON.parse(code);
const newMessage = {};
for (const [key, value] of Object.entries(message)) {
if (value.placeholders) {
value.message = value.message.replace(/\$\w+\$/g, m => {
const name = m.slice(1, -1).toLowerCase();
return value.placeholders[name].content;
});
}
if (/^options/.test(key)) {
newMessage[key] = value.message;
} else if (/^pref/.test(key)) {
newMessage[key[4].toLowerCase() + key.slice(5)] = value.message;
}
}
return JSON.stringify(newMessage, null, 2);
}
}
}