Skip to content

Commit

Permalink
chore: generate manifest.json version field from package.json (anoma#478
Browse files Browse the repository at this point in the history
)

Changes injected and content scripts to read the version from
package.json instead of src/manifest/_base.json.
  • Loading branch information
emccorson committed Nov 29, 2023
1 parent e27ce10 commit b464819
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"leb128": "^0.0.5",
"merge-jsons-webpack-plugin": "^2.0.1",
"mockzilla": "^0.14.0",
"remove-files-webpack-plugin": "^1.5.0",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"ts-loader": "^9.3.1",
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "extension";
import { KVPrefix, Ports } from "router/types";
import { initEvents } from "./events";
import manifest from "manifest/_base.json";
import { ExtensionKVStore } from "@namada/storage";

const extensionStore = new ExtensionKVStore(KVPrefix.LocalStorage, {
Expand All @@ -33,8 +32,9 @@ const approvedOriginsStore = new ExtensionKVStore(KVPrefix.LocalStorage, {
const init = new Promise<void>(async (resolve) => {
// Start proxying messages from Namada to InjectedNamada
const routerId = await getNamadaRouterId(extensionStore);
const packageVersion = process.env.npm_package_version || "";
Proxy.start(
new Namada(manifest.version, new ExtensionRequester(messenger, routerId)),
new Namada(packageVersion, new ExtensionRequester(messenger, routerId)),
approvedOriginsStore
);
resolve();
Expand Down
6 changes: 3 additions & 3 deletions apps/extension/src/content/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// webpack bundle webextension-polyfill, which causes a runtime crash
import { InjectedNamada } from "provider/InjectedNamada";

import manifest from "manifest/_base.json";

declare global {
// NOTE: var is required to extend globalThis
// eslint-disable-next-line
Expand All @@ -20,4 +18,6 @@ export function init(namada: InjectedNamada): void {
}
}

init(new InjectedNamada(manifest.version));
const packageVersion = process.env.npm_package_version || "";

init(new InjectedNamada(packageVersion));
3 changes: 1 addition & 2 deletions apps/extension/src/manifest/_base.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"name": "Namada Extension",
"description": "The Namada Extension manages a user's wallet for the Namada ecosystem.",
"version": "0.1.0"
"description": "The Namada Extension manages a user's wallet for the Namada ecosystem."
}
29 changes: 27 additions & 2 deletions apps/extension/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@ const { resolve } = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const MergeJsonWebpackPlugin = require("merge-jsons-webpack-plugin");
const ExtensionReloader = require("webpack-extension-reloader");
const RemovePlugin = require('remove-files-webpack-plugin');
const createStyledComponentsTransformer =
require("typescript-plugin-styled-components").default;
const packageJson = require("./package.json");

// Load .env from namada-interface:
require("dotenv").config({ path: "../namada-interface/.env" });

const { NODE_ENV, TARGET } = process.env;

const OUTPUT_PATH = resolve(__dirname, `./build/${TARGET}`);

const MANIFEST_VERSION = TARGET === "firefox" ? "v2" : "v3";
const MANIFEST_BASE_PATH = `./src/manifest/_base.json`;
const MANIFEST_BASE_VERSION_PATH = `./src/manifest/${MANIFEST_VERSION}/_base.json`;
const MANIFEST_PATH = `./src/manifest/${MANIFEST_VERSION}/${TARGET}.json`;
const MANIFEST_V2_DEV_ONLY_PATH = `./src/manifest/v2/_devOnly.json`;
const GENERATED_MANIFEST = "generatedManifest.json";

// Sets manifest.json fields at build time
function generateManifest(buffer) {
const manifest = JSON.parse(buffer.toString());

manifest.version = packageJson.version;

return JSON.stringify(manifest, null, 2);
}

const copyPatterns = [
{
Expand Down Expand Up @@ -47,6 +61,11 @@ const copyPatterns = [
from: "../../node_modules/webextension-polyfill/dist/browser-polyfill.js.map",
to: "./browser-polyfill.js.map",
},
{
from: MANIFEST_BASE_PATH,
to: GENERATED_MANIFEST,
transform: generateManifest
}
];

const plugins = [
Expand All @@ -55,7 +74,7 @@ const plugins = [
}),
new MergeJsonWebpackPlugin({
files: [
MANIFEST_BASE_PATH,
GENERATED_MANIFEST,
MANIFEST_BASE_VERSION_PATH,
MANIFEST_PATH,
...(NODE_ENV === "development" && TARGET === "firefox"
Expand All @@ -75,6 +94,12 @@ const plugins = [
env: JSON.stringify(process.env),
},
}),
new RemovePlugin({
after: {
include: [`${OUTPUT_PATH}/${GENERATED_MANIFEST}`],
log: false
},
})
];

if (NODE_ENV === "development") {
Expand Down Expand Up @@ -111,7 +136,7 @@ module.exports = {
},
output: {
publicPath: "",
path: resolve(__dirname, `./build/${TARGET}`),
path: OUTPUT_PATH,
filename: "[name].namada.js",
},
module: {
Expand Down
Loading

0 comments on commit b464819

Please sign in to comment.