-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/#52-compile-plugin-with-webpack
- Loading branch information
1 parent
3476f1d
commit 22dacaa
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |