2022-02-06 03:20:53 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2020-02-04 07:32:58 +01:00
|
|
|
const webpack = require("webpack");
|
|
|
|
const path = require('path');
|
2022-02-06 03:20:53 +01:00
|
|
|
const { validate } = require('schema-utils');
|
2020-02-04 07:32:58 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const manifest = require("../manifest/manifest.json");
|
|
|
|
const firefoxManifestExtra = require("../manifest/firefox-manifest-extra.json");
|
|
|
|
const chromeManifestExtra = require("../manifest/chrome-manifest-extra.json");
|
2022-01-15 03:51:42 +01:00
|
|
|
const safariManifestExtra = require("../manifest/safari-manifest-extra.json");
|
2020-02-20 17:39:06 +01:00
|
|
|
const betaManifestExtra = require("../manifest/beta-manifest-extra.json");
|
2020-02-24 02:49:17 +01:00
|
|
|
const firefoxBetaManifestExtra = require("../manifest/firefox-beta-manifest-extra.json");
|
2024-06-19 19:44:27 +02:00
|
|
|
const manifestV2ManifestExtra = require("../manifest/manifest-v2-extra.json");
|
2020-02-04 07:32:58 +01:00
|
|
|
|
|
|
|
// schema for options object
|
|
|
|
const schema = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
browser: {
|
|
|
|
type: 'string'
|
|
|
|
},
|
|
|
|
pretty: {
|
|
|
|
type: 'boolean'
|
2020-02-20 17:39:06 +01:00
|
|
|
},
|
|
|
|
steam: {
|
|
|
|
type: 'string'
|
2020-02-04 07:32:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BuildManifest {
|
|
|
|
constructor (options = {}) {
|
2022-02-06 03:20:53 +01:00
|
|
|
validate(schema, options, "Build Manifest Plugin");
|
2020-02-04 07:32:58 +01:00
|
|
|
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
2022-02-06 03:20:53 +01:00
|
|
|
apply() {
|
2020-02-04 07:48:32 +01:00
|
|
|
const distFolder = path.resolve(__dirname, "../dist/");
|
|
|
|
const distManifestFile = path.resolve(distFolder, "manifest.json");
|
2020-02-04 07:32:58 +01:00
|
|
|
|
|
|
|
// Add missing manifest elements
|
|
|
|
if (this.options.browser.toLowerCase() === "firefox") {
|
2024-06-19 19:44:27 +02:00
|
|
|
mergeObjects(manifest, manifestV2ManifestExtra);
|
2020-02-04 07:32:58 +01:00
|
|
|
mergeObjects(manifest, firefoxManifestExtra);
|
2022-01-15 03:51:42 +01:00
|
|
|
} else if (this.options.browser.toLowerCase() === "chrome"
|
|
|
|
|| this.options.browser.toLowerCase() === "chromium"
|
|
|
|
|| this.options.browser.toLowerCase() === "edge") {
|
2020-02-04 07:32:58 +01:00
|
|
|
mergeObjects(manifest, chromeManifestExtra);
|
2022-01-15 03:51:42 +01:00
|
|
|
} else if (this.options.browser.toLowerCase() === "safari") {
|
2024-06-19 19:44:27 +02:00
|
|
|
mergeObjects(manifest, manifestV2ManifestExtra);
|
2022-01-15 03:51:42 +01:00
|
|
|
mergeObjects(manifest, safariManifestExtra);
|
2020-02-04 07:32:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:39:06 +01:00
|
|
|
if (this.options.stream === "beta") {
|
|
|
|
mergeObjects(manifest, betaManifestExtra);
|
2020-02-24 02:49:17 +01:00
|
|
|
|
|
|
|
if (this.options.browser.toLowerCase() === "firefox") {
|
|
|
|
mergeObjects(manifest, firefoxBetaManifestExtra);
|
|
|
|
}
|
2020-02-20 17:39:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 07:32:58 +01:00
|
|
|
let result = JSON.stringify(manifest);
|
|
|
|
if (this.options.pretty) result = JSON.stringify(manifest, null, 2);
|
|
|
|
|
2020-02-04 07:48:32 +01:00
|
|
|
fs.mkdirSync(distFolder, {recursive: true});
|
2020-02-04 07:32:58 +01:00
|
|
|
fs.writeFileSync(distManifestFile, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeObjects(object1, object2) {
|
|
|
|
for (const key in object2) {
|
|
|
|
if (key in object1) {
|
|
|
|
if (Array.isArray(object1[key])) {
|
|
|
|
object1[key] = object1[key].concat(object2[key]);
|
|
|
|
} else if (typeof object1[key] == 'object') {
|
|
|
|
mergeObjects(object1[key], object2[key]);
|
|
|
|
} else {
|
|
|
|
object1[key] = object2[key];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
object1[key] = object2[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BuildManifest;
|