@atlaspack/transformer-webextension 2.14.0 → 2.14.1-canary.6

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (c) 2024 Atlassian US., Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,371 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _plugin() {
8
+ const data = require("@atlaspack/plugin");
9
+ _plugin = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _path() {
15
+ const data = _interopRequireDefault(require("path"));
16
+ _path = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _jsonSourcemap() {
22
+ const data = require("@mischnic/json-sourcemap");
23
+ _jsonSourcemap = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _contentSecurityPolicyParser() {
29
+ const data = _interopRequireDefault(require("content-security-policy-parser"));
30
+ _contentSecurityPolicyParser = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _utils() {
36
+ const data = require("@atlaspack/utils");
37
+ _utils = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _diagnostic() {
43
+ const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
44
+ _diagnostic = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ var _schema = require("./schema");
50
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
51
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
52
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
53
+ const DEP_LOCS = [['icons'], ['browser_action', 'default_icon'], ['browser_action', 'default_popup'], ['page_action', 'default_icon'], ['page_action', 'default_popup'], ['action', 'default_icon'], ['action', 'default_popup'], ['background', 'scripts'], ['chrome_url_overrides'], ['devtools_page'], ['options_ui', 'page'], ['sandbox', 'pages'], ['side_panel', 'default_path'], ['sidebar_action', 'default_icon'], ['sidebar_action', 'default_panel'], ['storage', 'managed_schema'], ['theme', 'images', 'theme_frame'], ['theme', 'images', 'additional_backgrounds'], ['user_scripts', 'api_script']];
54
+ async function collectDependencies(asset, program, ptrs, hmrOptions) {
55
+ var _program$browserActio, _program$background, _program$background2;
56
+ const hot = Boolean(hmrOptions);
57
+ const fs = asset.fs;
58
+ const filePath = asset.filePath;
59
+ const assetDir = _path().default.dirname(filePath);
60
+ const isMV2 = program.manifest_version == 2;
61
+ delete program.$schema;
62
+ if (program.default_locale) {
63
+ const locales = _path().default.join(assetDir, '_locales');
64
+ let err = !(await fs.exists(locales)) ? 'key' : !(await fs.exists(_path().default.join(locales, program.default_locale, 'messages.json'))) ? 'value' : null;
65
+ if (err) {
66
+ throw new (_diagnostic().default)({
67
+ diagnostic: [{
68
+ message: 'Invalid Web Extension manifest',
69
+ origin: '@atlaspack/transformer-webextension',
70
+ codeFrames: [{
71
+ filePath,
72
+ codeHighlights: [{
73
+ ...(0, _diagnostic().getJSONHighlightLocation)(ptrs['/default_locale'], err),
74
+ message: (0, _diagnostic().md)`Localization ${err == 'value' ? 'file for ' + program.default_locale : 'directory'} does not exist: ${_path().default.relative(assetDir, _path().default.join(locales, program.default_locale))}`
75
+ }]
76
+ }]
77
+ }]
78
+ });
79
+ }
80
+ for (const locale of await fs.readdir(locales)) {
81
+ if (await fs.exists(_path().default.join(locales, locale, 'messages.json'))) {
82
+ asset.addURLDependency(`_locales/${locale}/messages.json`, {
83
+ needsStableName: true,
84
+ pipeline: 'raw'
85
+ });
86
+ }
87
+ }
88
+ }
89
+ if (program.content_scripts) {
90
+ for (let i = 0; i < program.content_scripts.length; ++i) {
91
+ const sc = program.content_scripts[i];
92
+ for (const k of ['css', 'js']) {
93
+ const assets = sc[k] || [];
94
+ for (let j = 0; j < assets.length; ++j) {
95
+ assets[j] = asset.addURLDependency(assets[j], {
96
+ bundleBehavior: 'isolated',
97
+ loc: {
98
+ filePath,
99
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs[`/content_scripts/${i}/${k}/${j}`], 'value')
100
+ }
101
+ });
102
+ }
103
+ }
104
+ if (hot && sc.js && sc.js.length) {
105
+ sc.js.push(asset.addURLDependency('./runtime/autoreload.js', {
106
+ resolveFrom: __filename
107
+ }));
108
+ }
109
+ }
110
+ }
111
+ if (program.dictionaries) {
112
+ for (const dict in program.dictionaries) {
113
+ const dictFile = program.dictionaries[dict];
114
+ if (_path().default.extname(dictFile) != '.dic') {
115
+ throw new (_diagnostic().default)({
116
+ diagnostic: [{
117
+ message: 'Invalid Web Extension manifest',
118
+ origin: '@atlaspack/transformer-webextension',
119
+ codeFrames: [{
120
+ filePath,
121
+ codeHighlights: [{
122
+ ...(0, _diagnostic().getJSONHighlightLocation)(ptrs[`/dictionaries/${dict}`], 'value'),
123
+ message: 'Dictionaries must be .dic files'
124
+ }]
125
+ }]
126
+ }]
127
+ });
128
+ }
129
+ const loc = {
130
+ filePath,
131
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs[`/dictionaries/${dict}`], 'value')
132
+ };
133
+ program.dictionaries[dict] = asset.addURLDependency(dictFile, {
134
+ needsStableName: true,
135
+ loc
136
+ });
137
+ asset.addURLDependency(dictFile.slice(0, -4) + '.aff', {
138
+ needsStableName: true,
139
+ loc
140
+ });
141
+ }
142
+ }
143
+ const browserActionName = isMV2 ? 'browser_action' : 'action';
144
+ if ((_program$browserActio = program[browserActionName]) !== null && _program$browserActio !== void 0 && _program$browserActio.theme_icons) {
145
+ for (let i = 0; i < program[browserActionName].theme_icons.length; ++i) {
146
+ const themeIcon = program[browserActionName].theme_icons[i];
147
+ for (const k of ['light', 'dark']) {
148
+ const loc = (0, _diagnostic().getJSONSourceLocation)(ptrs[`/${browserActionName}/theme_icons/${i}/${k}`], 'value');
149
+ themeIcon[k] = asset.addURLDependency(themeIcon[k], {
150
+ loc: {
151
+ ...loc,
152
+ filePath
153
+ }
154
+ });
155
+ }
156
+ }
157
+ }
158
+ if (program.web_accessible_resources) {
159
+ let war = [];
160
+ for (let i = 0; i < program.web_accessible_resources.length; ++i) {
161
+ // TODO: this doesn't support Atlaspack resolution
162
+ const currentEntry = program.web_accessible_resources[i];
163
+ const files = isMV2 ? [currentEntry] : currentEntry.resources;
164
+ let currentFiles = [];
165
+ for (let j = 0; j < files.length; ++j) {
166
+ const globFiles = (await (0, _utils().glob)(_path().default.join(assetDir, files[j]), fs, {})).map(fp => asset.addURLDependency(_path().default.relative(assetDir, fp), {
167
+ bundleBehavior: 'isolated',
168
+ needsStableName: true,
169
+ loc: {
170
+ filePath,
171
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs[`/web_accessible_resources/${i}${isMV2 ? '' : `/resources/${j}`}`])
172
+ }
173
+ }));
174
+ currentFiles = currentFiles.concat(globFiles);
175
+ }
176
+ if (isMV2) {
177
+ war = war.concat(currentFiles);
178
+ } else {
179
+ currentEntry.resources = currentFiles;
180
+ war.push(currentEntry);
181
+ }
182
+ }
183
+ program.web_accessible_resources = war;
184
+ }
185
+ if (program.declarative_net_request) {
186
+ var _program$declarative_;
187
+ const rrs = ((_program$declarative_ = program.declarative_net_request) === null || _program$declarative_ === void 0 ? void 0 : _program$declarative_.rule_resources) ?? [];
188
+ rrs.forEach((resources, i) => {
189
+ resources.path = asset.addURLDependency(resources.path, {
190
+ pipeline: 'raw',
191
+ loc: {
192
+ filePath,
193
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs[`/declarative_net_request/rule_resources/${i}/path`], 'value')
194
+ }
195
+ });
196
+ });
197
+ }
198
+ for (const loc of DEP_LOCS) {
199
+ const location = '/' + loc.join('/');
200
+ if (!ptrs[location]) continue;
201
+ let parent = program;
202
+ for (let i = 0; i < loc.length - 1; ++i) {
203
+ parent = parent[loc[i]];
204
+ }
205
+ const lastLoc = loc[loc.length - 1];
206
+ const obj = parent[lastLoc];
207
+ if (typeof obj == 'string') parent[lastLoc] = asset.addURLDependency(obj, {
208
+ bundleBehavior: 'isolated',
209
+ loc: {
210
+ filePath,
211
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs[location], 'value')
212
+ },
213
+ pipeline: _path().default.extname(obj) == '.json' ? 'raw' : undefined
214
+ });else {
215
+ for (const k of Object.keys(obj)) {
216
+ obj[k] = asset.addURLDependency(obj[k], {
217
+ bundleBehavior: 'isolated',
218
+ loc: {
219
+ filePath,
220
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs[location + '/' + k], 'value')
221
+ },
222
+ pipeline: _path().default.extname(obj[k]) == '.json' ? 'raw' : undefined
223
+ });
224
+ }
225
+ }
226
+ }
227
+ if ((_program$background = program.background) !== null && _program$background !== void 0 && _program$background.page) {
228
+ program.background.page = asset.addURLDependency(program.background.page, {
229
+ bundleBehavior: 'isolated',
230
+ loc: {
231
+ filePath,
232
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs['/background/page'], 'value')
233
+ }
234
+ });
235
+ }
236
+ if ((_program$background2 = program.background) !== null && _program$background2 !== void 0 && _program$background2.service_worker) {
237
+ program.background.service_worker = asset.addURLDependency(program.background.service_worker, {
238
+ bundleBehavior: 'isolated',
239
+ loc: {
240
+ filePath,
241
+ ...(0, _diagnostic().getJSONSourceLocation)(ptrs['/background/service_worker'], 'value')
242
+ },
243
+ env: {
244
+ context: 'service-worker',
245
+ sourceType: program.background.type == 'module' ? 'module' : 'script'
246
+ }
247
+ });
248
+ }
249
+ if (hot) {
250
+ var _program$content_scri;
251
+ if (isMV2) {
252
+ // To enable HMR, we must override the CSP to allow 'unsafe-eval'
253
+ program.content_security_policy = cspPatchHMR(program.content_security_policy);
254
+ } else {
255
+ // Enable HMR for fetched localhost chunks
256
+ const csp = program.content_security_policy || {};
257
+ csp.extension_pages = cspPatchHMR(csp.extension_pages, `http://${(hmrOptions === null || hmrOptions === void 0 ? void 0 : hmrOptions.host) || 'localhost'}:*`);
258
+ // Sandbox allows eval by default
259
+ if (csp.sandbox) csp.sandbox = cspPatchHMR(csp.sandbox);
260
+ program.content_security_policy = csp;
261
+ }
262
+ if (!program.background) {
263
+ program.background = {};
264
+ }
265
+ if (program.background.page) {
266
+ asset.meta.webextBGInsert = program.background.page;
267
+ } else if (isMV2 || program.background.scripts) {
268
+ if (!program.background.scripts) {
269
+ program.background.scripts = [];
270
+ }
271
+ if (program.background.scripts.length == 0) {
272
+ program.background.scripts.push(asset.addURLDependency('./runtime/default-bg.js', {
273
+ resolveFrom: __filename
274
+ }));
275
+ }
276
+ asset.meta.webextBGInsert = program.background.scripts[0];
277
+ } else {
278
+ if (!program.background.service_worker) {
279
+ program.background.service_worker = asset.addURLDependency('./runtime/default-bg.js', {
280
+ resolveFrom: __filename,
281
+ env: {
282
+ context: 'service-worker'
283
+ }
284
+ });
285
+ }
286
+ asset.meta.webextBGInsert = program.background.service_worker;
287
+ }
288
+ if (!program.permissions) program.permissions = [];
289
+ if (!isMV2 && !program.permissions.includes('scripting')) {
290
+ program.permissions.push('scripting');
291
+ }
292
+ const hostPerms = [...new Set((_program$content_scri = program.content_scripts) === null || _program$content_scri === void 0 ? void 0 : _program$content_scri.flatMap(sc => sc.matches))];
293
+ if (isMV2) program.permissions = program.permissions.concat(hostPerms);else {
294
+ if (!program.host_permissions) program.host_permissions = [];
295
+ program.host_permissions = program.host_permissions.concat(hostPerms);
296
+ }
297
+ }
298
+ }
299
+ function cspPatchHMR(policy, insert) {
300
+ let defaultSrc = "'self'";
301
+ if (insert == null) {
302
+ insert = "'unsafe-eval'";
303
+ defaultSrc = "'self' blob: filesystem:";
304
+ }
305
+ if (policy) {
306
+ const csp = (0, _contentSecurityPolicyParser().default)(policy);
307
+ policy = '';
308
+ if (!csp['script-src']) {
309
+ csp['script-src'] = [defaultSrc];
310
+ }
311
+ if (!csp['script-src'].includes(insert)) {
312
+ csp['script-src'].push(insert);
313
+ }
314
+ if (csp.sandbox && !csp.sandbox.includes('allow-scripts')) {
315
+ csp.sandbox.push('allow-scripts');
316
+ }
317
+ for (const k in csp) {
318
+ policy += `${k} ${csp[k].join(' ')};`;
319
+ }
320
+ return policy;
321
+ } else {
322
+ return `script-src ${defaultSrc} ${insert};` + `object-src ${defaultSrc};`;
323
+ }
324
+ }
325
+ var _default = exports.default = new (_plugin().Transformer)({
326
+ async transform({
327
+ asset,
328
+ options
329
+ }) {
330
+ // Set environment to browser, since web extensions are always used in
331
+ // browsers, and because it avoids delegating extra config to the user
332
+ asset.setEnvironment({
333
+ context: 'browser',
334
+ outputFormat: asset.env.outputFormat == 'commonjs' ? 'global' : asset.env.outputFormat,
335
+ engines: {
336
+ browsers: asset.env.engines.browsers
337
+ },
338
+ sourceMap: asset.env.sourceMap && {
339
+ ...asset.env.sourceMap,
340
+ // Inline source maps work most reliably on web extensions but allow users to overwrite
341
+ inline: asset.env.sourceMap.inline ?? true,
342
+ inlineSources: asset.env.sourceMap.inlineSources ?? true
343
+ },
344
+ includeNodeModules: asset.env.includeNodeModules,
345
+ sourceType: asset.env.sourceType,
346
+ isLibrary: asset.env.isLibrary,
347
+ shouldOptimize: asset.env.shouldOptimize,
348
+ shouldScopeHoist: asset.env.shouldScopeHoist
349
+ });
350
+ const code = await asset.getCode();
351
+ const parsed = (0, _jsonSourcemap().parse)(code);
352
+ const data = parsed.data;
353
+
354
+ // Not using a unified schema dramatically improves error messages
355
+ let schema = _schema.VersionSchema;
356
+ if (data.manifest_version === 3) {
357
+ schema = _schema.MV3Schema;
358
+ } else if (data.manifest_version === 2) {
359
+ schema = _schema.MV2Schema;
360
+ }
361
+ _utils().validateSchema.diagnostic(schema, {
362
+ data: data,
363
+ source: code,
364
+ filePath: asset.filePath
365
+ }, '@atlaspack/transformer-webextension', 'Invalid Web Extension manifest');
366
+ await collectDependencies(asset, data, parsed.pointers, options.hmrOptions);
367
+ asset.setCode(JSON.stringify(data, null, 2));
368
+ asset.meta.webextEntry = true;
369
+ return [asset];
370
+ }
371
+ });
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ /* global chrome, browser, addEventListener, location */
4
+ var env = typeof browser == 'undefined' ? chrome : browser;
5
+ var blockReload = true;
6
+ addEventListener('beforeunload', function () {
7
+ if (!blockReload) return;
8
+ try {
9
+ env.runtime.sendMessage({
10
+ __parcel_hmr_reload__: true
11
+ });
12
+ // spinlock for 500ms to let background reload
13
+ let end = Date.now() + 500;
14
+ while (Date.now() < end);
15
+ } catch (err) {
16
+ // ignore throwing if extension context invalidated
17
+ }
18
+ });
19
+ env.runtime.onMessage.addListener(function (msg) {
20
+ if (msg.__parcel_hmr_reload__) {
21
+ blockReload = false;
22
+ setTimeout(function () {
23
+ location.reload();
24
+ }, 400);
25
+ }
26
+ });
@@ -0,0 +1 @@
1
+ "use strict";
package/lib/schema.js ADDED
@@ -0,0 +1,564 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.VersionSchema = exports.MV3Schema = exports.MV2Schema = void 0;
7
+ const validateVersion = ver => {
8
+ const parts = ver.split('.', 5);
9
+ if (parts.length == 5) return 'Extension versions to have at most three dots';
10
+ if (parts.every(part => part.length != 0 && Number(part[0]) >= 0 && Number(part) < 65536)) return;
11
+ return 'Extension versions must be dot-separated integers between 0 and 65535';
12
+ };
13
+ const string = {
14
+ type: 'string'
15
+ };
16
+ const boolean = {
17
+ type: 'boolean'
18
+ };
19
+ const icons = {
20
+ type: 'object',
21
+ properties: {},
22
+ additionalProperties: string
23
+ };
24
+ const actionProps = {
25
+ // FF only
26
+ browser_style: boolean,
27
+ chrome_style: boolean,
28
+ // You can also have a raw string, but not in Edge, apparently...
29
+ default_icon: {
30
+ oneOf: [icons, string]
31
+ },
32
+ default_popup: string,
33
+ default_title: string
34
+ };
35
+ const arrStr = {
36
+ type: 'array',
37
+ items: string
38
+ };
39
+ const browserAction = {
40
+ type: 'object',
41
+ properties: {
42
+ ...actionProps,
43
+ // rest are FF only
44
+ default_area: {
45
+ type: 'string',
46
+ enum: ['navbar', 'menupanel', 'tabstrip', 'personaltoolbar']
47
+ },
48
+ theme_icons: {
49
+ type: 'array',
50
+ items: {
51
+ type: 'object',
52
+ properties: {
53
+ light: string,
54
+ dark: string,
55
+ size: {
56
+ type: 'number'
57
+ }
58
+ },
59
+ additionalProperties: false,
60
+ required: ['light', 'dark', 'size']
61
+ }
62
+ }
63
+ },
64
+ additionalProperties: false
65
+ };
66
+ const warBase = {
67
+ type: 'object',
68
+ properties: {
69
+ resources: arrStr,
70
+ matches: arrStr,
71
+ extension_ids: arrStr,
72
+ use_dynamic_url: boolean
73
+ },
74
+ additionalProperties: false
75
+ };
76
+ const mv2Background = {
77
+ type: 'object',
78
+ properties: {
79
+ scripts: arrStr,
80
+ page: string,
81
+ persistent: boolean
82
+ },
83
+ additionalProperties: false
84
+ };
85
+ const commonProps = {
86
+ $schema: string,
87
+ name: string,
88
+ version: {
89
+ type: 'string',
90
+ __validate: validateVersion
91
+ },
92
+ default_locale: string,
93
+ description: string,
94
+ icons,
95
+ author: string,
96
+ browser_specific_settings: {
97
+ type: 'object',
98
+ properties: {},
99
+ additionalProperties: {
100
+ type: 'object',
101
+ properties: {}
102
+ }
103
+ },
104
+ chrome_settings_overrides: {
105
+ type: 'object',
106
+ properties: {
107
+ homepage: string,
108
+ search_provider: {
109
+ type: 'object',
110
+ properties: {
111
+ name: string,
112
+ keyword: string,
113
+ favicon_url: string,
114
+ search_url: string,
115
+ encoding: string,
116
+ suggest_url: string,
117
+ image_url: string,
118
+ instant_url: string,
119
+ search_url_post_params: string,
120
+ suggest_url_post_params: string,
121
+ image_url_post_params: string,
122
+ instant_url_post_params: string,
123
+ alternate_urls: arrStr,
124
+ prepopulated_id: {
125
+ type: 'number'
126
+ },
127
+ is_default: boolean
128
+ },
129
+ additionalProperties: false,
130
+ required: ['name', 'search_url']
131
+ },
132
+ startup_pages: arrStr
133
+ },
134
+ additionalProperties: false
135
+ },
136
+ chrome_url_overrides: {
137
+ type: 'object',
138
+ properties: {
139
+ bookmarks: string,
140
+ history: string,
141
+ newtab: string
142
+ },
143
+ additionalProperties: false
144
+ },
145
+ commands: {
146
+ type: 'object',
147
+ properties: {},
148
+ additionalProperties: {
149
+ type: 'object',
150
+ properties: {
151
+ suggested_key: {
152
+ type: 'object',
153
+ properties: {
154
+ default: string,
155
+ mac: string,
156
+ linux: string,
157
+ windows: string,
158
+ chromeos: string,
159
+ android: string,
160
+ ios: string
161
+ },
162
+ additionalProperties: false
163
+ },
164
+ description: string
165
+ },
166
+ additionalProperties: false
167
+ }
168
+ },
169
+ content_scripts: {
170
+ type: 'array',
171
+ items: {
172
+ type: 'object',
173
+ properties: {
174
+ matches: arrStr,
175
+ css: arrStr,
176
+ js: arrStr,
177
+ match_about_blank: boolean,
178
+ exclude_matches: arrStr,
179
+ include_globs: arrStr,
180
+ exclude_globs: arrStr,
181
+ run_at: {
182
+ type: 'string',
183
+ enum: ['document_idle', 'document_start', 'document_end']
184
+ },
185
+ all_frames: boolean,
186
+ world: {
187
+ type: 'string',
188
+ enum: ['ISOLATED', 'MAIN']
189
+ }
190
+ },
191
+ additionalProperties: false,
192
+ required: ['matches']
193
+ }
194
+ },
195
+ declarative_net_request: {
196
+ type: 'object',
197
+ properties: {
198
+ rule_resources: {
199
+ type: 'array',
200
+ items: {
201
+ type: 'object',
202
+ properties: {
203
+ id: string,
204
+ enabled: boolean,
205
+ path: string
206
+ },
207
+ additionalProperties: false,
208
+ required: ['id', 'enabled', 'path']
209
+ }
210
+ }
211
+ },
212
+ additionalProperties: false,
213
+ required: ['rule_resources']
214
+ },
215
+ devtools_page: string,
216
+ // looks to be FF only
217
+ dictionaries: {
218
+ type: 'object',
219
+ properties: {},
220
+ additionalProperties: string
221
+ },
222
+ externally_connectable: {
223
+ type: 'object',
224
+ properties: {
225
+ ids: arrStr,
226
+ matches: arrStr,
227
+ accept_tls_channel_id: boolean
228
+ },
229
+ additionalProperties: false
230
+ },
231
+ // These next two are where it gets a bit Chrome-y
232
+ // (we don't include all because some have next to no actual use)
233
+ file_browser_handlers: {
234
+ type: 'array',
235
+ items: {
236
+ type: 'object',
237
+ properties: {
238
+ id: string,
239
+ default_title: string,
240
+ file_filters: arrStr
241
+ },
242
+ additionalProperties: false,
243
+ required: ['id', 'default_title', 'file_filters']
244
+ }
245
+ },
246
+ file_system_provider_capabilities: {
247
+ type: 'object',
248
+ properties: {
249
+ configurable: boolean,
250
+ multiple_mounts: boolean,
251
+ watchable: boolean,
252
+ source: {
253
+ type: 'string',
254
+ enum: ['file', 'device', 'network']
255
+ }
256
+ },
257
+ additionalProperties: false,
258
+ required: ['source']
259
+ },
260
+ homepage_url: string,
261
+ incognito: {
262
+ type: 'string',
263
+ enum: ['spanning', 'split', 'not_allowed']
264
+ },
265
+ key: string,
266
+ minimum_chrome_version: {
267
+ type: 'string',
268
+ __validate: validateVersion
269
+ },
270
+ // No NaCl modules because deprecated
271
+ oauth2: {
272
+ type: 'object',
273
+ properties: {
274
+ client_id: string,
275
+ scopes: arrStr
276
+ },
277
+ additionalProperties: false
278
+ },
279
+ offline_enabled: boolean,
280
+ omnibox: {
281
+ type: 'object',
282
+ properties: {},
283
+ additionalProperties: string
284
+ },
285
+ optional_host_permissions: arrStr,
286
+ optional_permissions: arrStr,
287
+ // options_page is deprecated
288
+ options_ui: {
289
+ type: 'object',
290
+ properties: {
291
+ browser_style: boolean,
292
+ chrome_style: boolean,
293
+ open_in_tab: boolean,
294
+ page: string
295
+ },
296
+ additionalProperties: false,
297
+ required: ['page']
298
+ },
299
+ permissions: arrStr,
300
+ // FF only, but has some use
301
+ protocol_handlers: {
302
+ type: 'array',
303
+ items: {
304
+ type: 'object',
305
+ properties: {
306
+ protocol: string,
307
+ name: string,
308
+ uriTemplate: string
309
+ },
310
+ additionalProperties: false,
311
+ required: ['protocol', 'name', 'uriTemplate']
312
+ }
313
+ },
314
+ // Chrome only
315
+ requirements: {
316
+ type: 'object',
317
+ properties: {
318
+ '3D': {
319
+ type: 'object',
320
+ properties: {
321
+ features: arrStr
322
+ },
323
+ additionalProperties: false
324
+ }
325
+ }
326
+ },
327
+ short_name: string,
328
+ // FF only, but has some use
329
+ sidebar_action: {
330
+ type: 'object',
331
+ properties: {
332
+ browser_style: actionProps.browser_style,
333
+ default_icon: actionProps.default_icon,
334
+ default_panel: string,
335
+ default_title: string,
336
+ open_at_install: boolean
337
+ },
338
+ additionalProperties: false,
339
+ required: ['default_panel']
340
+ },
341
+ storage: {
342
+ type: 'object',
343
+ properties: {
344
+ managed_schema: string
345
+ },
346
+ additionalProperties: false
347
+ },
348
+ theme: {
349
+ type: 'object',
350
+ properties: {
351
+ images: {
352
+ type: 'object',
353
+ properties: {
354
+ theme_frame: string,
355
+ additional_backgrounds: arrStr
356
+ },
357
+ additionalProperties: false
358
+ },
359
+ colors: {
360
+ type: 'object',
361
+ properties: {
362
+ bookmark_text: string,
363
+ button_background_active: string,
364
+ button_background_hover: string,
365
+ icons: string,
366
+ icons_attention: string,
367
+ frame: string,
368
+ frame_inactive: string,
369
+ ntp_background: string,
370
+ ntp_text: string,
371
+ popup: string,
372
+ popup_border: string,
373
+ popup_highlight: string,
374
+ popup_highlight_text: string,
375
+ popup_text: string,
376
+ sidebar: string,
377
+ sidebar_border: string,
378
+ sidebar_highlight: string,
379
+ sidebar_highlight_text: string,
380
+ sidebar_text: string,
381
+ tab_background_separator: string,
382
+ tab_background_text: string,
383
+ tab_line: string,
384
+ tab_loading: string,
385
+ tab_selected: string,
386
+ tab_text: string,
387
+ toolbar: string,
388
+ toolbar_bottom_separator: string,
389
+ toolbar_field: string,
390
+ toolbar_field_border: string,
391
+ toolbar_field_border_focus: string,
392
+ toolbar_field_focus: string,
393
+ toolbar_field_highlight: string,
394
+ toolbar_field_highlight_text: string,
395
+ toolbar_field_separator: string,
396
+ toolbar_field_text: string,
397
+ toolbar_field_text_focus: string,
398
+ toolbar_text: string,
399
+ toolbar_top_separator: string,
400
+ toolbar_vertical_separator: string
401
+ },
402
+ additionalProperties: false
403
+ },
404
+ properties: {
405
+ type: 'object',
406
+ properties: {
407
+ additional_backgrounds_alignment: arrStr,
408
+ additional_backgrounds_tiling: {
409
+ type: 'array',
410
+ items: {
411
+ type: 'string',
412
+ enum: ['no-repeat', 'repeat', 'repeat-x', 'repeat-y']
413
+ }
414
+ }
415
+ },
416
+ additionalProperties: false
417
+ }
418
+ },
419
+ additionalProperties: false,
420
+ required: ['colors']
421
+ },
422
+ tts_engine: {
423
+ type: 'object',
424
+ properties: {
425
+ voices: {
426
+ type: 'array',
427
+ items: {
428
+ type: 'object',
429
+ properties: {
430
+ voice_name: string,
431
+ lang: string,
432
+ event_type: {
433
+ type: 'string',
434
+ enum: ['start', 'word', 'sentence', 'marker', 'end', 'error']
435
+ }
436
+ },
437
+ additionalProperties: false,
438
+ required: ['voice_name', 'event_type']
439
+ }
440
+ }
441
+ },
442
+ additionalProperties: false
443
+ },
444
+ update_url: string,
445
+ user_scripts: {
446
+ type: 'object',
447
+ properties: {
448
+ api_script: string
449
+ },
450
+ additionalProperties: false
451
+ },
452
+ version_name: string
453
+ };
454
+ const MV3Schema = exports.MV3Schema = {
455
+ type: 'object',
456
+ properties: {
457
+ ...commonProps,
458
+ manifest_version: {
459
+ type: 'number',
460
+ enum: [3]
461
+ },
462
+ action: browserAction,
463
+ background: {
464
+ oneOf: [{
465
+ type: 'object',
466
+ properties: {
467
+ service_worker: string,
468
+ type: {
469
+ type: 'string',
470
+ enum: ['classic', 'module']
471
+ },
472
+ // to support both Chrome and Firefox
473
+ scripts: arrStr,
474
+ page: string,
475
+ persistent: boolean
476
+ },
477
+ additionalProperties: false,
478
+ required: ['service_worker']
479
+ }, mv2Background] // for Firefox
480
+ },
481
+
482
+ content_security_policy: {
483
+ type: 'object',
484
+ properties: {
485
+ extension_pages: string,
486
+ sandbox: string
487
+ },
488
+ additionalProperties: false
489
+ },
490
+ host_permissions: arrStr,
491
+ sandbox: {
492
+ type: 'object',
493
+ properties: {
494
+ pages: arrStr
495
+ },
496
+ additionalProperties: false
497
+ },
498
+ side_panel: {
499
+ type: 'object',
500
+ properties: {
501
+ default_path: string
502
+ },
503
+ additionalProperties: false
504
+ },
505
+ web_accessible_resources: {
506
+ type: 'array',
507
+ items: {
508
+ oneOf: [{
509
+ ...warBase,
510
+ required: ['resources', 'matches']
511
+ }, {
512
+ ...warBase,
513
+ required: ['resources', 'extension_ids']
514
+ }]
515
+ }
516
+ }
517
+ },
518
+ required: ['manifest_version', 'name', 'version']
519
+ };
520
+ const MV2Schema = exports.MV2Schema = {
521
+ type: 'object',
522
+ properties: {
523
+ ...commonProps,
524
+ manifest_version: {
525
+ type: 'number',
526
+ enum: [2]
527
+ },
528
+ background: mv2Background,
529
+ browser_action: browserAction,
530
+ content_security_policy: string,
531
+ page_action: {
532
+ type: 'object',
533
+ properties: {
534
+ ...actionProps,
535
+ // rest are FF only
536
+ hide_matches: arrStr,
537
+ show_matches: arrStr,
538
+ pinned: boolean
539
+ },
540
+ additionalProperties: false
541
+ },
542
+ sandbox: {
543
+ type: 'object',
544
+ properties: {
545
+ pages: arrStr,
546
+ content_security_policy: string
547
+ },
548
+ additionalProperties: false
549
+ },
550
+ web_accessible_resources: arrStr
551
+ },
552
+ required: ['manifest_version', 'name', 'version']
553
+ };
554
+ const VersionSchema = exports.VersionSchema = {
555
+ type: 'object',
556
+ properties: {
557
+ $schema: string,
558
+ manifest_version: {
559
+ type: 'number',
560
+ enum: [2, 3]
561
+ }
562
+ },
563
+ required: ['manifest_version']
564
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/transformer-webextension",
3
- "version": "2.14.0",
3
+ "version": "2.14.1-canary.6+57e29c592",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,14 +12,15 @@
12
12
  "main": "lib/WebExtensionTransformer.js",
13
13
  "source": "src/WebExtensionTransformer.js",
14
14
  "engines": {
15
- "atlaspack": "^2.12.0"
15
+ "atlaspack": "2.14.1-canary.6+57e29c592"
16
16
  },
17
17
  "dependencies": {
18
+ "@atlaspack/diagnostic": "2.14.1-canary.6+57e29c592",
19
+ "@atlaspack/plugin": "2.14.1-canary.6+57e29c592",
20
+ "@atlaspack/utils": "2.14.1-canary.6+57e29c592",
18
21
  "@mischnic/json-sourcemap": "^0.1.0",
19
- "@atlaspack/diagnostic": "2.14.0",
20
- "@atlaspack/plugin": "2.14.0",
21
- "@atlaspack/utils": "2.14.0",
22
22
  "content-security-policy-parser": "^0.3.0"
23
23
  },
24
- "type": "commonjs"
24
+ "type": "commonjs",
25
+ "gitHead": "57e29c592cff406d084b3c676a1c899f0bb6e78f"
25
26
  }