|
| 1 | +######################################## |
| 2 | +How to create an external plugin UI slot |
| 3 | +######################################## |
| 4 | + |
| 5 | +To create a new plugin that will live outside the MFE host, it will be in npm. The plugin needs to be compiled by `Babel <https://babeljs.io/>`_, that way webpack will be available to read the plugin. Here's an example for a small plugin: |
| 6 | + |
| 7 | + |
| 8 | +**1. Plugin Structure:** |
| 9 | + |
| 10 | +The plugin will have the following structure: |
| 11 | + |
| 12 | + - root |
| 13 | + - package.json |
| 14 | + - .babelrc |
| 15 | + - Makefile |
| 16 | + - src |
| 17 | + - index.jsx |
| 18 | + - index.css |
| 19 | + |
| 20 | + |
| 21 | +**Example files:** |
| 22 | + |
| 23 | +**1.1 package.json:** |
| 24 | + |
| 25 | +This file contains the necessary dependencies for the plugin and also a Babel script to transpile the files inside the ``src`` folder of the plugin. |
| 26 | + |
| 27 | +.. code-block:: json |
| 28 | +
|
| 29 | + { |
| 30 | + "name": "@openedx-plugins/example-plugin", |
| 31 | + "version": "1.0.0", |
| 32 | + "scripts": { |
| 33 | + "babel:compile": "babel src --out-dir dist --source-maps --ignore '**/*.test.jsx,**/*.test.js' --copy-files" |
| 34 | + }, |
| 35 | + "peerDependencies": { |
| 36 | + "@edx/browserslist-config": "^1.2.0", |
| 37 | + "@edx/frontend-app-communications": "https://github.com/eduNEXT/frontend-app-communications.git#jv/pluggable-component-slot", |
| 38 | + "@edx/frontend-build": "13.0.1", |
| 39 | + "@edx/frontend-platform": "5.5.2", |
| 40 | + "@edx/paragon": "^20.44.0", |
| 41 | + "eslint-import-resolver-webpack": "^0.13.8", |
| 42 | + "react": "17.0.2", |
| 43 | + "prop-types": "^15.8.1", |
| 44 | + "react-dom": "17.0.2" |
| 45 | + }, |
| 46 | + "peerDependenciesMeta": { |
| 47 | + "@edx/frontend-app-communications": { |
| 48 | + "optional": true |
| 49 | + } |
| 50 | + }, |
| 51 | + "devDependencies": { |
| 52 | + "@babel/cli": "^7.23.4", |
| 53 | + "@babel/core": "^7.23.7", |
| 54 | + "@babel/eslint-parser": "^7.23.3", |
| 55 | + "@babel/plugin-proposal-class-properties": "^7.18.6", |
| 56 | + "@babel/plugin-proposal-object-rest-spread": "^7.20.7", |
| 57 | + "@babel/preset-env": "^7.23.8", |
| 58 | + "@babel/preset-react": "^7.23.3", |
| 59 | + "@edx/browserslist-config": "^1.2.0", |
| 60 | + "@edx/frontend-build": "13.0.1" |
| 61 | + } |
| 62 | + } |
| 63 | +
|
| 64 | +**1.2. .babelrc:** |
| 65 | + |
| 66 | +The essential configuration for Babel. This configuration works for any plugin. You could set it up if you need it. |
| 67 | + |
| 68 | +.. code-block:: json |
| 69 | +
|
| 70 | + { |
| 71 | + "presets": [ |
| 72 | + [ |
| 73 | + "@babel/preset-env", |
| 74 | + { |
| 75 | + "modules": false |
| 76 | + } |
| 77 | + ], |
| 78 | + "@babel/preset-react" |
| 79 | + ], |
| 80 | + "plugins": [ |
| 81 | + "@babel/plugin-proposal-object-rest-spread", |
| 82 | + "@babel/plugin-proposal-class-properties" |
| 83 | + ], |
| 84 | + "env": { |
| 85 | + "test": { |
| 86 | + "plugins": ["@babel/plugin-proposal-class-properties"], |
| 87 | + "presets": [["@babel/preset-env"], "@babel/preset-react"] |
| 88 | + } |
| 89 | + }, |
| 90 | + "ignore": ["**/*.test.jsx", "**/*.test.js"] |
| 91 | + } |
| 92 | +
|
| 93 | +
|
| 94 | +**1.3. Makefile:** |
| 95 | + |
| 96 | +This will create a folder called ``package`` that will have the compiled Babel files ready to upload to, for example, npm. |
| 97 | + |
| 98 | +.. code-block:: makefile |
| 99 | +
|
| 100 | + .ONESHELL: clean build |
| 101 | +
|
| 102 | + clean: |
| 103 | + # Remove the package folder if it exists |
| 104 | + rm -rf package |
| 105 | + rm -rf dist |
| 106 | +
|
| 107 | + build: clean |
| 108 | + # Run npm run babel |
| 109 | + npm run babel:compile |
| 110 | +
|
| 111 | + # Create the package folder |
| 112 | + mkdir -p package |
| 113 | +
|
| 114 | + # Copy package.json to the package folder |
| 115 | + cp package.json package/ |
| 116 | +
|
| 117 | + # Move files from dist folder to package folder |
| 118 | + find dist -type f ! -name "*.map" ! -name "*.test.js" ! -name "*.test.jsx" -exec cp {} package/ \; |
| 119 | +
|
| 120 | + # Rename index.js to index.jsx in the package folder |
| 121 | + rm -rf package/index.js |
| 122 | + cp dist/index.js package/index.jsx |
| 123 | +
|
| 124 | +
|
| 125 | +**1.4. index.jsx:** |
| 126 | + |
| 127 | +This will be the content of the plugin. In this case, it's a simple div. |
| 128 | + |
| 129 | +.. code-block:: jsx |
| 130 | +
|
| 131 | + import React from 'react'; |
| 132 | + |
| 133 | + import './index.css'; |
| 134 | + |
| 135 | + const MyPlugin = () => { |
| 136 | + return ( |
| 137 | + <div className="openedx-plugin"> |
| 138 | + <h1>Hello, World!</h1> |
| 139 | + {/* Add your plugin UI components here */} |
| 140 | + </div> |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + export default MyPlugin; |
| 145 | +
|
| 146 | +**1.5. index.css:** |
| 147 | + |
| 148 | +Styles for the plugin. |
| 149 | + |
| 150 | +.. code-block:: css |
| 151 | +
|
| 152 | + .openedx-plugin { |
| 153 | + background-color: red; |
| 154 | + color: white; |
| 155 | + } |
| 156 | +
|
| 157 | +
|
| 158 | +**2. Install Dependencies:** |
| 159 | + |
| 160 | +After having the plugin structure, you can install the dependencies: |
| 161 | + |
| 162 | +.. code-block:: bash |
| 163 | +
|
| 164 | + npm install |
| 165 | +
|
| 166 | +
|
| 167 | +**3. Compile the Plugin:** |
| 168 | + |
| 169 | +Now you can compile the plugin by running the Makefile. This will create a folder called ``package`` with the necessary compiled files. Run the following command: |
| 170 | + |
| 171 | +.. code-block:: bash |
| 172 | +
|
| 173 | + make build |
| 174 | +
|
| 175 | +Notice that inside the ``package`` folder, there will always be a file called ``index.jsx`` and not ``index.js``. This ensures that when loading components, lazy loading won't have any problems. |
| 176 | + |
| 177 | + |
| 178 | +**4. Upload the Plugin:** |
| 179 | + |
| 180 | +Now that you have compiled the plugin, you can upload it to npm for example. Inside the ``package`` folder you can publish the plugin from there |
0 commit comments