Skip to content

Commit

Permalink
Improvements and fixes for mf format
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Dec 4, 2023
1 parent f15845d commit 7d4435e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/specs/cli-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ System.register([],function(e,c){var dep;return{setters:[function(_dep){dep = _d

The `$pr_name` has to be replaced with the globally used name for other (lazy loaded) chunks of the pilet. The `$shared_dependencies` represent the used shared dependencies. This is a JSON object mapping the identifiers used for the shared dependencies to their bundles.

**`mf`**

The bundled code follows the specification and implementation of Module Federation. While originated at Webpack ("Webpack Module Federation") it is now also ported over to other bundlers. In supported bundlers the `mf` format can be used to produce compatible pilets.

Pilets that are compatible must have:

- Exposed entry point `./pilet` leading to the module with a `setup` function
- Accepting a share scope with name `default` (this is also the default, so no changes required here)

## Limitations

The specification does not cover things like validation, declaration generation, scaffolding, or upgrading.
Expand Down
8 changes: 7 additions & 1 deletion docs/specs/pilet-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ The script for the partial resource has to be loaded from the same base URL as t

### `v2` Format

This code in this section works exclusively for `v2` pilets. `v2` pilets are using the [SystemJS format](https://github.com/systemjs/systemjs/blob/main/docs/system-register.md). For conformance to this format the SystemJS specification should be considered.
The code in this section works exclusively for `v2` pilets. `v2` pilets are using the [SystemJS format](https://github.com/systemjs/systemjs/blob/main/docs/system-register.md). For conformance to this format the SystemJS specification should be considered.

### `mf` Format

The code in this section works exclusively for `mf` pilets. `mf` pilets are using [Module Federation](https://module-federation.io/) for exposing their API and taking part in (distributed) sharing of dependencies. For conformance to this format the Module Federation specification should be considered.

The `mf` format does not require a spec version marker. By detecting a script that uses module federation the `mf` format is assumed.

## Package Keys

Expand Down
9 changes: 9 additions & 0 deletions src/tooling/piral-cli/src/common/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export function getPiletSpecMeta(target: string, basePath: string) {
requireRef,
dependencies: getDependencies(plainDependencies, basePath),
};
} else if (
content.includes(
'"Container initialization failed as it has already been initialized with a different share scope"',
) &&
/^var [A-Za-z0-9_]+;/.test(content)
) {
return {
spec: 'mf',
};
} else {
// uses no arguments
return {
Expand Down
7 changes: 6 additions & 1 deletion src/tooling/piral-cli/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2628,11 +2628,12 @@ export function failedToOpenBrowser_0170(error: string): QuickMessage {
* the interpretation of compatible feed services slightly and has an impact of the usage
* of the pilet in the browser.
*
* The selected schema version needs to be either "v0", "v1", or "v2".
* The selected schema version needs to be either "v0", "v1", "v2", or "mf".
*
* - v0: will download and evaluate the pilet explicitly
* - v1: will use a script tag for integration of the pilet
* - v2: will use SystemJS for integration of the pilet (default)
* - mf: will use Module Federation for integration of the pilet (only supported bundlers)
*
* The v1 version has better support for older browsers, but requires a polyfill to work
* correctly. This polyfill is part of the `piral-ie11polyfills-utils` package.
Expand All @@ -2641,6 +2642,10 @@ export function failedToOpenBrowser_0170(error: string): QuickMessage {
* The v2 version uses a SystemJS format for the pilet. It has the broadest browser support
* but requires the custom format as output. Most bundlers support SystemJS directly or
* indirectly, making it a quite broad choice.
*
* In bundlers that support Module Federation (e.g., Webpack 5) the "mf" format may be
* the best choice. Keep in mind that "mf" is only supported by applications using
* Piral 1.4.0 or higher.
*
* @see
* - [GitHub currentScript-polyfill](https://github.com/amiller-gh/currentScript-polyfill)
Expand Down

0 comments on commit 7d4435e

Please sign in to comment.