wext-manifest-transformer 1.0.0

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.
@@ -0,0 +1,18 @@
1
+ export declare const ENVKeys: {
2
+ readonly DEV: "dev";
3
+ readonly PROD: "prod";
4
+ };
5
+ export declare const Browser: {
6
+ readonly CHROME: "chrome";
7
+ readonly FIREFOX: "firefox";
8
+ readonly EDGE: "edge";
9
+ readonly BRAVE: "brave";
10
+ readonly OPERA: "opera";
11
+ readonly VIVALDI: "vivaldi";
12
+ readonly ARC: "arc";
13
+ readonly YANDEX: "yandex";
14
+ };
15
+ export type BrowserType = (typeof Browser)[keyof typeof Browser];
16
+ export declare const browserVendors: BrowserType[];
17
+ export declare const envVariables: string[];
18
+ export declare const CUSTOM_PREFIX_REGEX: RegExp;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CUSTOM_PREFIX_REGEX = exports.envVariables = exports.browserVendors = exports.Browser = exports.ENVKeys = void 0;
4
+ exports.ENVKeys = {
5
+ DEV: 'dev',
6
+ PROD: 'prod',
7
+ };
8
+ exports.Browser = {
9
+ CHROME: 'chrome',
10
+ FIREFOX: 'firefox',
11
+ EDGE: 'edge',
12
+ BRAVE: 'brave',
13
+ OPERA: 'opera',
14
+ VIVALDI: 'vivaldi',
15
+ ARC: 'arc',
16
+ YANDEX: 'yandex',
17
+ };
18
+ exports.browserVendors = Object.values(exports.Browser);
19
+ exports.envVariables = [exports.ENVKeys.DEV, exports.ENVKeys.PROD];
20
+ exports.CUSTOM_PREFIX_REGEX = new RegExp(`^__((?:(?:${[...exports.browserVendors, ...exports.envVariables].join('|')})\\|?)+)__(.*)`);
@@ -0,0 +1,2 @@
1
+ import { transformer } from './transform';
2
+ export default transformer;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const transform_1 = require("./transform");
4
+ exports.default = transform_1.transformer;
5
+ module.exports = transform_1.transformer;
6
+ module.exports.default = transform_1.transformer;
@@ -0,0 +1,39 @@
1
+ {
2
+ "manifest_version": 3,
3
+ "name": "web.site",
4
+ "version": "0.0.0",
5
+ "icons": {
6
+ "16": "assets/icons/favicon-16.png",
7
+ "32": "assets/icons/favicon-32.png",
8
+ "48": "assets/icons/favicon-48.png",
9
+ "128": "assets/icons/favicon-128.png"
10
+ },
11
+ "description": "web.site",
12
+ "homepage_url": "https://web.site",
13
+ "short_name": "web.site",
14
+ "host_permissions": [
15
+ "*://*.web.site/*"
16
+ ],
17
+ "content_security_policy": {
18
+ "extension_pages": "script-src 'self' http://localhost:8097; object-src 'self'"
19
+ },
20
+ "author": "abhijithvijayan",
21
+ "minimum_chrome_version": "88",
22
+ "background": {
23
+ "scripts": [
24
+ "js/background.bundle.js"
25
+ ],
26
+ "persistent": false
27
+ },
28
+ "content_scripts": [
29
+ {
30
+ "matches": [
31
+ "http://web.site/*",
32
+ "https://web.site/*"
33
+ ],
34
+ "js": [
35
+ "js/contentScript.bundle.js"
36
+ ]
37
+ }
38
+ ]
39
+ }
@@ -0,0 +1,41 @@
1
+ {
2
+ "manifest_version": 2,
3
+ "name": "web.site",
4
+ "version": "0.0.0",
5
+ "icons": {
6
+ "16": "assets/icons/favicon-16.png",
7
+ "32": "assets/icons/favicon-32.png",
8
+ "48": "assets/icons/favicon-48.png",
9
+ "128": "assets/icons/favicon-128.png"
10
+ },
11
+ "description": "web.site",
12
+ "homepage_url": "https://web.site",
13
+ "short_name": "web.site",
14
+ "host_permissions": [
15
+ "*://*.web.site/*"
16
+ ],
17
+ "content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
18
+ "author": "abhijithvijayan",
19
+ "applications": {
20
+ "gecko": {
21
+ "id": "{754FB1AD-CC3B-4856-B6A0-7786F8CA9D17}",
22
+ "strict_min_version": "52.0"
23
+ }
24
+ },
25
+ "background": {
26
+ "scripts": [
27
+ "js/background.bundle.js"
28
+ ]
29
+ },
30
+ "content_scripts": [
31
+ {
32
+ "matches": [
33
+ "http://web.site/*",
34
+ "https://web.site/*"
35
+ ],
36
+ "js": [
37
+ "js/contentScript.bundle.js"
38
+ ]
39
+ }
40
+ ]
41
+ }
@@ -0,0 +1,56 @@
1
+ {
2
+ "__chrome|opera|edge__manifest_version": 3,
3
+ "__firefox__manifest_version": 2,
4
+ "name": "web.site",
5
+ "version": "0.0.0",
6
+ "icons": {
7
+ "16": "assets/icons/favicon-16.png",
8
+ "32": "assets/icons/favicon-32.png",
9
+ "48": "assets/icons/favicon-48.png",
10
+ "128": "assets/icons/favicon-128.png"
11
+ },
12
+ "description": "web.site",
13
+ "homepage_url": "https://web.site",
14
+ "short_name": "web.site",
15
+ "__dev__host_permissions": [
16
+ "*://*.web.site/*"
17
+ ],
18
+ "__prod__host_permissions": [
19
+ "https://*.web.site/*"
20
+ ],
21
+ "__chrome|opera|edge|dev__content_security_policy": {
22
+ "extension_pages": "script-src 'self' http://localhost:8097; object-src 'self'"
23
+ },
24
+ "__chrome|opera|edge|prod__content_security_policy": {
25
+ "extension_pages": "script-src 'self'; object-src 'self'"
26
+ },
27
+ "__firefox|dev__content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
28
+ "__firefox|prod__content_security_policy": "script-src 'self'; object-src 'self'",
29
+ "__chrome|firefox__author": "abhijithvijayan",
30
+ "__opera__developer": {
31
+ "name": "abhijithvijayan"
32
+ },
33
+ "__firefox__applications": {
34
+ "gecko": {
35
+ "id": "{754FB1AD-CC3B-4856-B6A0-7786F8CA9D17}",
36
+ "strict_min_version": "52.0"
37
+ }
38
+ },
39
+ "__chrome__minimum_chrome_version": "88",
40
+ "__opera__minimum_opera_version": "74",
41
+ "background": {
42
+ "scripts": [
43
+ "js/background.bundle.js"
44
+ ],
45
+ "__chrome|opera__persistent": false
46
+ },
47
+ "content_scripts": [{
48
+ "matches": [
49
+ "http://web.site/*",
50
+ "https://web.site/*"
51
+ ],
52
+ "js": [
53
+ "js/contentScript.bundle.js"
54
+ ]
55
+ }]
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../constants");
4
+ describe('RegExp tests', () => {
5
+ const invalidMatches = [
6
+ 'key',
7
+ '__key__',
8
+ 'chrome',
9
+ 'CHROME',
10
+ 'dev',
11
+ 'DEV',
12
+ '__dev_',
13
+ 'prod',
14
+ '__DEV__',
15
+ '__PROD__',
16
+ '__chrome|typo__',
17
+ ];
18
+ test.each(invalidMatches)('should return false for %s', (input) => {
19
+ expect(constants_1.CUSTOM_PREFIX_REGEX.test(input)).toEqual(false);
20
+ });
21
+ const validMatches = [
22
+ '__dev__',
23
+ '__prod__',
24
+ '__chrome__key',
25
+ '__chrome|firefox__key',
26
+ '__chrome|firefox|opera__key',
27
+ '__chrome|firefox|opera|edge__key',
28
+ '__chrome|chrome__key',
29
+ '__chrome|dev__key',
30
+ '__chrome|prod__key',
31
+ '__chrome|firefox|dev__key',
32
+ '__chrome|firefox|prod__key',
33
+ ];
34
+ test.each(validMatches)('should return true for %s', (input) => {
35
+ expect(constants_1.CUSTOM_PREFIX_REGEX.test(input)).toEqual(true);
36
+ });
37
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const manifest_json_1 = __importDefault(require("./manifest.json"));
7
+ const chrome_json_1 = __importDefault(require("./chrome.json"));
8
+ const firefox_json_1 = __importDefault(require("./firefox.json"));
9
+ const transform_1 = require("../transform");
10
+ const constants_1 = require("../constants");
11
+ describe("transformer tests", () => {
12
+ it("should return empty object", () => {
13
+ expect((0, transform_1.transformer)({}, "chrome", "development")).toEqual({});
14
+ });
15
+ it("should return correct JSON for JSON without vendor prefixes", () => {
16
+ expect((0, transform_1.transformer)(JSON.parse(`
17
+ {
18
+ "name": "test",
19
+ "manifest_version": 2
20
+ }
21
+ `), constants_1.Browser.CHROME, "development")).toEqual(JSON.parse(`
22
+ {
23
+ "name": "test",
24
+ "manifest_version": 2
25
+ }
26
+ `));
27
+ });
28
+ });
29
+ describe("ENV Tests", () => {
30
+ it("should return correct JSON for development", () => {
31
+ expect((0, transform_1.transformer)(JSON.parse(`
32
+ {
33
+ "__dev__content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
34
+ "__prod__content_security_policy": "script-src 'self'; object-src 'self'"
35
+ }
36
+ `), constants_1.Browser.CHROME, "development")).toEqual(JSON.parse(`
37
+ {
38
+ "content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'"
39
+ }
40
+ `));
41
+ });
42
+ it("should return correct JSON for production", () => {
43
+ expect((0, transform_1.transformer)(JSON.parse(`
44
+ {
45
+ "__dev__content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
46
+ "__prod__content_security_policy": "script-src 'self'; object-src 'self'"
47
+ }
48
+ `), constants_1.Browser.CHROME, "production")).toEqual(JSON.parse(`
49
+ {
50
+ "content_security_policy": "script-src 'self'; object-src 'self'"
51
+ }
52
+ `));
53
+ });
54
+ });
55
+ describe("chrome tests", () => {
56
+ it("should return correct JSON for chrome", () => {
57
+ expect((0, transform_1.transformer)(JSON.parse(`
58
+ {
59
+ "__chrome|opera|edge__manifest_version": 3,
60
+ "__firefox__manifest_version": 2
61
+ }
62
+ `), constants_1.Browser.CHROME, "development")).toEqual(JSON.parse(`
63
+ {
64
+ "manifest_version": 3
65
+ }
66
+ `));
67
+ });
68
+ it("nested vendor keys", () => {
69
+ expect((0, transform_1.transformer)(JSON.parse(`
70
+ {
71
+ "options_ui": {
72
+ "page": "options.html",
73
+ "open_in_tab": true,
74
+ "__chrome__chrome_style": false,
75
+ "__firefox|opera__browser_style": false
76
+ }
77
+ }
78
+ `), constants_1.Browser.CHROME, "development")).toEqual(JSON.parse(`
79
+ {
80
+ "options_ui": {
81
+ "page": "options.html",
82
+ "open_in_tab": true,
83
+ "chrome_style": false
84
+ }
85
+ }
86
+ `));
87
+ });
88
+ it("should transform whole JSON", () => {
89
+ expect((0, transform_1.transformer)(manifest_json_1.default, constants_1.Browser.CHROME, "development")).toEqual(chrome_json_1.default);
90
+ });
91
+ });
92
+ describe("firefox tests", () => {
93
+ it("should return correct JSON for firefox", () => {
94
+ expect((0, transform_1.transformer)(JSON.parse(`
95
+ {
96
+ "__chrome|opera|edge__manifest_version": 3,
97
+ "__firefox__manifest_version": 2
98
+ }
99
+ `), constants_1.Browser.FIREFOX, "development")).toEqual(JSON.parse(`
100
+ {
101
+ "manifest_version": 2
102
+ }
103
+ `));
104
+ });
105
+ it("nested vendor keys", () => {
106
+ expect((0, transform_1.transformer)(JSON.parse(`
107
+ {
108
+ "options_ui": {
109
+ "page": "options.html",
110
+ "open_in_tab": true,
111
+ "__chrome__chrome_style": false,
112
+ "__firefox|opera__browser_style": false
113
+ }
114
+ }
115
+ `), constants_1.Browser.FIREFOX, "development")).toEqual(JSON.parse(`
116
+ {
117
+ "options_ui": {
118
+ "page": "options.html",
119
+ "open_in_tab": true,
120
+ "browser_style": false
121
+ }
122
+ }
123
+ `));
124
+ });
125
+ it("should transform whole JSON", () => {
126
+ expect((0, transform_1.transformer)(manifest_json_1.default, constants_1.Browser.FIREFOX, "development")).toEqual(firefox_json_1.default);
127
+ });
128
+ });
@@ -0,0 +1,2 @@
1
+ import { BrowserType } from "./constants";
2
+ export declare const transformer: (manifest: Record<string, string> | string | number | unknown, selectedVendor: BrowserType, nodeEnv: "production" | "development" | string) => any;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformer = void 0;
4
+ const constants_1 = require("./constants");
5
+ const transformer = (manifest, selectedVendor, nodeEnv) => {
6
+ if (Array.isArray(manifest)) {
7
+ return manifest.map((newManifest) => {
8
+ return (0, exports.transformer)(newManifest, selectedVendor, nodeEnv);
9
+ });
10
+ }
11
+ if (typeof manifest === "object" && !!manifest) {
12
+ return Object.entries(manifest).reduce((newManifest, [key, value]) => {
13
+ const vendorMatch = key.match(constants_1.CUSTOM_PREFIX_REGEX);
14
+ if (vendorMatch) {
15
+ const matches = vendorMatch[1].split("|");
16
+ const isProd = nodeEnv === "production";
17
+ const hasCurrentVendor = matches.includes(selectedVendor);
18
+ const hasVendorKeys = matches.some((m) => constants_1.browserVendors.includes(m));
19
+ const hasEnvKey = matches.some((m) => constants_1.envVariables.includes(m));
20
+ const hasCurrentEnvKey = hasEnvKey &&
21
+ ((isProd && matches.includes(constants_1.ENVKeys.PROD)) ||
22
+ (!isProd && matches.includes(constants_1.ENVKeys.DEV)));
23
+ if ((hasCurrentVendor && hasCurrentEnvKey) ||
24
+ (!hasVendorKeys && hasCurrentEnvKey) ||
25
+ (!hasEnvKey && hasCurrentVendor)) {
26
+ newManifest[vendorMatch[2]] = (0, exports.transformer)(value, selectedVendor, nodeEnv);
27
+ }
28
+ }
29
+ else {
30
+ newManifest[key] = (0, exports.transformer)(value, selectedVendor, nodeEnv);
31
+ }
32
+ return newManifest;
33
+ }, {});
34
+ }
35
+ return manifest;
36
+ };
37
+ exports.transformer = transformer;
@@ -0,0 +1,18 @@
1
+ export declare const ENVKeys: {
2
+ readonly DEV: "dev";
3
+ readonly PROD: "prod";
4
+ };
5
+ export declare const Browser: {
6
+ readonly CHROME: "chrome";
7
+ readonly FIREFOX: "firefox";
8
+ readonly EDGE: "edge";
9
+ readonly BRAVE: "brave";
10
+ readonly OPERA: "opera";
11
+ readonly VIVALDI: "vivaldi";
12
+ readonly ARC: "arc";
13
+ readonly YANDEX: "yandex";
14
+ };
15
+ export type BrowserType = (typeof Browser)[keyof typeof Browser];
16
+ export declare const browserVendors: BrowserType[];
17
+ export declare const envVariables: string[];
18
+ export declare const CUSTOM_PREFIX_REGEX: RegExp;
@@ -0,0 +1,17 @@
1
+ export const ENVKeys = {
2
+ DEV: 'dev',
3
+ PROD: 'prod',
4
+ };
5
+ export const Browser = {
6
+ CHROME: 'chrome',
7
+ FIREFOX: 'firefox',
8
+ EDGE: 'edge',
9
+ BRAVE: 'brave',
10
+ OPERA: 'opera',
11
+ VIVALDI: 'vivaldi',
12
+ ARC: 'arc',
13
+ YANDEX: 'yandex',
14
+ };
15
+ export const browserVendors = Object.values(Browser);
16
+ export const envVariables = [ENVKeys.DEV, ENVKeys.PROD];
17
+ export const CUSTOM_PREFIX_REGEX = new RegExp(`^__((?:(?:${[...browserVendors, ...envVariables].join('|')})\\|?)+)__(.*)`);
@@ -0,0 +1,2 @@
1
+ import { transformer } from './transform';
2
+ export default transformer;
@@ -0,0 +1,4 @@
1
+ import { transformer } from './transform';
2
+ export default transformer;
3
+ module.exports = transformer;
4
+ module.exports.default = transformer;
@@ -0,0 +1,39 @@
1
+ {
2
+ "manifest_version": 3,
3
+ "name": "web.site",
4
+ "version": "0.0.0",
5
+ "icons": {
6
+ "16": "assets/icons/favicon-16.png",
7
+ "32": "assets/icons/favicon-32.png",
8
+ "48": "assets/icons/favicon-48.png",
9
+ "128": "assets/icons/favicon-128.png"
10
+ },
11
+ "description": "web.site",
12
+ "homepage_url": "https://web.site",
13
+ "short_name": "web.site",
14
+ "host_permissions": [
15
+ "*://*.web.site/*"
16
+ ],
17
+ "content_security_policy": {
18
+ "extension_pages": "script-src 'self' http://localhost:8097; object-src 'self'"
19
+ },
20
+ "author": "abhijithvijayan",
21
+ "minimum_chrome_version": "88",
22
+ "background": {
23
+ "scripts": [
24
+ "js/background.bundle.js"
25
+ ],
26
+ "persistent": false
27
+ },
28
+ "content_scripts": [
29
+ {
30
+ "matches": [
31
+ "http://web.site/*",
32
+ "https://web.site/*"
33
+ ],
34
+ "js": [
35
+ "js/contentScript.bundle.js"
36
+ ]
37
+ }
38
+ ]
39
+ }
@@ -0,0 +1,41 @@
1
+ {
2
+ "manifest_version": 2,
3
+ "name": "web.site",
4
+ "version": "0.0.0",
5
+ "icons": {
6
+ "16": "assets/icons/favicon-16.png",
7
+ "32": "assets/icons/favicon-32.png",
8
+ "48": "assets/icons/favicon-48.png",
9
+ "128": "assets/icons/favicon-128.png"
10
+ },
11
+ "description": "web.site",
12
+ "homepage_url": "https://web.site",
13
+ "short_name": "web.site",
14
+ "host_permissions": [
15
+ "*://*.web.site/*"
16
+ ],
17
+ "content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
18
+ "author": "abhijithvijayan",
19
+ "applications": {
20
+ "gecko": {
21
+ "id": "{754FB1AD-CC3B-4856-B6A0-7786F8CA9D17}",
22
+ "strict_min_version": "52.0"
23
+ }
24
+ },
25
+ "background": {
26
+ "scripts": [
27
+ "js/background.bundle.js"
28
+ ]
29
+ },
30
+ "content_scripts": [
31
+ {
32
+ "matches": [
33
+ "http://web.site/*",
34
+ "https://web.site/*"
35
+ ],
36
+ "js": [
37
+ "js/contentScript.bundle.js"
38
+ ]
39
+ }
40
+ ]
41
+ }
@@ -0,0 +1,56 @@
1
+ {
2
+ "__chrome|opera|edge__manifest_version": 3,
3
+ "__firefox__manifest_version": 2,
4
+ "name": "web.site",
5
+ "version": "0.0.0",
6
+ "icons": {
7
+ "16": "assets/icons/favicon-16.png",
8
+ "32": "assets/icons/favicon-32.png",
9
+ "48": "assets/icons/favicon-48.png",
10
+ "128": "assets/icons/favicon-128.png"
11
+ },
12
+ "description": "web.site",
13
+ "homepage_url": "https://web.site",
14
+ "short_name": "web.site",
15
+ "__dev__host_permissions": [
16
+ "*://*.web.site/*"
17
+ ],
18
+ "__prod__host_permissions": [
19
+ "https://*.web.site/*"
20
+ ],
21
+ "__chrome|opera|edge|dev__content_security_policy": {
22
+ "extension_pages": "script-src 'self' http://localhost:8097; object-src 'self'"
23
+ },
24
+ "__chrome|opera|edge|prod__content_security_policy": {
25
+ "extension_pages": "script-src 'self'; object-src 'self'"
26
+ },
27
+ "__firefox|dev__content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
28
+ "__firefox|prod__content_security_policy": "script-src 'self'; object-src 'self'",
29
+ "__chrome|firefox__author": "abhijithvijayan",
30
+ "__opera__developer": {
31
+ "name": "abhijithvijayan"
32
+ },
33
+ "__firefox__applications": {
34
+ "gecko": {
35
+ "id": "{754FB1AD-CC3B-4856-B6A0-7786F8CA9D17}",
36
+ "strict_min_version": "52.0"
37
+ }
38
+ },
39
+ "__chrome__minimum_chrome_version": "88",
40
+ "__opera__minimum_opera_version": "74",
41
+ "background": {
42
+ "scripts": [
43
+ "js/background.bundle.js"
44
+ ],
45
+ "__chrome|opera__persistent": false
46
+ },
47
+ "content_scripts": [{
48
+ "matches": [
49
+ "http://web.site/*",
50
+ "https://web.site/*"
51
+ ],
52
+ "js": [
53
+ "js/contentScript.bundle.js"
54
+ ]
55
+ }]
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ import { CUSTOM_PREFIX_REGEX } from '../constants';
2
+ describe('RegExp tests', () => {
3
+ const invalidMatches = [
4
+ 'key',
5
+ '__key__',
6
+ 'chrome',
7
+ 'CHROME',
8
+ 'dev',
9
+ 'DEV',
10
+ '__dev_',
11
+ 'prod',
12
+ '__DEV__',
13
+ '__PROD__',
14
+ '__chrome|typo__',
15
+ ];
16
+ test.each(invalidMatches)('should return false for %s', (input) => {
17
+ expect(CUSTOM_PREFIX_REGEX.test(input)).toEqual(false);
18
+ });
19
+ const validMatches = [
20
+ '__dev__',
21
+ '__prod__',
22
+ '__chrome__key',
23
+ '__chrome|firefox__key',
24
+ '__chrome|firefox|opera__key',
25
+ '__chrome|firefox|opera|edge__key',
26
+ '__chrome|chrome__key',
27
+ '__chrome|dev__key',
28
+ '__chrome|prod__key',
29
+ '__chrome|firefox|dev__key',
30
+ '__chrome|firefox|prod__key',
31
+ ];
32
+ test.each(validMatches)('should return true for %s', (input) => {
33
+ expect(CUSTOM_PREFIX_REGEX.test(input)).toEqual(true);
34
+ });
35
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,123 @@
1
+ import manifest from "./manifest.json";
2
+ import chromeOutput from "./chrome.json";
3
+ import firefoxOutput from "./firefox.json";
4
+ import { transformer } from "../transform";
5
+ import { Browser } from "../constants";
6
+ describe("transformer tests", () => {
7
+ it("should return empty object", () => {
8
+ expect(transformer({}, "chrome", "development")).toEqual({});
9
+ });
10
+ it("should return correct JSON for JSON without vendor prefixes", () => {
11
+ expect(transformer(JSON.parse(`
12
+ {
13
+ "name": "test",
14
+ "manifest_version": 2
15
+ }
16
+ `), Browser.CHROME, "development")).toEqual(JSON.parse(`
17
+ {
18
+ "name": "test",
19
+ "manifest_version": 2
20
+ }
21
+ `));
22
+ });
23
+ });
24
+ describe("ENV Tests", () => {
25
+ it("should return correct JSON for development", () => {
26
+ expect(transformer(JSON.parse(`
27
+ {
28
+ "__dev__content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
29
+ "__prod__content_security_policy": "script-src 'self'; object-src 'self'"
30
+ }
31
+ `), Browser.CHROME, "development")).toEqual(JSON.parse(`
32
+ {
33
+ "content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'"
34
+ }
35
+ `));
36
+ });
37
+ it("should return correct JSON for production", () => {
38
+ expect(transformer(JSON.parse(`
39
+ {
40
+ "__dev__content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",
41
+ "__prod__content_security_policy": "script-src 'self'; object-src 'self'"
42
+ }
43
+ `), Browser.CHROME, "production")).toEqual(JSON.parse(`
44
+ {
45
+ "content_security_policy": "script-src 'self'; object-src 'self'"
46
+ }
47
+ `));
48
+ });
49
+ });
50
+ describe("chrome tests", () => {
51
+ it("should return correct JSON for chrome", () => {
52
+ expect(transformer(JSON.parse(`
53
+ {
54
+ "__chrome|opera|edge__manifest_version": 3,
55
+ "__firefox__manifest_version": 2
56
+ }
57
+ `), Browser.CHROME, "development")).toEqual(JSON.parse(`
58
+ {
59
+ "manifest_version": 3
60
+ }
61
+ `));
62
+ });
63
+ it("nested vendor keys", () => {
64
+ expect(transformer(JSON.parse(`
65
+ {
66
+ "options_ui": {
67
+ "page": "options.html",
68
+ "open_in_tab": true,
69
+ "__chrome__chrome_style": false,
70
+ "__firefox|opera__browser_style": false
71
+ }
72
+ }
73
+ `), Browser.CHROME, "development")).toEqual(JSON.parse(`
74
+ {
75
+ "options_ui": {
76
+ "page": "options.html",
77
+ "open_in_tab": true,
78
+ "chrome_style": false
79
+ }
80
+ }
81
+ `));
82
+ });
83
+ it("should transform whole JSON", () => {
84
+ expect(transformer(manifest, Browser.CHROME, "development")).toEqual(chromeOutput);
85
+ });
86
+ });
87
+ describe("firefox tests", () => {
88
+ it("should return correct JSON for firefox", () => {
89
+ expect(transformer(JSON.parse(`
90
+ {
91
+ "__chrome|opera|edge__manifest_version": 3,
92
+ "__firefox__manifest_version": 2
93
+ }
94
+ `), Browser.FIREFOX, "development")).toEqual(JSON.parse(`
95
+ {
96
+ "manifest_version": 2
97
+ }
98
+ `));
99
+ });
100
+ it("nested vendor keys", () => {
101
+ expect(transformer(JSON.parse(`
102
+ {
103
+ "options_ui": {
104
+ "page": "options.html",
105
+ "open_in_tab": true,
106
+ "__chrome__chrome_style": false,
107
+ "__firefox|opera__browser_style": false
108
+ }
109
+ }
110
+ `), Browser.FIREFOX, "development")).toEqual(JSON.parse(`
111
+ {
112
+ "options_ui": {
113
+ "page": "options.html",
114
+ "open_in_tab": true,
115
+ "browser_style": false
116
+ }
117
+ }
118
+ `));
119
+ });
120
+ it("should transform whole JSON", () => {
121
+ expect(transformer(manifest, Browser.FIREFOX, "development")).toEqual(firefoxOutput);
122
+ });
123
+ });
@@ -0,0 +1,2 @@
1
+ import { BrowserType } from "./constants";
2
+ export declare const transformer: (manifest: Record<string, string> | string | number | unknown, selectedVendor: BrowserType, nodeEnv: "production" | "development" | string) => any;
@@ -0,0 +1,33 @@
1
+ import { CUSTOM_PREFIX_REGEX, browserVendors, envVariables, ENVKeys, } from "./constants";
2
+ export const transformer = (manifest, selectedVendor, nodeEnv) => {
3
+ if (Array.isArray(manifest)) {
4
+ return manifest.map((newManifest) => {
5
+ return transformer(newManifest, selectedVendor, nodeEnv);
6
+ });
7
+ }
8
+ if (typeof manifest === "object" && !!manifest) {
9
+ return Object.entries(manifest).reduce((newManifest, [key, value]) => {
10
+ const vendorMatch = key.match(CUSTOM_PREFIX_REGEX);
11
+ if (vendorMatch) {
12
+ const matches = vendorMatch[1].split("|");
13
+ const isProd = nodeEnv === "production";
14
+ const hasCurrentVendor = matches.includes(selectedVendor);
15
+ const hasVendorKeys = matches.some((m) => browserVendors.includes(m));
16
+ const hasEnvKey = matches.some((m) => envVariables.includes(m));
17
+ const hasCurrentEnvKey = hasEnvKey &&
18
+ ((isProd && matches.includes(ENVKeys.PROD)) ||
19
+ (!isProd && matches.includes(ENVKeys.DEV)));
20
+ if ((hasCurrentVendor && hasCurrentEnvKey) ||
21
+ (!hasVendorKeys && hasCurrentEnvKey) ||
22
+ (!hasEnvKey && hasCurrentVendor)) {
23
+ newManifest[vendorMatch[2]] = transformer(value, selectedVendor, nodeEnv);
24
+ }
25
+ }
26
+ else {
27
+ newManifest[key] = transformer(value, selectedVendor, nodeEnv);
28
+ }
29
+ return newManifest;
30
+ }, {});
31
+ }
32
+ return manifest;
33
+ };
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Abhijith Vijayan <email@abhijithvijayan.in> (abhijithvijayan.in)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "wext-manifest-transformer",
3
+ "version": "1.0.0",
4
+ "description": "Transformer that lets you specify `manifest.json` properties to appear only in specific browsers.",
5
+ "license": "MIT",
6
+ "repository": "https://github.com/abhijithvijayan/wext-manifest-transformer.git",
7
+ "author": {
8
+ "name": "abhijithvijayan",
9
+ "email": "email@abhijithvijayan.in",
10
+ "url": "https://abhijithvijayan.in"
11
+ },
12
+ "engines": {
13
+ "node": ">=10.0.0"
14
+ },
15
+ "main": "lib/cjs/index.js",
16
+ "module": "lib/esm/index.js",
17
+ "types": "lib/cjs/index.d.ts",
18
+ "files": [
19
+ "lib"
20
+ ],
21
+ "scripts": {
22
+ "dev": "tsc --module esnext --outDir lib/esm --watch",
23
+ "dev:cjs": "tsc --module commonjs --outDir lib/cjs --watch",
24
+ "build:cjs": "tsc --module commonjs --outDir lib/cjs",
25
+ "build:esm": "tsc --module esnext --outDir lib/esm",
26
+ "build": "rimraf lib && npm run build:cjs && npm run build:esm",
27
+ "pack:list": "npm pack && tar -xvzf *.tgz && rm -rf package *.tgz",
28
+ "prepublishOnly": "npm run pack:list",
29
+ "test": "jest",
30
+ "test:watch": "jest --watch",
31
+ "lint": "eslint . --ext .ts",
32
+ "lint:fix": "eslint . --ext .ts --fix"
33
+ },
34
+ "husky": {
35
+ "hooks": {
36
+ "pre-commit": "lint-staged"
37
+ }
38
+ },
39
+ "lint-staged": {
40
+ "*.ts": [
41
+ "eslint --ext .ts"
42
+ ]
43
+ },
44
+ "keywords": [
45
+ "webextension",
46
+ "manifest",
47
+ "wext",
48
+ "transformer",
49
+ "chrome",
50
+ "firefox",
51
+ "edge",
52
+ "brave",
53
+ "opera",
54
+ "vivaldi",
55
+ "arc",
56
+ "yandex"
57
+ ],
58
+ "devDependencies": {
59
+ "@abhijithvijayan/eslint-config": "2.8.1",
60
+ "@abhijithvijayan/eslint-config-airbnb": "1.1.0",
61
+ "@abhijithvijayan/tsconfig": "1.3.0",
62
+ "@babel/eslint-parser": "7.27.5",
63
+ "@types/jest": "30.0.0",
64
+ "@types/node": "20.19.1",
65
+ "@typescript-eslint/eslint-plugin": "8.35.0",
66
+ "@typescript-eslint/parser": "8.35.0",
67
+ "eslint": "9.29.0",
68
+ "eslint-config-prettier": "10.1.5",
69
+ "eslint-plugin-import": "2.32.0",
70
+ "eslint-plugin-jsx-a11y": "6.10.2",
71
+ "eslint-plugin-node": "11.1.0",
72
+ "eslint-plugin-prettier": "5.5.0",
73
+ "eslint-plugin-react": "7.37.5",
74
+ "eslint-plugin-react-hooks": "5.2.0",
75
+ "husky": "9.1.7",
76
+ "jest": "30.0.2",
77
+ "lint-staged": "16.1.2",
78
+ "prettier": "3.6.0",
79
+ "rimraf": "6.0.1",
80
+ "ts-jest": "29.4.0",
81
+ "typescript": "5.8.3"
82
+ }
83
+ }
package/readme.md ADDED
@@ -0,0 +1,223 @@
1
+ <h1 align="center">wext-manifest-transformer</h1>
2
+ <p align="center">Transformer module for Webextension manifest.json</p>
3
+ <div align="center">
4
+ <a href="https://www.npmjs.com/package/wext-manifest-transformer">
5
+ <img src="https://img.shields.io/npm/v/wext-manifest-transformer" alt="NPM" />
6
+ </a>
7
+ <a href="https://david-dm.org/abhijithvijayan/wext-manifest-transformer">
8
+ <img src="https://img.shields.io/david/abhijithvijayan/wext-manifest-transformer.svg?colorB=orange" alt="DEPENDENCIES" />
9
+ </a>
10
+ <a href="https://github.com/abhijithvijayan/wext-manifest-transformer/blob/main/license">
11
+ <img src="https://img.shields.io/github/license/abhijithvijayan/wext-manifest-transformer.svg" alt="LICENSE" />
12
+ </a>
13
+ <a href="https://twitter.com/intent/tweet?text=Check%20out%20wext-manifest-transformer%21%20by%20%40_abhijithv%0A%0ATransformer%20module%20for%20Webextension%20manifest.json%0Ahttps%3A%2F%2Fgithub.com%2Fabhijithvijayan%2Fwext-manifest-transformer%0A%0A%23transformer%20%23manifest%20%23javascript%20%23webextensions">
14
+ <img src="https://img.shields.io/twitter/url/http/shields.io.svg?style=social" alt="TWEET" />
15
+ </a>
16
+ </div>
17
+ <h3 align="center">🙋‍♂️ Made by <a href="https://twitter.com/_abhijithv">@abhijithvijayan</a></h3>
18
+ <p align="center">
19
+ Donate:
20
+ <a href="https://www.paypal.me/iamabhijithvijayan" target='_blank'><i><b>PayPal</b></i></a>,
21
+ <a href="https://www.patreon.com/abhijithvijayan" target='_blank'><i><b>Patreon</b></i></a>
22
+ </p>
23
+ <p align="center">
24
+ <a href='https://www.buymeacoffee.com/abhijithvijayan' target='_blank'>
25
+ <img height='36' style='border:0px;height:36px;' src='https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png' border='0' alt='Buy Me a Coffee' />
26
+ </a>
27
+ </p>
28
+ <hr />
29
+
30
+ Generate browser tailored `manifest.json` content for Web Extensions that you specify properties to appear only in specific browsers.
31
+
32
+ ❤️ it? ⭐️ it on [GitHub](https://github.com/abhijithvijayan/wext-manifest-transformer/stargazers) or [Tweet](https://twitter.com/intent/tweet?text=Check%20out%20wext-manifest-transformer%21%20by%20%40_abhijithv%0A%0ATransformer%20module%20for%20Webextension%20manifest.json%0Ahttps%3A%2F%2Fgithub.com%2Fabhijithvijayan%2Fwext-manifest-transformer%0A%0A%23transformer%20%23manifest%20%23javascript%20%23webextensions) about it.
33
+
34
+ ## Table of Contents
35
+
36
+ - [Installation](#installation)
37
+ - [Usage](#usage)
38
+ - [FAQs](#faqs)
39
+ - [Issues](#issues)
40
+ - [🐛 Bugs](#-bugs)
41
+ - [LICENSE](#license)
42
+
43
+ This loader will take a definition input for the manifest, and return you content for the specified browser.
44
+
45
+ ### Looking for Web Extension starter
46
+
47
+ Checkout [web-extension-starter](https://github.com/abhijithvijayan/web-extension-starter) that uses this package with the help of [vite-plugin-wext-manifest](https://github.com/abhijithvijayan/vite-plugin-wext-manifest) plugin.
48
+
49
+ ## Installation
50
+
51
+ Ensure you have [Node.js](https://nodejs.org) 10 or later installed. Then run the following:
52
+
53
+ ```sh
54
+ # via npm
55
+ npm install wext-manifest-transformer
56
+
57
+ # or yarn
58
+ yarn add wext-manifest-transformer
59
+ ```
60
+
61
+ ## Usage
62
+
63
+ You can easily use this module together with the [`vite-plugin-wext-manifest`](https://www.npmjs.com/package/vite-plugin-wext-manifest) to output the `manifest.json` as part of your build process with auto rebundling on file change.
64
+ This also lets you build v2 manifest & v3 manifest for different browsers from the same `manifest.json`.
65
+
66
+ #### Sample manifest with vendor prefixed keys
67
+
68
+ <https://github.com/abhijithvijayan/web-extension-starter/blob/react-typescript/source/manifest.json>
69
+
70
+ ```js
71
+ import { transformer } from 'wext-manifest-transformer';
72
+ // Or using CommonJS
73
+ // const { transformer } = require('wext-manifest-transformer');
74
+
75
+ // 1. Define your manifest with vendor-prefixed keys
76
+ const manifest = {
77
+ "name": "My Awesome Extension",
78
+ "version": "1.0",
79
+ "__chrome|opera__manifest_version": 3,
80
+ "__firefox__manifest_version": 2,
81
+ "__dev__name": "My Awesome Extension (Dev)",
82
+ "options_ui": {
83
+ "page": "options.html",
84
+ "__chrome__open_in_tab": true,
85
+ "__firefox__browser_style": true
86
+ },
87
+ "__chrome|prod__host_permissions": [
88
+ "https://*.google.com/"
89
+ ],
90
+ "__firefox|prod__host_permissions": [
91
+ "https://*.mozilla.org/"
92
+ ]
93
+ };
94
+
95
+ // 2. Transform the manifest for a specific target
96
+
97
+ // Example for Chrome in a development environment
98
+ const chromeDevManifest = transformer(manifest, 'chrome', 'development');
99
+ console.log(chromeDevManifest);
100
+ /*
101
+ Output:
102
+ {
103
+ "name": "My Awesome Extension (Dev)",
104
+ "version": "1.0",
105
+ "manifest_version": 3,
106
+ "options_ui": {
107
+ "page": "options.html",
108
+ "open_in_tab": true
109
+ }
110
+ }
111
+ */
112
+
113
+ // Example for Firefox in a production environment
114
+ const firefoxProdManifest = transformer(manifest, 'firefox', 'production');
115
+ console.log(firefoxProdManifest);
116
+ /*
117
+ Output:
118
+ {
119
+ "name": "My Awesome Extension",
120
+ "version": "1.0",
121
+ "manifest_version": 2,
122
+ "options_ui": {
123
+ "page": "options.html",
124
+ "browser_style": true
125
+ },
126
+ "host_permissions": [
127
+ "https://*.mozilla.org/"
128
+ ]
129
+ }
130
+ */
131
+ ```
132
+
133
+ <hr />
134
+
135
+ ## FAQs
136
+
137
+ ### 1.What are vendor prefixed manifest keys
138
+
139
+ Vendor prefixed manifest keys allow you to write one `manifest.json` for multiple vendors.
140
+
141
+ ```js
142
+ {
143
+ "__chrome__name": "AwesomeChrome",
144
+ "__firefox__name": "AwesomeFirefox",
145
+ "__edge__name": "AwesomeEdge",
146
+ "__opera__name": "AwesomeOpera"
147
+ }
148
+ ```
149
+
150
+ if the **TARGET_BROWSER** is `chrome` this compiles to:
151
+
152
+ ```js
153
+ {
154
+ "name": "AwesomeChrome",
155
+ }
156
+ ```
157
+
158
+ ---
159
+
160
+ Add keys to multiple vendors by seperating them with `|` in the prefix
161
+
162
+ ```
163
+ {
164
+ __chrome|opera__name: "AwesomeExtension"
165
+ }
166
+ ```
167
+
168
+ if the vendor is `chrome` or `opera`, this compiles to:
169
+
170
+ ```
171
+ {
172
+ "name": "AwesomeExtension"
173
+ }
174
+ ```
175
+
176
+ ### 2. How can I conditionally set keys based on environment
177
+
178
+ ```js
179
+ {
180
+ "__dev__name": "NameInDevelopment",
181
+ "__prod__name": "NameInProduction",
182
+ "__chrome|firefox|dev__description": "DescriptionInDevelopmentForSetOfBrowsers",
183
+ "__chrome|firefox|prod__description": "DescriptionInProductionForSetOfBrowsers"
184
+ }
185
+ ```
186
+
187
+ if the **NODE_ENV** is `production` and the **TARGET_BROWSER** is `chrome` this compiles to:
188
+
189
+ ```js
190
+ {
191
+ "name": "NameInProduction",
192
+ "description": "DescriptionInProductionForSetOfBrowsers"
193
+ }
194
+ ```
195
+
196
+ else
197
+
198
+ ```js
199
+ {
200
+ "name": "NameInDevelopment",
201
+ "description": "DescriptionInDevelopmentForSetOfBrowsers"
202
+ }
203
+ ```
204
+
205
+ ## Issues
206
+
207
+ _Looking to contribute? Look for the [Good First Issue](https://github.com/abhijithvijayan/wext-manifest-transformer/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22good+first+issue%22)
208
+ label._
209
+
210
+ ### 🐛 Bugs
211
+
212
+ Please file an issue [here](https://github.com/abhijithvijayan/wext-manifest-transformer/issues/new) for bugs, missing documentation, or unexpected behavior.
213
+
214
+ [**See Bugs**](https://github.com/abhijithvijayan/wext-manifest-transformer/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22type%3A+bug%22)
215
+
216
+ ### Linting & TypeScript Config
217
+
218
+ - Shared Eslint & Prettier Configuration - [`@abhijithvijayan/eslint-config`](https://www.npmjs.com/package/@abhijithvijayan/eslint-config)
219
+ - Shared TypeScript Configuration - [`@abhijithvijayan/tsconfig`](https://www.npmjs.com/package/@abhijithvijayan/tsconfig)
220
+
221
+ ## License
222
+
223
+ MIT © [Abhijith Vijayan](https://abhijithvijayan.in)