1
0
mirror of https://github.com/bitwarden/browser synced 2024-12-28 10:51:42 +01:00
bitwarden-estensione-browser/apps/browser/webpack/manifest.js
Oscar Hinton b55a28f755
[PM-14485] Remove manifest and index.html logic from gulp (#12033)
Refactor the remaining logic from gulp.  Part of the browser build script
refactor effort.

Webpack is now responsible for performing most of the operations previously
done by gulp. This includes: - Setting browser specific class - Building the
manifest file  The `package.json` is modified to include browser specific
commands for `build`, `build:prod`, `build:watch` and `dist`.

# Manifests

Manifests now uses the `copy-webpack-plugin` `transform` feature. The logic is
located in `apps/browser/webpack/manifest.js`. It reads a template, which
supports some basic operations primarily overriding with browser specific
fields using `__browser__`.  The `manifest.json` for both regular and mv3
builds are identical to our existing manifests except:
- `applications` renamed to `browser_specific_settings`.
- `permissions` sorted alphabetically.

# Safari build

Safari requires additional packaging commands. This is implemented as a
powershell script due to the cross-platform nature, and since we generally
require powershell in our distribution pipelines. An alternative would be to
write it in bash, but bash is less powerful and would require some additional
commands like `jq`. Another alternative is to write it using js, but that would
require additional dependencies.
2024-11-19 13:25:30 +00:00

73 lines
1.6 KiB
JavaScript

/**
* Transform the manifest template into a browser specific manifest.
*
* We support a simple browser prefix to the manifest keys. Example:
*
* ```json
* {
* "name": "Default name",
* "__chrome__name": "Chrome override"
* }
* ```
*
* Will result in the following manifest:
*
* ```json
* {
* "name": "Chrome override"
* }
* ```
*
* for Chrome.
*/
function transform(browser) {
return (buffer) => {
let manifest = JSON.parse(buffer.toString());
manifest = transformPrefixes(manifest, browser);
return JSON.stringify(manifest, null, 2);
};
}
const browsers = ["chrome", "edge", "firefox", "opera", "safari"];
/**
* Flatten the browser prefixes in the manifest.
*
* - Removes unrelated browser prefixes.
* - A null value deletes the non prefixed key.
*/
function transformPrefixes(manifest, browser) {
const prefix = `__${browser}__`;
function transformObject(obj) {
return Object.keys(obj).reduce((acc, key) => {
// Determine if we need to recurse into the object.
const nested = typeof obj[key] === "object" && obj[key] !== null && !Array.isArray(obj[key]);
if (key.startsWith(prefix)) {
const newKey = key.slice(prefix.length);
// Null values are used to remove keys.
if (obj[key] == null) {
delete acc[newKey];
return acc;
}
acc[newKey] = nested ? transformObject(obj[key]) : obj[key];
} else if (!browsers.some((b) => key.startsWith(`__${b}__`))) {
acc[key] = nested ? transformObject(obj[key]) : obj[key];
}
return acc;
}, {});
}
return transformObject(manifest);
}
module.exports = {
transform,
};