Skip to content

Commit 79bc991

Browse files
committed
feat: Add firefox and chrome variable in @wxt-dev/browser
1 parent 4f772fc commit 79bc991

File tree

7 files changed

+9710
-5
lines changed

7 files changed

+9710
-5
lines changed

packages/browser/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"devDependencies": {
2626
"@types/chrome": "0.0.323",
27+
"@types/firefox-webext-browser": "120.0.4",
2728
"fs-extra": "^11.3.0",
2829
"nano-spawn": "^0.2.0",
2930
"tsx": "4.19.4",

packages/browser/scripts/generate.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import { sep as posixSep } from 'node:path/posix';
66

77
// Fetch latest version
88

9-
console.log('Getting latest version of \x1b[36m@types/chrome\x1b[0m');
10-
await spawn('pnpm', ['i', '--ignore-scripts', '-D', '@types/chrome@latest']);
9+
console.log(
10+
'Getting latest version of \x1b[36m@types/chrome\x1b[0m, \x1b[36m@types/firefox-webext-browser\x1b[0m',
11+
);
12+
await spawn('pnpm', [
13+
'i',
14+
'--ignore-scripts',
15+
'-D',
16+
'@types/chrome@latest',
17+
'@types/firefox-webext-browser@latest',
18+
]);
1119

1220
// Generate new package.json
1321

@@ -16,11 +24,17 @@ console.log('Generating new \x1b[36mpackage.json\x1b[0m');
1624
const pkgJsonPath = fileURLToPath(
1725
import.meta.resolve('@types/chrome/package.json'),
1826
);
27+
const firefoxPkgJsonPath = fileURLToPath(
28+
import.meta.resolve('@types/firefox-webext-browser/package.json'),
29+
);
1930
const pkgDir = dirname(pkgJsonPath);
2031
const pkgJson = await fs.readJson(pkgJsonPath);
32+
const firefoxPkgJson = await fs.readJson(firefoxPkgJsonPath);
2133
const pkgJsonTemplate = await fs.readFile('templates/package.json', 'utf8');
2234
const newPkgJson = JSON.parse(
23-
pkgJsonTemplate.replaceAll('{{chromeTypesVersion}}', pkgJson.version),
35+
pkgJsonTemplate
36+
.replaceAll('{{chromeTypesVersion}}', pkgJson.version)
37+
.replaceAll('{{firefoxTypesVersion}}', firefoxPkgJson.version),
2438
);
2539
newPkgJson.dependencies = pkgJson.dependencies;
2640
newPkgJson.peerDependencies = pkgJson.peerDependencies;
@@ -51,7 +65,38 @@ const declarationFileMapping = (
5165

5266
for (const { file, srcPath, destPath } of declarationFileMapping) {
5367
const content = await fs.readFile(srcPath, 'utf8');
54-
const transformedContent = transformFile(file, content);
68+
const transformedContent = transformChromeFile(file, content);
69+
const destDir = dirname(destPath);
70+
await fs.mkdir(destDir, { recursive: true });
71+
await fs.writeFile(destPath, transformedContent);
72+
console.log(` \x1b[2m-\x1b[0m \x1b[36m${file}\x1b[0m`);
73+
}
74+
// Generate Firefox declaration files
75+
console.log('Generating \x1b[36mfirefox\x1b[0m declaration files');
76+
77+
const firefoxPkgDir = dirname(firefoxPkgJsonPath);
78+
const firefoxDeclarationFileMapping = (
79+
await fs.readdir(firefoxPkgDir, {
80+
recursive: true,
81+
encoding: 'utf8',
82+
})
83+
)
84+
// Filter to .d.ts files
85+
.filter((file) => file.endsWith('.d.ts'))
86+
// Map to usable paths
87+
.map((file) => ({
88+
file: file.includes('index')
89+
? file.replaceAll(sep, posixSep).replace('index', 'firefox')
90+
: file.replaceAll(sep, posixSep),
91+
srcPath: join(firefoxPkgDir, file),
92+
destPath: file.includes('index')
93+
? join(outDir, 'firefox.d.ts')
94+
: join(outDir, file),
95+
}));
96+
97+
for (const { file, srcPath, destPath } of firefoxDeclarationFileMapping) {
98+
const content = await fs.readFile(srcPath, 'utf8');
99+
const transformedContent = transformFirefoxFile(file, content);
55100
const destDir = dirname(destPath);
56101
await fs.mkdir(destDir, { recursive: true });
57102
await fs.writeFile(destPath, transformedContent);
@@ -66,7 +111,7 @@ console.log(
66111

67112
// Transformations
68113

69-
function transformFile(file: string, content: string): string {
114+
function transformChromeFile(file: string, content: string): string {
70115
return (
71116
// Add prefix
72117
`/* DO NOT EDIT - generated by scripts/generate.ts */\n\n${content}\n`
@@ -80,3 +125,13 @@ function transformFile(file: string, content: string): string {
80125
.replaceAll('developer.Browser.com', 'developer.chrome.com')
81126
);
82127
}
128+
function transformFirefoxFile(file: string, content: string): string {
129+
return (
130+
// Add prefix
131+
`/* DO NOT EDIT - generated by scripts/generate.ts */\n\n${content}\n`
132+
// Rename `browser` namespace to `Firefox` and export it
133+
.replaceAll('declare namespace browser', 'export namespace Firefox')
134+
// Update references to `browser` namespace to `Firefox`
135+
.replaceAll('browser.', 'Firefox.')
136+
);
137+
}

0 commit comments

Comments
 (0)