Skip to content

Commit

Permalink
feature/#52-compile-plugin-with-webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo committed Sep 18, 2023
1 parent 3476f1d commit 22dacaa
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion web-client/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const createMainWindow = () => {
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, "preload.js"),
// contextIsolation: false,
enableRemoteModule: true,
},
});
const startURL = isDev
Expand Down
59 changes: 59 additions & 0 deletions web-client/electron/plugins/base-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import axios from "axios";

// Provide menu item as plain object
const newItem = { text: "Item from object" };

// Provide menu item as callback.
// The parent that is provided by the extension point is returned in the object
const newChildItem = (parent) => ({
parent,
text: "Child item from function",
});

// Provide menu item as async callback
// Does the same as the previous function otherwise
const asyncChildItem = (parent) =>
new Promise((resolve) => {
setTimeout(
() =>
resolve({
parent,
text: "Child item from async function",
}),
500
);
});
// Provide an async method to manipulate the price provided by the extension point
const addVat = (price) =>
new Promise(async (resolve) => {
const vat = 1.2;
setTimeout(() => resolve(price * vat), 1);
axios
.get("https://jsonplaceholder.typicode.com/todos/1")
.then((response) => {
console.log(response);
});
});

// Provide a synchronous method to manipulate the price provided by the extension point
const addDelivery = (price) => price + 5;

// Append in img element to the img-viewer div with the provided url as source.
// The url is provided by the extension point.
const displayImg = (url) => {
const img = document.createElement("img");
img.setAttribute("src", url);
img.setAttribute("width", "100%");
document.getElementById("img-viewer").appendChild(img);
};
// Register all the above functions and objects with the relevant extension points
export function init({ register }) {
register("extend-menu", "newItem", newItem);
register("extend-menu", "newChildItem", newChildItem);
register("extend-menu", "asyncChildItem", asyncChildItem);
// Notice that for the calc-price extension point, the addVat function is executed
// before the addDelivery because it has a lower priority
register("calc-price", "addVat", addVat, 1);
register("calc-price", "addDelivery", addDelivery, 10);
register("display-img", "displayImg", displayImg);
}
29 changes: 29 additions & 0 deletions web-client/electron/plugins/base-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "demoplugin",
"version": "2.1.0",
"description": "",
"main": "dist/bundle.js",
"author": "Igor Honhoff",
"license": "MIT",
"activationPoints": [
"init"
],
"scripts": {
"prepare": "webpack --config webpack.config.js",
"package": "rimraf ./demoplugin*.tgz && npm pack"
},
"devDependencies": {
"rimraf": "^3.0.2",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"axios": "^1.5.0",
"sql.js": "^1.8.0"
},
"files": [
"dist/*",
"package.json",
"README.md"
]
}
15 changes: 15 additions & 0 deletions web-client/electron/plugins/base-plugin/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const path = require("path");

module.exports = {
experiments: { outputModule: true },
entry: "./index.js", // Adjust the entry point to match your project's main file
output: {
filename: "bundle.js", // Adjust the output file name as needed
path: path.resolve(__dirname, "dist"),
library: { type: "module" }, // Specify ESM output format
},
resolve: {
extensions: [".js"],
},
// Add loaders and other configuration as needed for your project
};

0 comments on commit 22dacaa

Please sign in to comment.