|
| 1 | +--- |
| 2 | +html_meta: |
| 3 | + "description": "How to use Module Federation in Mockup and add-on bundles." |
| 4 | + "property=og:description": "How to use Module Federation in Mockup and add-on bundles." |
| 5 | + "property=og:title": "Module Federation in Mockup" |
| 6 | + "keywords": "Plone, Classic UI, classic-ui, Mockup, Module Federation, webpack, JavaScript" |
| 7 | +--- |
| 8 | + |
| 9 | +(classic-ui-module-federation-in-mockup-label)= |
| 10 | + |
| 11 | +# Module Federation in Mockup |
| 12 | + |
| 13 | +Module Federation allows sharing of dependencies between bundles. |
| 14 | +Each bundle includes the whole set of dependencies. |
| 15 | +However, if multiple bundles have the same dependencies, then they are loaded only once. |
| 16 | + |
| 17 | +For example, if bundle A and B both depend on jQuery and bundle A has already loaded it, then bundle B can just reuse the already loaded jQuery file. |
| 18 | +But if only bundle B is loaded, it uses its own bundled version of the jQuery library. |
| 19 | + |
| 20 | +There is a host bundle, as in the fictional example above, our bundle A. |
| 21 | +In Plone the host bundle is the main Mockup bundle. |
| 22 | +Add-ons can add bundles called "remotes" which are initialized for Module Federation by the host bundle. |
| 23 | + |
| 24 | +```{seealso} |
| 25 | +Webpack's documentation on [Module Federation](https://webpack.js.org/concepts/module-federation/). |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Use Module Federation |
| 30 | + |
| 31 | +If you created an add-on with a Mockup pattern, and you want to include the respective JavaScript code in your theme code, then the following instructions are for you. |
| 32 | + |
| 33 | +Starting with the webpack configuration that you get when creating a Barceloneta theme package via [`plonecli`](https://pypi.org/project/plonecli/), add the following. |
| 34 | + |
| 35 | +Create a new entry point {file}`index.js` which only imports the normal entry point. |
| 36 | + |
| 37 | +```js |
| 38 | +import("./patterns"); |
| 39 | +``` |
| 40 | + |
| 41 | +Next add the Module Federation plugin in {file}`webpack.config.js`. |
| 42 | +There is a configuration factory `mf_config` which you can use for that. |
| 43 | +Add the following line near the top of the file. |
| 44 | + |
| 45 | +```js |
| 46 | +const mf_config = require("@patternslib/dev/webpack/webpack.mf"); |
| 47 | +``` |
| 48 | + |
| 49 | +Import all the dependencies you want to share. |
| 50 | +Potentially these are the ones from [Patternslib](https://github.com/Patternslib/Patterns/blob/master/package.json), Mockup, and your own dependencies. |
| 51 | +You can add the Patternslib and Mockup dependencies, even if you are not using them. |
| 52 | + |
| 53 | +```js |
| 54 | +const package_json = require("./package.json"); |
| 55 | +const package_json_mockup = require("@plone/mockup/package.json"); |
| 56 | +const package_json_patternslib = require("@patternslib/patternslib/package.json"); |
| 57 | +``` |
| 58 | + |
| 59 | +Then find the following line. |
| 60 | + |
| 61 | +```js |
| 62 | + config = patternslib_config(env, argv, config, ["@plone/mockup"]); |
| 63 | +``` |
| 64 | + |
| 65 | +Below this line add the following. |
| 66 | + |
| 67 | +```js |
| 68 | + config.plugins.push( |
| 69 | + mf_config({ |
| 70 | + name: "myaddon", |
| 71 | + filename: "myaddon-remote.min.js", |
| 72 | + remote_entry: config.entry["myaddon.min"], |
| 73 | + dependencies: { |
| 74 | + ...package_json_patternslib.dependencies, |
| 75 | + ...package_json_mockup.dependencies, |
| 76 | + ...package_json.dependencies, |
| 77 | + }, |
| 78 | + }) |
| 79 | + ); |
| 80 | +``` |
| 81 | + |
| 82 | +Replace the name `myaddon` with your add-on bundle's unique name. |
| 83 | +Replace the file name {file}`myaddon-remote.min.js` with the file name you want to use for your remote bundle. |
| 84 | +Finally replace `myaddon.min` with the corresponding key in `config.entry` that points to your {file}`index.js`. |
| 85 | + |
| 86 | +For a full and basic example, see the Patterns generator [pat-PATTERN-TEMPLATE](https://github.com/Patternslib/pat-PATTERN_TEMPLATE/blob/master/webpack.config.js) or any other Pattern add-on in the [patternslib GitHub organization](https://github.com/patternslib/). |
| 87 | +For a complex example with Mockup integration see [`plone.app.mosaic`](https://github.com/plone/plone.app.mosaic/blob/master/webpack.config.js) and [Mockup](https://github.com/plone/mockup/blob/master/webpack.config.js) itself. |
| 88 | + |
| 89 | + |
| 90 | +## Special case: global modules `jQuery` and `Bootstrap` |
| 91 | + |
| 92 | +To preserve compatibility with older add-ons and JavaScript implementations, the modules `jQuery` and `Bootstrap` are stored in the global `window` namespace. |
| 93 | +Constructs like the following still work: |
| 94 | + |
| 95 | +```js |
| 96 | + (function($) { |
| 97 | + // JS code which uses $ |
| 98 | + })(jQuery); |
| 99 | +``` |
0 commit comments