Skip to content

Commit

Permalink
feat(react): Add versioning for workspace libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Sep 8, 2023
1 parent 4b344ac commit 78f1c8d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/generated/devkit/shareWorkspaceLibraries.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Function: shareWorkspaceLibraries

**shareWorkspaceLibraries**(`libraries`, `tsConfigPath?`): [`SharedWorkspaceLibraryConfig`](../../devkit/documents/SharedWorkspaceLibraryConfig)
**shareWorkspaceLibraries**(`workspaceLibs`, `tsConfigPath?`): [`SharedWorkspaceLibraryConfig`](../../devkit/documents/SharedWorkspaceLibraryConfig)

Build an object of functions to be used with the ModuleFederationPlugin to
share Nx Workspace Libraries between Hosts and Remotes.
Expand All @@ -9,7 +9,7 @@ share Nx Workspace Libraries between Hosts and Remotes.

| Name | Type | Description |
| :-------------- | :-------------------------------------------------------------- | :--------------------------------------------------------------------------- |
| `libraries` | [`WorkspaceLibrary`](../../devkit/documents/WorkspaceLibrary)[] | The Nx Workspace Libraries to share |
| `workspaceLibs` | [`WorkspaceLibrary`](../../devkit/documents/WorkspaceLibrary)[] | The Nx Workspace Libraries to share |
| `tsConfigPath?` | `string` | The path to TS Config File that contains the Path Mappings for the Libraries |

#### Returns
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NormalModuleReplacementPlugin } from 'webpack';

export type ModuleFederationLibrary = { type: string; name: string };

export type WorkspaceLibrary = {
name: string;
root: string;
Expand Down
50 changes: 39 additions & 11 deletions packages/devkit/src/utils/module-federation/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ import {
} from './secondary-entry-points';
import type { ProjectGraph } from 'nx/src/config/project-graph';
import { requireNx } from '../../../nx';
import type { PackageJson } from 'nx/src/utils/package-json';
import { existsSync } from 'fs-extra';

const { workspaceRoot, logger } = requireNx();
const { workspaceRoot, logger, readJsonFile } = requireNx();

/**
* Build an object of functions to be used with the ModuleFederationPlugin to
* share Nx Workspace Libraries between Hosts and Remotes.
*
* @param libraries - The Nx Workspace Libraries to share
* @param workspaceLibs - The Nx Workspace Libraries to share
* @param tsConfigPath - The path to TS Config File that contains the Path Mappings for the Libraries
*/
export function shareWorkspaceLibraries(
libraries: WorkspaceLibrary[],
workspaceLibs: WorkspaceLibrary[],
tsConfigPath = process.env.NX_TSCONFIG_PATH ?? getRootTsConfigPath()
): SharedWorkspaceLibraryConfig {
if (!libraries) {
if (!workspaceLibs) {
return getEmptySharedLibrariesConfig();
}

Expand All @@ -38,7 +40,7 @@ export function shareWorkspaceLibraries(

const pathMappings: { name: string; path: string }[] = [];
for (const [key, paths] of Object.entries(tsconfigPathAliases)) {
const library = libraries.find((lib) => lib.importKey === key);
const library = workspaceLibs.find((lib) => lib.importKey === key);
if (!library) {
continue;
}
Expand All @@ -63,20 +65,46 @@ export function shareWorkspaceLibraries(

const webpack = require('webpack');

const pkgJson = readRootPackageJson();

return {
getAliases: () =>
pathMappings.reduce(
(aliases, library) => ({ ...aliases, [library.name]: library.path }),
{}
),
getLibraries: (eager?: boolean): Record<string, SharedLibraryConfig> =>
pathMappings.reduce(
(libraries, library) => ({
pathMappings.reduce((libraries, library) => {
// Check to see if the library version ranged is delcared in the root package.json
let version =
pkgJson?.dependencies?.[library.name] ??
pkgJson?.devDependencies?.[library.name] ??
null;
if (!version) {
const workspaceLib = workspaceLibs.find(
(lib) => lib.importKey === library.name
);

const workspacePkgJsonPath = join(workspaceLib.root, 'package.json');
if (existsSync(workspacePkgJsonPath)) {
const workspacePackageJson: PackageJson =
readJsonFile(workspacePkgJsonPath);

if (!workspacePackageJson) {
logger.warn(
`Could not find a version for "${library.name}" in the root "package.json"`
);
} else {
version = workspacePackageJson.version;
}
}
}

return {
...libraries,
[library.name]: { requiredVersion: false, eager },
}),
{} as Record<string, SharedLibraryConfig>
),
[library.name]: { requiredVersion: version ?? false, eager },
};
}, {} as Record<string, SharedLibraryConfig>),
getReplacementPlugin: () =>
new webpack.NormalModuleReplacementPlugin(/./, (req) => {
if (!req.request.startsWith('.')) {
Expand Down

0 comments on commit 78f1c8d

Please sign in to comment.