This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGruntfile.js
236 lines (221 loc) · 7.15 KB
/
Gruntfile.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* eslint-env node */
/* eslint-disable camelcase */
const path = require('path');
const fs = require('fs');
let WEB_EXT_API_SECRET = null;
let WEB_EXT_API_KEY = null;
if (fs.existsSync('./.amo.json')) {
const amoConfig = require('./.amo.json');
WEB_EXT_API_KEY = amoConfig.apiKey;
WEB_EXT_API_SECRET = amoConfig.apiSecret;
} else if (process.env.WEB_EXT_API_SECRET && process.env.WEB_EXT_API_KEY) {
WEB_EXT_API_SECRET = process.env.WEB_EXT_API_SECRET;
WEB_EXT_API_KEY = process.env.WEB_EXT_API_KEY;
}
const isCi = process.env.CI === 'true';
function generateWebpackConfig({ name, dir }) {
return {
entry: `${dir}/${name}`,
mode: 'development',
output: {
filename: `${name}.js`,
path: path.resolve(__dirname, 'build', 'common', dir),
},
resolve: {
modules: [
path.resolve(__dirname, 'src'),
'node_modules',
],
},
devtool: 'inline-source-map',
};
}
function getGeneratedFilesMap() {
const result = new Map([
[ 'options', ['main']],
[ 'background', ['main']],
]);
for (const dir of [ 'connectors', 'inject' ]) {
result.set(dir, []);
for (const entry of fs.readdirSync(path.resolve('src', dir), { withFileTypes: true })) {
if (!entry.isFile() || !entry.name.endsWith('.js')) {
continue;
}
result.get(dir).push(entry.name.slice(0, -3));
}
}
return result;
}
function generateWebpackConfigs() {
const generatedFilesMap = getGeneratedFilesMap();
const webpackConfigs = {};
for (const [ dir, files ] of generatedFilesMap.entries()) {
for (const name of files) {
webpackConfigs[`${dir}-${name}`] = generateWebpackConfig({ dir, name });
}
}
return webpackConfigs;
}
const supportedBrowsers = [ 'chrome', 'firefox' ];
function logBrowserNotSupported(grunt, browser) {
grunt.fail.fatal(`You browser '${browser}' is currently not supported for packaging extension. We only support 'chrome' and 'firefox'`);
}
function generateOptionsView() {
const connectors = require('./src/background/connectors');
const simpleOptions = Object.keys(require('./src/common/defaults')).map((optionName) => ({ optionName }));
return { simpleOptions, connectors };
}
module.exports = (grunt) => {
require('load-grunt-tasks')(grunt);
const resources = [ '_locales/**', 'options/match-input.mustache', '**/*.css', 'manifest.json' ];
const webpackConfigs = generateWebpackConfigs();
const optionsView = generateOptionsView();
// Supported browser argument values for all tasks that take it are 'chrome' and 'firefox'
grunt.registerTask('build', 'Build extension directory for a browser', (browser) => {
if (!supportedBrowsers.includes(browser)) {
logBrowserNotSupported(grunt, browser);
return;
}
grunt.task.run(`clean:common`,
`clean:${browser}`,
`copy:resources`,
`copy:icons`,
`copy:optionsCSS`,
`copy:optionsFonts`,
`mustache_render:options_page`,
`webpack`,
`copy:${browser}`,
`replace_json:${browser}`,
);
});
grunt.registerTask('pack', 'Pack signed extension for a browser', (browser) => {
grunt.task.run(`build:${browser}`, `webext_builder:${browser}`);
});
grunt.initConfig({
webpack: webpackConfigs,
webext_builder: {
chrome: {
privateKey: './.cws.private.pem',
targets: ['chrome-crx'],
files: {
'dist/': ['build/chrome'],
},
},
firefox: {
jwtIssuer: WEB_EXT_API_KEY,
jwtSecret: WEB_EXT_API_SECRET,
targets: ['firefox-xpi'],
files: {
'dist/': ['build/firefox'],
},
},
},
clean: {
firefox: 'build/firefox',
chrome: 'build/chrome',
common: 'build/common',
build: 'build',
},
copy: {
resources: {
expand: true,
cwd: 'src/',
src: resources,
dest: 'build/common/',
},
icons: {
expand: true,
src: 'icons/**',
dest: 'build/common',
},
firefox: {
expand: true,
cwd: 'build/common/',
src: '**',
dest: 'build/firefox/',
},
chrome: {
expand: true,
cwd: 'build/common/',
src: '**',
dest: 'build/chrome/',
},
optionsCSS: {
expand: true,
cwd: 'node_modules/',
src: [ 'bootstrap/dist/css/bootstrap.min.css', 'font-awesome/css/font-awesome.min.css' ],
dest: 'build/common/options/css',
flatten: true,
},
optionsFonts: {
expand: true,
cwd: 'node_modules/',
src: 'font-awesome/fonts/**',
dest: 'build/common/options/fonts',
flatten: true,
},
},
watch: {
firefox: {
files: 'src/**',
tasks: ['build:firefox'],
options: {
atBegin: true,
},
},
chrome: {
files: 'src/**',
tasks: ['build:chrome'],
options: {
atBegin: true,
},
},
},
bump: {
options: {
files: ['src/manifest.json'],
commit: true,
commitMessage: 'Version v%VERSION%',
commitFiles: ['src/manifest.json'],
createTag: true,
tagName: 'v%VERSION%',
tagMessage: 'Version v%VERSION%',
push: true,
pushTo: 'origin',
},
},
replace_json: {
firefox: {
src: 'build/firefox/manifest.json',
changes: grunt.file.readJSON('src/firefox_manifest.json'),
},
chrome: {
src: 'build/chrome/manifest.json',
changes: grunt.file.readJSON('src/chrome_manifest.json'),
},
},
mustache_render: {
options_page: {
files: [
{
data: optionsView,
template: 'src/options/options.mustache',
dest: 'build/common/options/options.html',
},
],
},
},
eslint: {
options: {
configFile: '.eslintrc.yaml',
},
target: 'src/**/*.js',
fix: {
src: [ 'src/**/*.js', '*.js' ],
options: {
fix: !isCi,
},
},
},
});
};