This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
generated from mozilla-rally/study-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollup.config.js
151 lines (140 loc) · 4.64 KB
/
rollup.config.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
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
144
145
146
147
148
149
150
151
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This is the Rollup configuration for the study template. It is
// part of the build system, and you should not have to modify it.
import commonjs from "@rollup/plugin-commonjs";
import replace from "@rollup/plugin-replace";
import resolve from "@rollup/plugin-node-resolve";
import globby from "globby";
import typescript from '@rollup/plugin-typescript';
import webScienceRollupPlugin from "@mozilla/web-science/rollup-plugin";
import svelte from 'rollup-plugin-svelte';
import html from "@rollup/plugin-html"
import fs from "fs";
import copy from 'rollup-plugin-copy'
require("svelte/register");
function makeTemplate(templateData, svelteComponent, props, scriptSrc) {
return ({ attributes, bundle, files, publicPath, title }) => {
const body = svelteComponent.render(props).html;
return templateData.replace("%%BODY%%", body).replace("%%TITLE%%", title).replace("%%SCRIPT%%", scriptSrc);
};
}
const htmlPages = [];
htmlPages.push(
html({
fileName: `pages/lottery_popup.html`,
title: "Search Engine Study Lottery Winner",
template: makeTemplate(
fs.readFileSync("./src/pages/template.html", "utf8"),
require("./src/pages/components/lottery-popup/LotteryPopup.svelte").default,
null,
"assets/js/lotteryPopup.js"
),
})
);
const choiceBallotTypes = [ "Default", "HiddenDescription", "VisibleDescription", "Extended" ];
for(let index = 0; index < choiceBallotTypes.length; index++) {
const choiceBallotType = choiceBallotTypes[ index ];
htmlPages.push(
html({
fileName: `pages/choice_ballot_${index + 1}.html`,
title: "Search Engine Choice Ballot",
template: makeTemplate(
fs.readFileSync("./src/pages/template.html", "utf8"),
require("./src/pages/components/choice-ballot/ChoiceBallot.svelte").default,
{ choiceBallotType },
"assets/js/choiceBallot.js"
),
})
);
}
const revertOptions = [ false, true ];
for(let index = 0; index < revertOptions.length; index++) {
const revertOption = revertOptions[ index ];
htmlPages.push(
html({
fileName: `pages/notice_${index + 1}.html`,
title: "Search Engine Change Notice",
template: makeTemplate(
fs.readFileSync("./src/pages/template.html", "utf8"),
require("./src/pages/components/notice/Notice.svelte").default,
{ revertOption },
"assets/js/notice.js"
),
})
);
}
/**
* Helper to detect developer mode.
*
* @param cliArgs the command line arguments.
* @return {Boolean} whether or not developer mode is enabled.
*/
function isDevMode(cliArgs) {
return Boolean(cliArgs[ "config-enable-developer-mode" ]);
}
export default (cliArgs) => {
// Configuration for the main background script, src/background.js.
// The script will be output to dist/background.js with any module
// dependencies (your own modules or modules from NPM) bundled in.
const rollupConfig = [
{
input: "src/background.ts",
output: {
file: "dist/background.js",
sourcemap: isDevMode(cliArgs) ? "inline" : false,
},
plugins: [
replace({
preventAssignment: true,
// In Developer Mode, the study does not submit data and
// gracefully handles communication errors with the Core
// Add-on.
__ENABLE_DEVELOPER_MODE__: isDevMode(cliArgs),
}),
resolve({
browser: true,
}),
commonjs(),
typescript(),
webScienceRollupPlugin(),
svelte({
compilerOptions: {
hydratable: true,
}
}),
...htmlPages,
copy({
targets: [
{ src: 'src/pages/assets/*', dest: 'dist/pages/assets' },
]
})
],
}
];
const pageScriptPaths = globby.sync([ `src/page-scripts/choiceBallot.ts`, `src/page-scripts/notice.ts`, `src/page-scripts/lotteryPopup.ts` ]);
for(const pageScriptPath of pageScriptPaths) {
rollupConfig.push({
input: pageScriptPath,
output: {
file: `dist/pages/assets/js/${pageScriptPath.slice("src/page-scripts/".length, -3)}.js`,
format: "iife",
sourcemap: isDevMode(cliArgs) ? "inline" : false,
},
plugins: [
replace({
preventAssignment: true,
__ENABLE_DEVELOPER_MODE__: isDevMode(cliArgs),
}),
webScienceRollupPlugin(),
resolve({
browser: true,
}),
typescript(),
commonjs(),
],
});
}
return rollupConfig;
}