-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.config.firefox.mjs
143 lines (131 loc) · 5.16 KB
/
rollup.config.firefox.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2024 Chris Wheeler and Jonathan Chua
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// https://rollupjs.org/
import copy from "rollup-plugin-copy"; // https://www.npmjs.com/package/rollup-plugin-copy
import del from "rollup-plugin-delete"; // https://www.npmjs.com/package/rollup-plugin-delete
// This is a Rollup configuration file for building the Firefox extension. It first
// copies all the necessary files from `src` to the `firefox` directory, then replaces
// all imports with the code the imports correspond to. Lastly, it deletes all files in
// the `firefox` directory that were imported into other files there and are no longer
// needed.
//
// Each time this runs, any existing files with the same names as those copied are
// overwritten.
/**
* transform changes a js file to make it compatible with Firefox.
* @param {Buffer} contents - a file's contents.
* @param {string} filename - a file's name.
* @returns {any}
*/
function transform(contents, filename) {
if (!filename.endsWith(".js")) {
return contents;
}
return contents
.toString()
.replace(
// Remove all `browser` imports because firefox/browserSpecific.js doesn't
// define `browser` because Firefox already has a global `browser` variable.
/((?:^|\n)import\s*\{(?:\s*\w+,)*)\s*browser,?((?:\s*\w+,?)*\s*\}\s*from)/,
"$1$2",
)
.replace(
// Comment out `window.onload = setUpListeners` because although Chrome
// needs it, in Firefox it causes a "Clipboard write is not allowed" error
// on some sites despite not preventing clipboard writes.
/window\.onload = setUpListeners/,
"// window.onload = setUpListeners",
);
}
export default [
{
input: "firefox/background.js",
output: {
file: "firefox/background.js",
format: "iife", // immediately-invoked function expression
},
plugins: [
copy({
// Copy all files and folders to the firefox folder except the
// browserSpecific.js that is for testing. There are multiple targets
// because the tranform function must only be called on JavaScript
// files.
targets: [
{
// copy the images folder and html & css files
src: ["src/images", "src/**/*.html", "src/**/*.css"],
dest: "firefox",
},
{
// copy js files and transform them
src: ["src/**/*.js", "!src/browserSpecific.js"],
dest: "firefox",
transform: transform,
},
],
flatten: false, // don't combine all nested folders into one flat folder
hook: "buildStart", // run the copy before the build starts
}),
],
},
{
input: "firefox/content.js",
output: {
file: "firefox/content.js",
format: "iife", // immediately-invoked function expression
},
},
{
input: "firefox/popup.js",
output: {
file: "firefox/popup.js",
format: "iife", // immediately-invoked function expression
},
},
{
input: "firefox/sidebar.js",
output: {
file: "firefox/sidebar.js",
format: "iife", // immediately-invoked function expression
},
},
{
input: "firefox/settings.js",
output: {
file: "firefox/settings.js",
format: "iife", // immediately-invoked function expression
},
plugins: [
del({
// Delete all files in the `firefox` directory that were imported into
// other files there and are no longer needed.
targets: [
"firefox/*", // delete all files except the ones below
"!firefox/images",
"!firefox/manifest.json",
"!firefox/**/*.html",
"!firefox/**/*.css",
"!firefox/browserSpecific.js",
"!firefox/background.js",
"!firefox/content.js",
"!firefox/popup.js",
"!firefox/sidebar.js",
"!firefox/settings.js",
"!firefox/text-fragment-utils.js",
"!firefox/fragment-generation-utils.js",
],
hook: "buildEnd", // run the delete after the build ends
}),
],
},
];