Skip to content

Commit

Permalink
Rewrite to ImportMapMicrofrontendUtils class (#2)
Browse files Browse the repository at this point in the history
* Rewrite to class

* Documentation
  • Loading branch information
joeldenning authored Oct 17, 2024
1 parent df24b2c commit 4255f57
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# import-map-microfrontend-utils

Utility types and functions for import-map-microfrontend related projects

## Installation

```sh
npm i @single-spa/import-map-microfrontend-utils
```

## Usage

```ts
import { ImportMapMicrofrontendUtils } from "@single-spa/import-map-microfrontend-utils";

const utils = new ImportMapMicrofrontendUtils({
baseOrigin: "https://cdn.example.com",
});

utils.getMicrofrontendURLPrefix("@org/navbar");
utils.getDependenciesURLPrefix();
utils.getDependenciesFolderName();
utils.getMFEsFolderName();
```
14 changes: 8 additions & 6 deletions src/__snapshots__/import-map-microfrontend-utils.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`microfrontendNameToFolderName can process scoped packages 1`] = `"org-root-config"`;
exports[`getDependenciesURLPrefix returns url with trailing slash 1`] = `"https://cdn.example.com/deps/"`;

exports[`microfrontendNameToFolderName can process scoped packages 2`] = `"org-name-navbar"`;
exports[`getMicrofrontendURLPrefix can process scoped packages 1`] = `"https://cdn.example.com/mfes/org-root-config/"`;

exports[`microfrontendNameToFolderName can process scoped packages 3`] = `"org_name-left_nav"`;
exports[`getMicrofrontendURLPrefix can process scoped packages 2`] = `"https://cdn.example.com/mfes/org-name-navbar/"`;

exports[`microfrontendNameToFolderName can process unscoped packages 1`] = `"root-config"`;
exports[`getMicrofrontendURLPrefix can process scoped packages 3`] = `"https://cdn.example.com/mfes/org_name-left_nav/"`;

exports[`microfrontendNameToFolderName can process unscoped packages 2`] = `"navbar"`;
exports[`getMicrofrontendURLPrefix can process unscoped packages 1`] = `"https://cdn.example.com/mfes/root-config/"`;

exports[`microfrontendNameToFolderName can process unscoped packages 3`] = `"left_nav"`;
exports[`getMicrofrontendURLPrefix can process unscoped packages 2`] = `"https://cdn.example.com/mfes/navbar/"`;

exports[`getMicrofrontendURLPrefix can process unscoped packages 3`] = `"https://cdn.example.com/mfes/left_nav/"`;
35 changes: 27 additions & 8 deletions src/import-map-microfrontend-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { describe, expect, it } from "@jest/globals";
import { microfrontendNameToFolderName } from "./import-map-microfrontend-utils";
import { ImportMapMicrofrontendUtils } from "./import-map-microfrontend-utils";

describe(`microfrontendNameToFolderName`, () => {
describe(`getMicrofrontendURLPrefix`, () => {
it(`can process unscoped packages`, () => {
expect(microfrontendNameToFolderName("root-config")).toMatchSnapshot();
expect(microfrontendNameToFolderName("navbar")).toMatchSnapshot();
expect(microfrontendNameToFolderName("left_nav")).toMatchSnapshot();
const utils = new ImportMapMicrofrontendUtils({
baseOrigin: "https://cdn.example.com",
});
expect(utils.getMicrofrontendURLPrefix("root-config")).toMatchSnapshot();
expect(utils.getMicrofrontendURLPrefix("navbar")).toMatchSnapshot();
expect(utils.getMicrofrontendURLPrefix("left_nav")).toMatchSnapshot();
});

it(`can process scoped packages`, () => {
expect(microfrontendNameToFolderName("@org/root-config")).toMatchSnapshot();
expect(microfrontendNameToFolderName("@org-name/navbar")).toMatchSnapshot();
const utils = new ImportMapMicrofrontendUtils({
baseOrigin: "https://cdn.example.com",
});
expect(
microfrontendNameToFolderName("@org_name/left_nav"),
utils.getMicrofrontendURLPrefix("@org/root-config"),
).toMatchSnapshot();
expect(
utils.getMicrofrontendURLPrefix("@org-name/navbar"),
).toMatchSnapshot();
expect(
utils.getMicrofrontendURLPrefix("@org_name/left_nav"),
).toMatchSnapshot();
});
});

describe(`getDependenciesURLPrefix`, () => {
it(`returns url with trailing slash`, () => {
const utils = new ImportMapMicrofrontendUtils({
baseOrigin: "https://cdn.example.com",
});
expect(utils.getDependenciesURLPrefix()).toMatchSnapshot();
});
});
39 changes: 35 additions & 4 deletions src/import-map-microfrontend-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
export function microfrontendNameToFolderName(
microfrontendName: string,
): string {
return microfrontendName.replace(/@/g, "").replace(/\//, "-");
export class ImportMapMicrofrontendUtils {
declare baseOrigin: string;

constructor(init: Init) {
if (init.baseOrigin.endsWith("/")) {
throw Error(`baseOrigin must not end with slash`);
}

this.baseOrigin = init.baseOrigin;
}

getMicrofrontendURLPrefix(microfrontendName: string): string {
return (
this.baseOrigin +
`/${this.getMFEsFolderName()}/` +
microfrontendName.replace(/@/g, "").replace(/\//, "-") +
"/"
);
}

getDependenciesURLPrefix(): string {
return this.baseOrigin + `/${this.getDependenciesFolderName()}/`;
}

getDependenciesFolderName() {
return "deps";
}

getMFEsFolderName() {
return "mfes";
}
}

interface Init {
baseOrigin: string;
}

0 comments on commit 4255f57

Please sign in to comment.