@atlaspack/transformer-webextension 2.14.5-canary.35 → 2.14.5-canary.351
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +325 -0
- package/dist/WebExtensionTransformer.js +412 -0
- package/dist/schema.js +560 -0
- package/lib/types/WebExtensionTransformer.d.ts +3 -0
- package/lib/types/schema.d.ts +4 -0
- package/package.json +12 -8
- package/src/{WebExtensionTransformer.js → WebExtensionTransformer.ts} +21 -17
- package/src/{schema.js → schema.ts} +21 -22
- package/tsconfig.json +18 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const plugin_1 = require("@atlaspack/plugin");
|
|
40
|
+
const path_1 = __importDefault(require("path"));
|
|
41
|
+
const json_sourcemap_1 = require("@mischnic/json-sourcemap");
|
|
42
|
+
const content_security_policy_parser_1 = __importDefault(require("content-security-policy-parser"));
|
|
43
|
+
const utils_1 = require("@atlaspack/utils");
|
|
44
|
+
const diagnostic_1 = __importStar(require("@atlaspack/diagnostic"));
|
|
45
|
+
const utils_2 = require("@atlaspack/utils");
|
|
46
|
+
const schema_1 = require("./schema");
|
|
47
|
+
const DEP_LOCS = [
|
|
48
|
+
['icons'],
|
|
49
|
+
['browser_action', 'default_icon'],
|
|
50
|
+
['browser_action', 'default_popup'],
|
|
51
|
+
['page_action', 'default_icon'],
|
|
52
|
+
['page_action', 'default_popup'],
|
|
53
|
+
['action', 'default_icon'],
|
|
54
|
+
['action', 'default_popup'],
|
|
55
|
+
['background', 'scripts'],
|
|
56
|
+
['chrome_url_overrides'],
|
|
57
|
+
['devtools_page'],
|
|
58
|
+
['options_ui', 'page'],
|
|
59
|
+
['sandbox', 'pages'],
|
|
60
|
+
['side_panel', 'default_path'],
|
|
61
|
+
['sidebar_action', 'default_icon'],
|
|
62
|
+
['sidebar_action', 'default_panel'],
|
|
63
|
+
['storage', 'managed_schema'],
|
|
64
|
+
['theme', 'images', 'theme_frame'],
|
|
65
|
+
['theme', 'images', 'additional_backgrounds'],
|
|
66
|
+
['user_scripts', 'api_script'],
|
|
67
|
+
];
|
|
68
|
+
async function collectDependencies(asset, program, ptrs, hmrOptions) {
|
|
69
|
+
const hot = Boolean(hmrOptions);
|
|
70
|
+
const fs = asset.fs;
|
|
71
|
+
const filePath = asset.filePath;
|
|
72
|
+
const assetDir = path_1.default.dirname(filePath);
|
|
73
|
+
const isMV2 = program.manifest_version == 2;
|
|
74
|
+
delete program.$schema;
|
|
75
|
+
if (program.default_locale) {
|
|
76
|
+
const locales = path_1.default.join(assetDir, '_locales');
|
|
77
|
+
let err = !(await fs.exists(locales))
|
|
78
|
+
? 'key'
|
|
79
|
+
: !(await fs.exists(path_1.default.join(locales, program.default_locale, 'messages.json')))
|
|
80
|
+
? 'value'
|
|
81
|
+
: null;
|
|
82
|
+
if (err) {
|
|
83
|
+
throw new diagnostic_1.default({
|
|
84
|
+
diagnostic: [
|
|
85
|
+
{
|
|
86
|
+
message: 'Invalid Web Extension manifest',
|
|
87
|
+
origin: '@atlaspack/transformer-webextension',
|
|
88
|
+
codeFrames: [
|
|
89
|
+
{
|
|
90
|
+
filePath,
|
|
91
|
+
codeHighlights: [
|
|
92
|
+
{
|
|
93
|
+
...(0, diagnostic_1.getJSONHighlightLocation)(ptrs['/default_locale'], err),
|
|
94
|
+
message: (0, diagnostic_1.md) `Localization ${err == 'value'
|
|
95
|
+
? 'file for ' + program.default_locale
|
|
96
|
+
: 'directory'} does not exist: ${path_1.default.relative(assetDir, path_1.default.join(locales, program.default_locale))}`,
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
for (const locale of await fs.readdir(locales)) {
|
|
106
|
+
if (await fs.exists(path_1.default.join(locales, locale, 'messages.json'))) {
|
|
107
|
+
asset.addURLDependency(`_locales/${locale}/messages.json`, {
|
|
108
|
+
needsStableName: true,
|
|
109
|
+
pipeline: 'raw',
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (program.content_scripts) {
|
|
115
|
+
for (let i = 0; i < program.content_scripts.length; ++i) {
|
|
116
|
+
const sc = program.content_scripts[i];
|
|
117
|
+
for (const k of ['css', 'js']) {
|
|
118
|
+
const assets = sc[k] || [];
|
|
119
|
+
for (let j = 0; j < assets.length; ++j) {
|
|
120
|
+
assets[j] = asset.addURLDependency(assets[j], {
|
|
121
|
+
bundleBehavior: 'isolated',
|
|
122
|
+
loc: {
|
|
123
|
+
filePath,
|
|
124
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs[`/content_scripts/${i}/${k}/${j}`], 'value'),
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (hot && sc.js && sc.js.length) {
|
|
130
|
+
sc.js.push(asset.addURLDependency('./runtime/autoreload.js', {
|
|
131
|
+
resolveFrom: __filename,
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (program.dictionaries) {
|
|
137
|
+
for (const dict in program.dictionaries) {
|
|
138
|
+
const dictFile = program.dictionaries[dict];
|
|
139
|
+
if (path_1.default.extname(dictFile) != '.dic') {
|
|
140
|
+
throw new diagnostic_1.default({
|
|
141
|
+
diagnostic: [
|
|
142
|
+
{
|
|
143
|
+
message: 'Invalid Web Extension manifest',
|
|
144
|
+
origin: '@atlaspack/transformer-webextension',
|
|
145
|
+
codeFrames: [
|
|
146
|
+
{
|
|
147
|
+
filePath,
|
|
148
|
+
codeHighlights: [
|
|
149
|
+
{
|
|
150
|
+
...(0, diagnostic_1.getJSONHighlightLocation)(ptrs[`/dictionaries/${dict}`], 'value'),
|
|
151
|
+
message: 'Dictionaries must be .dic files',
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
const loc = {
|
|
161
|
+
filePath,
|
|
162
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs[`/dictionaries/${dict}`], 'value'),
|
|
163
|
+
};
|
|
164
|
+
program.dictionaries[dict] = asset.addURLDependency(dictFile, {
|
|
165
|
+
needsStableName: true,
|
|
166
|
+
loc,
|
|
167
|
+
});
|
|
168
|
+
asset.addURLDependency(dictFile.slice(0, -4) + '.aff', {
|
|
169
|
+
needsStableName: true,
|
|
170
|
+
loc,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const browserActionName = isMV2 ? 'browser_action' : 'action';
|
|
175
|
+
if (program[browserActionName]?.theme_icons) {
|
|
176
|
+
for (let i = 0; i < program[browserActionName].theme_icons.length; ++i) {
|
|
177
|
+
const themeIcon = program[browserActionName].theme_icons[i];
|
|
178
|
+
for (const k of ['light', 'dark']) {
|
|
179
|
+
const loc = (0, diagnostic_1.getJSONSourceLocation)(ptrs[`/${browserActionName}/theme_icons/${i}/${k}`], 'value');
|
|
180
|
+
themeIcon[k] = asset.addURLDependency(themeIcon[k], {
|
|
181
|
+
loc: {
|
|
182
|
+
...loc,
|
|
183
|
+
filePath,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (program.web_accessible_resources) {
|
|
190
|
+
let war = [];
|
|
191
|
+
for (let i = 0; i < program.web_accessible_resources.length; ++i) {
|
|
192
|
+
// TODO: this doesn't support Atlaspack resolution
|
|
193
|
+
const currentEntry = program.web_accessible_resources[i];
|
|
194
|
+
const files = isMV2 ? [currentEntry] : currentEntry.resources;
|
|
195
|
+
let currentFiles = [];
|
|
196
|
+
for (let j = 0; j < files.length; ++j) {
|
|
197
|
+
const globFiles = (await (0, utils_2.glob)(path_1.default.join(assetDir, files[j]), fs, {})).map((fp) => asset.addURLDependency(path_1.default.relative(assetDir, fp), {
|
|
198
|
+
bundleBehavior: 'isolated',
|
|
199
|
+
needsStableName: true,
|
|
200
|
+
loc: {
|
|
201
|
+
filePath,
|
|
202
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs[`/web_accessible_resources/${i}${isMV2 ? '' : `/resources/${j}`}`]),
|
|
203
|
+
},
|
|
204
|
+
}));
|
|
205
|
+
currentFiles = currentFiles.concat(globFiles);
|
|
206
|
+
}
|
|
207
|
+
if (isMV2) {
|
|
208
|
+
war = war.concat(currentFiles);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
currentEntry.resources = currentFiles;
|
|
212
|
+
war.push(currentEntry);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
program.web_accessible_resources = war;
|
|
216
|
+
}
|
|
217
|
+
if (program.declarative_net_request) {
|
|
218
|
+
const rrs = program.declarative_net_request?.rule_resources ?? [];
|
|
219
|
+
rrs.forEach((resources, i) => {
|
|
220
|
+
resources.path = asset.addURLDependency(resources.path, {
|
|
221
|
+
pipeline: 'raw',
|
|
222
|
+
loc: {
|
|
223
|
+
filePath,
|
|
224
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs[`/declarative_net_request/rule_resources/${i}/path`], 'value'),
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
for (const loc of DEP_LOCS) {
|
|
230
|
+
const location = '/' + loc.join('/');
|
|
231
|
+
if (!ptrs[location])
|
|
232
|
+
continue;
|
|
233
|
+
let parent = program;
|
|
234
|
+
for (let i = 0; i < loc.length - 1; ++i) {
|
|
235
|
+
parent = parent[loc[i]];
|
|
236
|
+
}
|
|
237
|
+
const lastLoc = loc[loc.length - 1];
|
|
238
|
+
const obj = parent[lastLoc];
|
|
239
|
+
if (typeof obj == 'string')
|
|
240
|
+
parent[lastLoc] = asset.addURLDependency(obj, {
|
|
241
|
+
bundleBehavior: 'isolated',
|
|
242
|
+
loc: {
|
|
243
|
+
filePath,
|
|
244
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs[location], 'value'),
|
|
245
|
+
},
|
|
246
|
+
pipeline: path_1.default.extname(obj) == '.json' ? 'raw' : undefined,
|
|
247
|
+
});
|
|
248
|
+
else {
|
|
249
|
+
for (const k of Object.keys(obj)) {
|
|
250
|
+
obj[k] = asset.addURLDependency(obj[k], {
|
|
251
|
+
bundleBehavior: 'isolated',
|
|
252
|
+
loc: {
|
|
253
|
+
filePath,
|
|
254
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs[location + '/' + k], 'value'),
|
|
255
|
+
},
|
|
256
|
+
pipeline: path_1.default.extname(obj[k]) == '.json' ? 'raw' : undefined,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (program.background?.page) {
|
|
262
|
+
program.background.page = asset.addURLDependency(program.background.page, {
|
|
263
|
+
bundleBehavior: 'isolated',
|
|
264
|
+
loc: {
|
|
265
|
+
filePath,
|
|
266
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs['/background/page'], 'value'),
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
if (program.background?.service_worker) {
|
|
271
|
+
program.background.service_worker = asset.addURLDependency(program.background.service_worker, {
|
|
272
|
+
bundleBehavior: 'isolated',
|
|
273
|
+
loc: {
|
|
274
|
+
filePath,
|
|
275
|
+
...(0, diagnostic_1.getJSONSourceLocation)(ptrs['/background/service_worker'], 'value'),
|
|
276
|
+
},
|
|
277
|
+
env: {
|
|
278
|
+
context: 'service-worker',
|
|
279
|
+
sourceType: program.background.type == 'module' ? 'module' : 'script',
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
if (hot) {
|
|
284
|
+
if (isMV2) {
|
|
285
|
+
// To enable HMR, we must override the CSP to allow 'unsafe-eval'
|
|
286
|
+
program.content_security_policy = cspPatchHMR(program.content_security_policy);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
// Enable HMR for fetched localhost chunks
|
|
290
|
+
const csp = program.content_security_policy || {};
|
|
291
|
+
csp.extension_pages = cspPatchHMR(csp.extension_pages, `http://${hmrOptions?.host || 'localhost'}:*`);
|
|
292
|
+
// Sandbox allows eval by default
|
|
293
|
+
if (csp.sandbox)
|
|
294
|
+
csp.sandbox = cspPatchHMR(csp.sandbox);
|
|
295
|
+
program.content_security_policy = csp;
|
|
296
|
+
}
|
|
297
|
+
if (!program.background) {
|
|
298
|
+
program.background = {};
|
|
299
|
+
}
|
|
300
|
+
if (program.background.page) {
|
|
301
|
+
asset.meta.webextBGInsert = program.background.page;
|
|
302
|
+
}
|
|
303
|
+
else if (isMV2 || program.background.scripts) {
|
|
304
|
+
if (!program.background.scripts) {
|
|
305
|
+
program.background.scripts = [];
|
|
306
|
+
}
|
|
307
|
+
if (program.background.scripts.length == 0) {
|
|
308
|
+
program.background.scripts.push(asset.addURLDependency('./runtime/default-bg.js', {
|
|
309
|
+
resolveFrom: __filename,
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
asset.meta.webextBGInsert = program.background.scripts[0];
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
if (!program.background.service_worker) {
|
|
316
|
+
program.background.service_worker = asset.addURLDependency('./runtime/default-bg.js', {
|
|
317
|
+
resolveFrom: __filename,
|
|
318
|
+
env: { context: 'service-worker' },
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
asset.meta.webextBGInsert = program.background.service_worker;
|
|
322
|
+
}
|
|
323
|
+
if (!program.permissions)
|
|
324
|
+
program.permissions = [];
|
|
325
|
+
if (!isMV2 && !program.permissions.includes('scripting')) {
|
|
326
|
+
program.permissions.push('scripting');
|
|
327
|
+
}
|
|
328
|
+
const hostPerms = [
|
|
329
|
+
...new Set(program.content_scripts?.flatMap((sc) => sc.matches)),
|
|
330
|
+
];
|
|
331
|
+
if (isMV2)
|
|
332
|
+
program.permissions = program.permissions.concat(hostPerms);
|
|
333
|
+
else {
|
|
334
|
+
if (!program.host_permissions)
|
|
335
|
+
program.host_permissions = [];
|
|
336
|
+
program.host_permissions = program.host_permissions.concat(hostPerms);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function cspPatchHMR(policy, insert) {
|
|
341
|
+
let defaultSrc = "'self'";
|
|
342
|
+
if (insert == null) {
|
|
343
|
+
insert = "'unsafe-eval'";
|
|
344
|
+
defaultSrc = "'self' blob: filesystem:";
|
|
345
|
+
}
|
|
346
|
+
if (policy) {
|
|
347
|
+
const csp = (0, content_security_policy_parser_1.default)(policy);
|
|
348
|
+
policy = '';
|
|
349
|
+
if (!csp['script-src']) {
|
|
350
|
+
csp['script-src'] = [defaultSrc];
|
|
351
|
+
}
|
|
352
|
+
if (!csp['script-src'].includes(insert)) {
|
|
353
|
+
csp['script-src'].push(insert);
|
|
354
|
+
}
|
|
355
|
+
if (csp.sandbox && !csp.sandbox.includes('allow-scripts')) {
|
|
356
|
+
csp.sandbox.push('allow-scripts');
|
|
357
|
+
}
|
|
358
|
+
for (const k in csp) {
|
|
359
|
+
policy += `${k} ${csp[k].join(' ')};`;
|
|
360
|
+
}
|
|
361
|
+
return policy;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
return `script-src ${defaultSrc} ${insert};` + `object-src ${defaultSrc};`;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
exports.default = new plugin_1.Transformer({
|
|
368
|
+
async transform({ asset, options }) {
|
|
369
|
+
// Set environment to browser, since web extensions are always used in
|
|
370
|
+
// browsers, and because it avoids delegating extra config to the user
|
|
371
|
+
asset.setEnvironment({
|
|
372
|
+
context: 'browser',
|
|
373
|
+
outputFormat: asset.env.outputFormat == 'commonjs'
|
|
374
|
+
? 'global'
|
|
375
|
+
: asset.env.outputFormat,
|
|
376
|
+
engines: {
|
|
377
|
+
browsers: asset.env.engines.browsers,
|
|
378
|
+
},
|
|
379
|
+
sourceMap: asset.env.sourceMap && {
|
|
380
|
+
...asset.env.sourceMap,
|
|
381
|
+
// Inline source maps work most reliably on web extensions but allow users to overwrite
|
|
382
|
+
inline: asset.env.sourceMap.inline ?? true,
|
|
383
|
+
inlineSources: asset.env.sourceMap.inlineSources ?? true,
|
|
384
|
+
},
|
|
385
|
+
includeNodeModules: asset.env.includeNodeModules,
|
|
386
|
+
sourceType: asset.env.sourceType,
|
|
387
|
+
isLibrary: asset.env.isLibrary,
|
|
388
|
+
shouldOptimize: asset.env.shouldOptimize,
|
|
389
|
+
shouldScopeHoist: asset.env.shouldScopeHoist,
|
|
390
|
+
});
|
|
391
|
+
const code = await asset.getCode();
|
|
392
|
+
const parsed = (0, json_sourcemap_1.parse)(code);
|
|
393
|
+
const data = parsed.data;
|
|
394
|
+
// Not using a unified schema dramatically improves error messages
|
|
395
|
+
let schema = schema_1.VersionSchema;
|
|
396
|
+
if (data.manifest_version === 3) {
|
|
397
|
+
schema = schema_1.MV3Schema;
|
|
398
|
+
}
|
|
399
|
+
else if (data.manifest_version === 2) {
|
|
400
|
+
schema = schema_1.MV2Schema;
|
|
401
|
+
}
|
|
402
|
+
utils_1.validateSchema.diagnostic(schema, {
|
|
403
|
+
data: data,
|
|
404
|
+
source: code,
|
|
405
|
+
filePath: asset.filePath,
|
|
406
|
+
}, '@atlaspack/transformer-webextension', 'Invalid Web Extension manifest');
|
|
407
|
+
await collectDependencies(asset, data, parsed.pointers, options.hmrOptions);
|
|
408
|
+
asset.setCode(JSON.stringify(data, null, 2));
|
|
409
|
+
asset.meta.webextEntry = true;
|
|
410
|
+
return [asset];
|
|
411
|
+
},
|
|
412
|
+
});
|