@nx/module-federation 20.2.0-beta.2 → 20.2.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/angular.d.ts +2 -0
- package/angular.js +5 -0
- package/index.d.ts +1 -0
- package/index.js +4 -0
- package/package.json +12 -11
- package/rspack.d.ts +2 -0
- package/rspack.js +5 -0
- package/src/utils/dependencies.d.ts +6 -0
- package/src/utils/dependencies.js +56 -0
- package/src/utils/get-remotes-for-host.d.ts +16 -0
- package/src/utils/get-remotes-for-host.js +97 -0
- package/src/utils/index.d.ts +9 -0
- package/src/utils/index.js +12 -0
- package/src/utils/models/index.d.ts +47 -0
- package/src/utils/models/index.js +2 -0
- package/src/utils/package-json.d.ts +8 -0
- package/src/utils/package-json.js +12 -0
- package/src/utils/parse-static-remotes-config.d.ts +13 -0
- package/src/utils/parse-static-remotes-config.js +34 -0
- package/src/utils/plugins/runtime-library-control.plugin.d.ts +3 -0
- package/src/utils/plugins/runtime-library-control.plugin.js +54 -0
- package/src/utils/public-api.d.ts +6 -0
- package/src/utils/public-api.js +16 -0
- package/src/utils/remotes.d.ts +19 -0
- package/src/utils/remotes.js +89 -0
- package/src/utils/secondary-entry-points.d.ts +12 -0
- package/src/utils/secondary-entry-points.js +125 -0
- package/src/utils/share.d.ts +49 -0
- package/src/utils/share.js +246 -0
- package/src/utils/start-remote-proxies.d.ts +5 -0
- package/src/utils/start-remote-proxies.js +38 -0
- package/src/utils/start-ssr-remote-proxies.d.ts +5 -0
- package/src/utils/start-ssr-remote-proxies.js +52 -0
- package/src/utils/typescript.d.ts +4 -0
- package/src/utils/typescript.js +53 -0
- package/src/with-module-federation/angular/utils.d.ts +15 -0
- package/src/with-module-federation/angular/utils.js +101 -0
- package/src/with-module-federation/angular/with-module-federation-ssr.d.ts +2 -0
- package/src/with-module-federation/angular/with-module-federation-ssr.js +77 -0
- package/src/with-module-federation/angular/with-module-federation.d.ts +2 -0
- package/src/with-module-federation/angular/with-module-federation.js +70 -0
- package/src/with-module-federation/rspack/utils.d.ts +12 -0
- package/src/with-module-federation/rspack/utils.js +74 -0
- package/src/with-module-federation/rspack/with-module-federation-ssr.d.ts +4 -0
- package/src/with-module-federation/rspack/with-module-federation-ssr.js +67 -0
- package/src/with-module-federation/rspack/with-module-federation.d.ts +9 -0
- package/src/with-module-federation/rspack/with-module-federation.js +70 -0
- package/src/with-module-federation/webpack/utils.d.ts +12 -0
- package/src/with-module-federation/webpack/utils.js +75 -0
- package/src/with-module-federation/webpack/with-module-federation-ssr.d.ts +2 -0
- package/src/with-module-federation/webpack/with-module-federation-ssr.js +59 -0
- package/src/with-module-federation/webpack/with-module-federation.d.ts +5 -0
- package/src/with-module-federation/webpack/with-module-federation.js +62 -0
- package/webpack.d.ts +2 -0
- package/webpack.js +5 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.collectWorkspaceLibrarySecondaryEntryPoints = collectWorkspaceLibrarySecondaryEntryPoints;
|
|
4
|
+
exports.getNonNodeModulesSubDirs = getNonNodeModulesSubDirs;
|
|
5
|
+
exports.recursivelyCollectSecondaryEntryPointsFromDirectory = recursivelyCollectSecondaryEntryPointsFromDirectory;
|
|
6
|
+
exports.collectPackageSecondaryEntryPoints = collectPackageSecondaryEntryPoints;
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const devkit_1 = require("@nx/devkit");
|
|
10
|
+
const package_json_1 = require("nx/src/utils/package-json");
|
|
11
|
+
function collectWorkspaceLibrarySecondaryEntryPoints(library, tsconfigPathAliases) {
|
|
12
|
+
const libraryRoot = (0, path_1.join)(devkit_1.workspaceRoot, library.root);
|
|
13
|
+
const needsSecondaryEntryPointsCollected = (0, fs_1.existsSync)((0, path_1.join)(libraryRoot, 'ng-package.json'));
|
|
14
|
+
const secondaryEntryPoints = [];
|
|
15
|
+
if (needsSecondaryEntryPointsCollected) {
|
|
16
|
+
const tsConfigAliasesForLibWithSecondaryEntryPoints = Object.entries(tsconfigPathAliases).reduce((acc, [tsKey, tsPaths]) => {
|
|
17
|
+
if (!tsKey.startsWith(library.importKey)) {
|
|
18
|
+
return { ...acc };
|
|
19
|
+
}
|
|
20
|
+
if (tsPaths.some((path) => path.startsWith(`${library.root}/`))) {
|
|
21
|
+
acc = { ...acc, [tsKey]: tsPaths };
|
|
22
|
+
}
|
|
23
|
+
return acc;
|
|
24
|
+
}, {});
|
|
25
|
+
for (const [alias] of Object.entries(tsConfigAliasesForLibWithSecondaryEntryPoints)) {
|
|
26
|
+
const pathToLib = (0, path_1.dirname)((0, path_1.join)(devkit_1.workspaceRoot, tsconfigPathAliases[alias][0]));
|
|
27
|
+
let searchDir = pathToLib;
|
|
28
|
+
while (searchDir !== libraryRoot) {
|
|
29
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(searchDir, 'ng-package.json'))) {
|
|
30
|
+
secondaryEntryPoints.push({ name: alias, path: pathToLib });
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
searchDir = (0, path_1.dirname)(searchDir);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return secondaryEntryPoints;
|
|
38
|
+
}
|
|
39
|
+
function getNonNodeModulesSubDirs(directory) {
|
|
40
|
+
return (0, fs_1.readdirSync)(directory)
|
|
41
|
+
.filter((file) => file !== 'node_modules')
|
|
42
|
+
.map((file) => (0, path_1.join)(directory, file))
|
|
43
|
+
.filter((file) => (0, fs_1.lstatSync)(file).isDirectory());
|
|
44
|
+
}
|
|
45
|
+
function recursivelyCollectSecondaryEntryPointsFromDirectory(pkgName, pkgVersion, pkgRoot, mainEntryPointExports, directories, collectedPackages) {
|
|
46
|
+
for (const directory of directories) {
|
|
47
|
+
const packageJsonPath = (0, path_1.join)(directory, 'package.json');
|
|
48
|
+
const relativeEntryPointPath = (0, path_1.relative)(pkgRoot, directory);
|
|
49
|
+
const entryPointName = (0, devkit_1.joinPathFragments)(pkgName, relativeEntryPointPath);
|
|
50
|
+
if ((0, fs_1.existsSync)(packageJsonPath)) {
|
|
51
|
+
try {
|
|
52
|
+
// require the secondary entry point to try to rule out sample code
|
|
53
|
+
require.resolve(entryPointName, { paths: [devkit_1.workspaceRoot] });
|
|
54
|
+
const { name } = (0, devkit_1.readJsonFile)(packageJsonPath);
|
|
55
|
+
// further check to make sure what we were able to require is the
|
|
56
|
+
// same as the package name
|
|
57
|
+
if (name === entryPointName) {
|
|
58
|
+
collectedPackages.push({ name, version: pkgVersion });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch { }
|
|
62
|
+
}
|
|
63
|
+
else if (mainEntryPointExports) {
|
|
64
|
+
// if the package.json doesn't exist, check if the directory is
|
|
65
|
+
// exported by the main entry point
|
|
66
|
+
const entryPointExportKey = `./${relativeEntryPointPath}`;
|
|
67
|
+
const entryPointInfo = mainEntryPointExports[entryPointExportKey];
|
|
68
|
+
if (entryPointInfo) {
|
|
69
|
+
collectedPackages.push({
|
|
70
|
+
name: entryPointName,
|
|
71
|
+
version: pkgVersion,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const subDirs = getNonNodeModulesSubDirs(directory);
|
|
76
|
+
recursivelyCollectSecondaryEntryPointsFromDirectory(pkgName, pkgVersion, pkgRoot, mainEntryPointExports, subDirs, collectedPackages);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function collectPackagesFromExports(pkgName, pkgVersion, exports, collectedPackages) {
|
|
80
|
+
for (const [relativeEntryPoint, exportOptions] of Object.entries(exports)) {
|
|
81
|
+
const defaultExportOptions = typeof exportOptions?.['default'] === 'string'
|
|
82
|
+
? exportOptions?.['default']
|
|
83
|
+
: exportOptions?.['default']?.['default'];
|
|
84
|
+
if (defaultExportOptions?.search(/\.(js|mjs|cjs)$/)) {
|
|
85
|
+
let entryPointName = (0, devkit_1.joinPathFragments)(pkgName, relativeEntryPoint);
|
|
86
|
+
if (entryPointName.endsWith('.json')) {
|
|
87
|
+
entryPointName = (0, path_1.dirname)(entryPointName);
|
|
88
|
+
}
|
|
89
|
+
if (entryPointName === '.') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (collectedPackages.find((p) => p.name === entryPointName)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
collectedPackages.push({ name: entryPointName, version: pkgVersion });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function collectPackageSecondaryEntryPoints(pkgName, pkgVersion, collectedPackages) {
|
|
100
|
+
let pathToPackage;
|
|
101
|
+
let packageJsonPath;
|
|
102
|
+
let packageJson;
|
|
103
|
+
try {
|
|
104
|
+
({ path: packageJsonPath, packageJson } = (0, package_json_1.readModulePackageJson)(pkgName));
|
|
105
|
+
pathToPackage = (0, path_1.dirname)(packageJsonPath);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// the package.json might not resolve if the package has the "exports"
|
|
109
|
+
// entry and is not exporting the package.json file, fall back to trying
|
|
110
|
+
// to find it from the top-level node_modules
|
|
111
|
+
pathToPackage = (0, path_1.join)(devkit_1.workspaceRoot, 'node_modules', pkgName);
|
|
112
|
+
packageJsonPath = (0, path_1.join)(pathToPackage, 'package.json');
|
|
113
|
+
if (!(0, fs_1.existsSync)(packageJsonPath)) {
|
|
114
|
+
// might not exist if it's nested in another package, just return here
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
packageJson = (0, devkit_1.readJsonFile)(packageJsonPath);
|
|
118
|
+
}
|
|
119
|
+
const { exports } = packageJson;
|
|
120
|
+
if (exports) {
|
|
121
|
+
collectPackagesFromExports(pkgName, pkgVersion, exports, collectedPackages);
|
|
122
|
+
}
|
|
123
|
+
const subDirs = getNonNodeModulesSubDirs(pathToPackage);
|
|
124
|
+
recursivelyCollectSecondaryEntryPointsFromDirectory(pkgName, pkgVersion, pathToPackage, exports, subDirs, collectedPackages);
|
|
125
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { SharedLibraryConfig, SharedWorkspaceLibraryConfig, WorkspaceLibrary } from './models';
|
|
2
|
+
import { AdditionalSharedConfig, SharedFunction } from './models';
|
|
3
|
+
import { type ProjectGraph } from '@nx/devkit';
|
|
4
|
+
/**
|
|
5
|
+
* Build an object of functions to be used with the ModuleFederationPlugin to
|
|
6
|
+
* share Nx Workspace Libraries between Hosts and Remotes.
|
|
7
|
+
*
|
|
8
|
+
* @param workspaceLibs - The Nx Workspace Libraries to share
|
|
9
|
+
* @param tsConfigPath - The path to TS Config File that contains the Path Mappings for the Libraries
|
|
10
|
+
* @param bundler - The bundler to use for the replacement plugin
|
|
11
|
+
*/
|
|
12
|
+
export declare function shareWorkspaceLibraries(workspaceLibs: WorkspaceLibrary[], tsConfigPath?: string, bundler?: 'rspack' | 'webpack'): SharedWorkspaceLibraryConfig;
|
|
13
|
+
/**
|
|
14
|
+
* Build the Module Federation Share Config for a specific package and the
|
|
15
|
+
* specified version of that package.
|
|
16
|
+
* @param pkgName - Name of the package to share
|
|
17
|
+
* @param version - Version of the package to require by other apps in the Module Federation setup
|
|
18
|
+
*/
|
|
19
|
+
export declare function getNpmPackageSharedConfig(pkgName: string, version: string): SharedLibraryConfig | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Create a dictionary of packages and their Module Federation Shared Config
|
|
22
|
+
* from an array of package names.
|
|
23
|
+
*
|
|
24
|
+
* Lookup the versions of the packages from the root package.json file in the
|
|
25
|
+
* workspace.
|
|
26
|
+
* @param packages - Array of package names as strings
|
|
27
|
+
*/
|
|
28
|
+
export declare function sharePackages(packages: string[]): Record<string, SharedLibraryConfig>;
|
|
29
|
+
/**
|
|
30
|
+
* Apply a custom function provided by the user that will modify the Shared Config
|
|
31
|
+
* of the dependencies for the Module Federation build.
|
|
32
|
+
*
|
|
33
|
+
* @param sharedConfig - The original Shared Config to be modified
|
|
34
|
+
* @param sharedFn - The custom function to run
|
|
35
|
+
*/
|
|
36
|
+
export declare function applySharedFunction(sharedConfig: Record<string, SharedLibraryConfig>, sharedFn: SharedFunction | undefined): void;
|
|
37
|
+
/**
|
|
38
|
+
* Add additional dependencies to the shared package that may not have been
|
|
39
|
+
* discovered by the project graph.
|
|
40
|
+
*
|
|
41
|
+
* This can be useful for applications that use a Dependency Injection system
|
|
42
|
+
* that expects certain Singleton values to be present in the shared injection
|
|
43
|
+
* hierarchy.
|
|
44
|
+
*
|
|
45
|
+
* @param sharedConfig - The original Shared Config
|
|
46
|
+
* @param additionalShared - The additional dependencies to add
|
|
47
|
+
* @param projectGraph - The Nx project graph
|
|
48
|
+
*/
|
|
49
|
+
export declare function applyAdditionalShared(sharedConfig: Record<string, SharedLibraryConfig>, additionalShared: AdditionalSharedConfig | undefined, projectGraph: ProjectGraph): void;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shareWorkspaceLibraries = shareWorkspaceLibraries;
|
|
4
|
+
exports.getNpmPackageSharedConfig = getNpmPackageSharedConfig;
|
|
5
|
+
exports.sharePackages = sharePackages;
|
|
6
|
+
exports.applySharedFunction = applySharedFunction;
|
|
7
|
+
exports.applyAdditionalShared = applyAdditionalShared;
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const package_json_1 = require("./package-json");
|
|
10
|
+
const typescript_1 = require("./typescript");
|
|
11
|
+
const secondary_entry_points_1 = require("./secondary-entry-points");
|
|
12
|
+
const devkit_1 = require("@nx/devkit");
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const core_1 = require("@rspack/core");
|
|
15
|
+
/**
|
|
16
|
+
* Build an object of functions to be used with the ModuleFederationPlugin to
|
|
17
|
+
* share Nx Workspace Libraries between Hosts and Remotes.
|
|
18
|
+
*
|
|
19
|
+
* @param workspaceLibs - The Nx Workspace Libraries to share
|
|
20
|
+
* @param tsConfigPath - The path to TS Config File that contains the Path Mappings for the Libraries
|
|
21
|
+
* @param bundler - The bundler to use for the replacement plugin
|
|
22
|
+
*/
|
|
23
|
+
function shareWorkspaceLibraries(workspaceLibs, tsConfigPath = process.env.NX_TSCONFIG_PATH ?? (0, typescript_1.getRootTsConfigPath)(), bundler = 'rspack') {
|
|
24
|
+
if (!workspaceLibs) {
|
|
25
|
+
return getEmptySharedLibrariesConfig();
|
|
26
|
+
}
|
|
27
|
+
const tsconfigPathAliases = (0, typescript_1.readTsPathMappings)(tsConfigPath);
|
|
28
|
+
if (!Object.keys(tsconfigPathAliases).length) {
|
|
29
|
+
return getEmptySharedLibrariesConfig();
|
|
30
|
+
}
|
|
31
|
+
// Nested projects must come first, sort them as such
|
|
32
|
+
const sortedTsConfigPathAliases = {};
|
|
33
|
+
Object.keys(tsconfigPathAliases)
|
|
34
|
+
.sort((a, b) => {
|
|
35
|
+
return b.split('/').length - a.split('/').length;
|
|
36
|
+
})
|
|
37
|
+
.forEach((key) => (sortedTsConfigPathAliases[key] = tsconfigPathAliases[key]));
|
|
38
|
+
const pathMappings = [];
|
|
39
|
+
for (const [key, paths] of Object.entries(sortedTsConfigPathAliases)) {
|
|
40
|
+
const library = workspaceLibs.find((lib) => lib.importKey === key);
|
|
41
|
+
if (!library) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// This is for Angular Projects that use ng-package.json
|
|
45
|
+
// It will do nothing for React Projects
|
|
46
|
+
(0, secondary_entry_points_1.collectWorkspaceLibrarySecondaryEntryPoints)(library, sortedTsConfigPathAliases).forEach(({ name, path }) => pathMappings.push({
|
|
47
|
+
name,
|
|
48
|
+
path,
|
|
49
|
+
}));
|
|
50
|
+
pathMappings.push({
|
|
51
|
+
name: key,
|
|
52
|
+
path: (0, path_1.normalize)((0, path_1.join)(devkit_1.workspaceRoot, paths[0])),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const normalModuleReplacementPluginImpl = bundler === 'rspack'
|
|
56
|
+
? core_1.NormalModuleReplacementPlugin
|
|
57
|
+
: require('webpack').NormalModuleReplacementPlugin;
|
|
58
|
+
return {
|
|
59
|
+
getAliases: () => pathMappings.reduce((aliases, library) => ({
|
|
60
|
+
...aliases,
|
|
61
|
+
// If the library path ends in a wildcard, remove it as webpack/rspack can't handle this in resolve.alias
|
|
62
|
+
// e.g. path/to/my/lib/* -> path/to/my/lib
|
|
63
|
+
[library.name]: library.path.replace(/\/\*$/, ''),
|
|
64
|
+
}), {}),
|
|
65
|
+
getLibraries: (projectRoot, eager) => {
|
|
66
|
+
let pkgJson = null;
|
|
67
|
+
if (projectRoot &&
|
|
68
|
+
(0, fs_1.existsSync)((0, devkit_1.joinPathFragments)(devkit_1.workspaceRoot, projectRoot, 'package.json'))) {
|
|
69
|
+
pkgJson = (0, devkit_1.readJsonFile)((0, devkit_1.joinPathFragments)(devkit_1.workspaceRoot, projectRoot, 'package.json'));
|
|
70
|
+
}
|
|
71
|
+
return pathMappings.reduce((libraries, library) => {
|
|
72
|
+
// Check to see if the library version is declared in the app's package.json
|
|
73
|
+
let version = pkgJson?.dependencies?.[library.name];
|
|
74
|
+
if (!version && workspaceLibs.length > 0) {
|
|
75
|
+
const workspaceLib = workspaceLibs.find((lib) => lib.importKey === library.name);
|
|
76
|
+
const libPackageJsonPath = workspaceLib
|
|
77
|
+
? (0, path_1.join)(workspaceLib.root, 'package.json')
|
|
78
|
+
: null;
|
|
79
|
+
if (libPackageJsonPath && (0, fs_1.existsSync)(libPackageJsonPath)) {
|
|
80
|
+
pkgJson = (0, devkit_1.readJsonFile)(libPackageJsonPath);
|
|
81
|
+
if (pkgJson) {
|
|
82
|
+
version = pkgJson.version;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
...libraries,
|
|
88
|
+
[library.name]: {
|
|
89
|
+
...(version
|
|
90
|
+
? {
|
|
91
|
+
requiredVersion: version,
|
|
92
|
+
singleton: true,
|
|
93
|
+
}
|
|
94
|
+
: { requiredVersion: false }),
|
|
95
|
+
eager,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}, {});
|
|
99
|
+
},
|
|
100
|
+
getReplacementPlugin: () => new normalModuleReplacementPluginImpl(/./, (req) => {
|
|
101
|
+
if (!req.request.startsWith('.')) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const from = req.context;
|
|
105
|
+
const to = (0, path_1.normalize)((0, path_1.join)(req.context, req.request));
|
|
106
|
+
for (const library of pathMappings) {
|
|
107
|
+
const libFolder = (0, path_1.normalize)((0, path_1.dirname)(library.path));
|
|
108
|
+
if (!from.startsWith(libFolder) && to.startsWith(libFolder)) {
|
|
109
|
+
const newReq = library.name.endsWith('/*')
|
|
110
|
+
? /**
|
|
111
|
+
* req usually is in the form of "../../../path/to/file"
|
|
112
|
+
* library.path is usually in the form of "/Users/username/path/to/Workspace/path/to/library"
|
|
113
|
+
*
|
|
114
|
+
* When a wildcard is used in the TS path mappings, we want to get everything after the import to
|
|
115
|
+
* re-route the request correctly inline with the webpack/rspack resolve.alias
|
|
116
|
+
*/
|
|
117
|
+
(0, path_1.join)(library.name, req.request.split(library.path.replace(devkit_1.workspaceRoot, '').replace('/*', ''))[1])
|
|
118
|
+
: library.name;
|
|
119
|
+
req.request = newReq;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Build the Module Federation Share Config for a specific package and the
|
|
127
|
+
* specified version of that package.
|
|
128
|
+
* @param pkgName - Name of the package to share
|
|
129
|
+
* @param version - Version of the package to require by other apps in the Module Federation setup
|
|
130
|
+
*/
|
|
131
|
+
function getNpmPackageSharedConfig(pkgName, version) {
|
|
132
|
+
if (!version) {
|
|
133
|
+
devkit_1.logger.warn(`Could not find a version for "${pkgName}" in the root "package.json" ` +
|
|
134
|
+
'when collecting shared packages for the Module Federation setup. ' +
|
|
135
|
+
'The package will not be shared.');
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
return { singleton: true, strictVersion: true, requiredVersion: version };
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Create a dictionary of packages and their Module Federation Shared Config
|
|
142
|
+
* from an array of package names.
|
|
143
|
+
*
|
|
144
|
+
* Lookup the versions of the packages from the root package.json file in the
|
|
145
|
+
* workspace.
|
|
146
|
+
* @param packages - Array of package names as strings
|
|
147
|
+
*/
|
|
148
|
+
function sharePackages(packages) {
|
|
149
|
+
const pkgJson = (0, package_json_1.readRootPackageJson)();
|
|
150
|
+
const allPackages = [];
|
|
151
|
+
packages.forEach((pkg) => {
|
|
152
|
+
const pkgVersion = pkgJson.dependencies?.[pkg] ?? pkgJson.devDependencies?.[pkg];
|
|
153
|
+
allPackages.push({ name: pkg, version: pkgVersion });
|
|
154
|
+
(0, secondary_entry_points_1.collectPackageSecondaryEntryPoints)(pkg, pkgVersion, allPackages);
|
|
155
|
+
});
|
|
156
|
+
return allPackages.reduce((shared, pkg) => {
|
|
157
|
+
const config = getNpmPackageSharedConfig(pkg.name, pkg.version);
|
|
158
|
+
if (config) {
|
|
159
|
+
shared[pkg.name] = config;
|
|
160
|
+
}
|
|
161
|
+
return shared;
|
|
162
|
+
}, {});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Apply a custom function provided by the user that will modify the Shared Config
|
|
166
|
+
* of the dependencies for the Module Federation build.
|
|
167
|
+
*
|
|
168
|
+
* @param sharedConfig - The original Shared Config to be modified
|
|
169
|
+
* @param sharedFn - The custom function to run
|
|
170
|
+
*/
|
|
171
|
+
function applySharedFunction(sharedConfig, sharedFn) {
|
|
172
|
+
if (!sharedFn) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
for (const [libraryName, library] of Object.entries(sharedConfig)) {
|
|
176
|
+
const mappedDependency = sharedFn(libraryName, library);
|
|
177
|
+
if (mappedDependency === false) {
|
|
178
|
+
delete sharedConfig[libraryName];
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
else if (!mappedDependency) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
sharedConfig[libraryName] = mappedDependency;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Add additional dependencies to the shared package that may not have been
|
|
189
|
+
* discovered by the project graph.
|
|
190
|
+
*
|
|
191
|
+
* This can be useful for applications that use a Dependency Injection system
|
|
192
|
+
* that expects certain Singleton values to be present in the shared injection
|
|
193
|
+
* hierarchy.
|
|
194
|
+
*
|
|
195
|
+
* @param sharedConfig - The original Shared Config
|
|
196
|
+
* @param additionalShared - The additional dependencies to add
|
|
197
|
+
* @param projectGraph - The Nx project graph
|
|
198
|
+
*/
|
|
199
|
+
function applyAdditionalShared(sharedConfig, additionalShared, projectGraph) {
|
|
200
|
+
if (!additionalShared) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
for (const shared of additionalShared) {
|
|
204
|
+
if (typeof shared === 'string') {
|
|
205
|
+
addStringDependencyToSharedConfig(sharedConfig, shared, projectGraph);
|
|
206
|
+
}
|
|
207
|
+
else if (Array.isArray(shared)) {
|
|
208
|
+
sharedConfig[shared[0]] = shared[1];
|
|
209
|
+
}
|
|
210
|
+
else if (typeof shared === 'object') {
|
|
211
|
+
sharedConfig[shared.libraryName] = shared.sharedConfig;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function addStringDependencyToSharedConfig(sharedConfig, dependency, projectGraph) {
|
|
216
|
+
if (projectGraph.nodes[dependency]) {
|
|
217
|
+
sharedConfig[dependency] = { requiredVersion: false };
|
|
218
|
+
}
|
|
219
|
+
else if (projectGraph.externalNodes?.[`npm:${dependency}`]) {
|
|
220
|
+
const pkgJson = (0, package_json_1.readRootPackageJson)();
|
|
221
|
+
const config = getNpmPackageSharedConfig(dependency, pkgJson.dependencies?.[dependency] ??
|
|
222
|
+
pkgJson.devDependencies?.[dependency]);
|
|
223
|
+
if (!config) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
sharedConfig[dependency] = config;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
const pkgJsonPath = require.resolve(`${dependency}/package.json`);
|
|
230
|
+
if (!pkgJsonPath) {
|
|
231
|
+
throw new Error(`Could not find package ${dependency} when applying it as a shared package. Are you sure it has been installed?`);
|
|
232
|
+
}
|
|
233
|
+
const pkgJson = (0, devkit_1.readJsonFile)(pkgJsonPath);
|
|
234
|
+
const config = getNpmPackageSharedConfig(dependency, pkgJson.version);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function getEmptySharedLibrariesConfig(bundler = 'rspack') {
|
|
238
|
+
const normalModuleReplacementPluginImpl = bundler === 'rspack'
|
|
239
|
+
? core_1.NormalModuleReplacementPlugin
|
|
240
|
+
: require('webpack').NormalModuleReplacementPlugin;
|
|
241
|
+
return {
|
|
242
|
+
getAliases: () => ({}),
|
|
243
|
+
getLibraries: () => ({}),
|
|
244
|
+
getReplacementPlugin: () => new normalModuleReplacementPluginImpl(/./, () => { }),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StaticRemotesConfig } from './parse-static-remotes-config';
|
|
2
|
+
export declare function startRemoteProxies(staticRemotesConfig: StaticRemotesConfig, mappedLocationsOfRemotes: Record<string, string>, sslOptions?: {
|
|
3
|
+
pathToCert: string;
|
|
4
|
+
pathToKey: string;
|
|
5
|
+
}): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startRemoteProxies = startRemoteProxies;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
function startRemoteProxies(staticRemotesConfig, mappedLocationsOfRemotes, sslOptions) {
|
|
7
|
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
8
|
+
const express = require('express');
|
|
9
|
+
let sslCert;
|
|
10
|
+
let sslKey;
|
|
11
|
+
if (sslOptions && sslOptions.pathToCert && sslOptions.pathToKey) {
|
|
12
|
+
if ((0, fs_1.existsSync)(sslOptions.pathToCert) && (0, fs_1.existsSync)(sslOptions.pathToKey)) {
|
|
13
|
+
sslCert = (0, fs_1.readFileSync)(sslOptions.pathToCert);
|
|
14
|
+
sslKey = (0, fs_1.readFileSync)(sslOptions.pathToKey);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
devkit_1.logger.warn(`Encountered SSL options in project.json, however, the certificate files do not exist in the filesystem. Using http.`);
|
|
18
|
+
devkit_1.logger.warn(`Attempted to find '${sslOptions.pathToCert}' and '${sslOptions.pathToKey}'.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const http = require('http');
|
|
22
|
+
const https = require('https');
|
|
23
|
+
devkit_1.logger.info(`NX Starting static remotes proxies...`);
|
|
24
|
+
for (const app of staticRemotesConfig.remotes) {
|
|
25
|
+
const expressProxy = express();
|
|
26
|
+
expressProxy.use(createProxyMiddleware({
|
|
27
|
+
target: mappedLocationsOfRemotes[app],
|
|
28
|
+
changeOrigin: true,
|
|
29
|
+
secure: sslCert ? false : undefined,
|
|
30
|
+
}));
|
|
31
|
+
const proxyServer = (sslCert ? https : http)
|
|
32
|
+
.createServer({ cert: sslCert, key: sslKey }, expressProxy)
|
|
33
|
+
.listen(staticRemotesConfig.config[app].port);
|
|
34
|
+
process.on('SIGTERM', () => proxyServer.close());
|
|
35
|
+
process.on('exit', () => proxyServer.close());
|
|
36
|
+
}
|
|
37
|
+
devkit_1.logger.info(`NX Static remotes proxies started successfully`);
|
|
38
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StaticRemotesConfig } from './parse-static-remotes-config';
|
|
2
|
+
export declare function startSsrRemoteProxies(staticRemotesConfig: StaticRemotesConfig, mappedLocationsOfRemotes: Record<string, string>, sslOptions?: {
|
|
3
|
+
pathToCert: string;
|
|
4
|
+
pathToKey: string;
|
|
5
|
+
}): void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startSsrRemoteProxies = startSsrRemoteProxies;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
function startSsrRemoteProxies(staticRemotesConfig, mappedLocationsOfRemotes, sslOptions) {
|
|
7
|
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
8
|
+
const express = require('express');
|
|
9
|
+
let sslCert;
|
|
10
|
+
let sslKey;
|
|
11
|
+
if (sslOptions && sslOptions.pathToCert && sslOptions.pathToKey) {
|
|
12
|
+
if ((0, fs_1.existsSync)(sslOptions.pathToCert) && (0, fs_1.existsSync)(sslOptions.pathToKey)) {
|
|
13
|
+
sslCert = (0, fs_1.readFileSync)(sslOptions.pathToCert);
|
|
14
|
+
sslKey = (0, fs_1.readFileSync)(sslOptions.pathToKey);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
devkit_1.logger.warn(`Encountered SSL options in project.json, however, the certificate files do not exist in the filesystem. Using http.`);
|
|
18
|
+
devkit_1.logger.warn(`Attempted to find '${sslOptions.pathToCert}' and '${sslOptions.pathToKey}'.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const http = require('http');
|
|
22
|
+
const https = require('https');
|
|
23
|
+
devkit_1.logger.info(`NX Starting static remotes proxies...`);
|
|
24
|
+
for (const app of staticRemotesConfig.remotes) {
|
|
25
|
+
const expressProxy = express();
|
|
26
|
+
/**
|
|
27
|
+
* SSR remotes have two output paths: one for the browser and one for the server.
|
|
28
|
+
* We need to handle paths for both of them.
|
|
29
|
+
* The browser output path is used to serve the client-side code.
|
|
30
|
+
* The server output path is used to serve the server-side code.
|
|
31
|
+
*/
|
|
32
|
+
expressProxy.use(createProxyMiddleware({
|
|
33
|
+
target: `${mappedLocationsOfRemotes[app]}`,
|
|
34
|
+
secure: sslCert ? false : undefined,
|
|
35
|
+
changeOrigin: true,
|
|
36
|
+
pathRewrite: (path) => {
|
|
37
|
+
if (path.includes('/server')) {
|
|
38
|
+
return path;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return `browser/${path}`;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
}));
|
|
45
|
+
const proxyServer = (sslCert ? https : http)
|
|
46
|
+
.createServer({ cert: sslCert, key: sslKey }, expressProxy)
|
|
47
|
+
.listen(staticRemotesConfig.config[app].port);
|
|
48
|
+
process.on('SIGTERM', () => proxyServer.close());
|
|
49
|
+
process.on('exit', () => proxyServer.close());
|
|
50
|
+
}
|
|
51
|
+
devkit_1.logger.info(`Nx SSR Static remotes proxies started successfully`);
|
|
52
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ParsedCommandLine } from 'typescript';
|
|
2
|
+
export declare function readTsPathMappings(tsConfigPath?: string): ParsedCommandLine['options']['paths'];
|
|
3
|
+
export declare function readTsConfig(tsConfigPath: string): ParsedCommandLine;
|
|
4
|
+
export declare function getRootTsConfigPath(): string | null;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readTsPathMappings = readTsPathMappings;
|
|
4
|
+
exports.readTsConfig = readTsConfig;
|
|
5
|
+
exports.getRootTsConfigPath = getRootTsConfigPath;
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const devkit_1 = require("@nx/devkit");
|
|
9
|
+
let tsConfig = new Map();
|
|
10
|
+
let tsPathMappings = new Map();
|
|
11
|
+
function readTsPathMappings(tsConfigPath = process.env.NX_TSCONFIG_PATH ?? getRootTsConfigPath()) {
|
|
12
|
+
if (tsPathMappings.has(tsConfigPath)) {
|
|
13
|
+
return tsPathMappings.get(tsConfigPath);
|
|
14
|
+
}
|
|
15
|
+
if (!tsConfig.has(tsConfigPath)) {
|
|
16
|
+
tsConfig.set(tsConfigPath, readTsConfiguration(tsConfigPath));
|
|
17
|
+
}
|
|
18
|
+
tsPathMappings.set(tsConfigPath, {});
|
|
19
|
+
Object.entries(tsConfig.get(tsConfigPath).options?.paths ?? {}).forEach(([alias, paths]) => {
|
|
20
|
+
tsPathMappings.set(tsConfigPath, {
|
|
21
|
+
...tsPathMappings.get(tsConfigPath),
|
|
22
|
+
[alias]: paths.map((path) => path.replace(/^\.\//, '')),
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
return tsPathMappings.get(tsConfigPath);
|
|
26
|
+
}
|
|
27
|
+
function readTsConfiguration(tsConfigPath) {
|
|
28
|
+
if (!(0, fs_1.existsSync)(tsConfigPath)) {
|
|
29
|
+
throw new Error(`NX MF: TsConfig Path for workspace libraries does not exist! (${tsConfigPath}).`);
|
|
30
|
+
}
|
|
31
|
+
return readTsConfig(tsConfigPath);
|
|
32
|
+
}
|
|
33
|
+
let tsModule;
|
|
34
|
+
function readTsConfig(tsConfigPath) {
|
|
35
|
+
if (!tsModule) {
|
|
36
|
+
tsModule = require('typescript');
|
|
37
|
+
}
|
|
38
|
+
const readResult = tsModule.readConfigFile(tsConfigPath, tsModule.sys.readFile);
|
|
39
|
+
return tsModule.parseJsonConfigFileContent(readResult.config, tsModule.sys, (0, path_1.dirname)(tsConfigPath));
|
|
40
|
+
}
|
|
41
|
+
function getRootTsConfigPath() {
|
|
42
|
+
const tsConfigFileName = getRootTsConfigFileName();
|
|
43
|
+
return tsConfigFileName ? (0, path_1.join)(devkit_1.workspaceRoot, tsConfigFileName) : null;
|
|
44
|
+
}
|
|
45
|
+
function getRootTsConfigFileName() {
|
|
46
|
+
for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) {
|
|
47
|
+
const tsConfigPath = (0, path_1.join)(devkit_1.workspaceRoot, tsConfigName);
|
|
48
|
+
if ((0, fs_1.existsSync)(tsConfigPath)) {
|
|
49
|
+
return tsConfigName;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ModuleFederationConfig, SharedLibraryConfig } from '../../utils';
|
|
2
|
+
export declare function applyDefaultEagerPackages(sharedConfig: Record<string, SharedLibraryConfig>): void;
|
|
3
|
+
export declare const DEFAULT_NPM_PACKAGES_TO_AVOID: string[];
|
|
4
|
+
export declare const DEFAULT_ANGULAR_PACKAGES_TO_SHARE: string[];
|
|
5
|
+
export declare function getFunctionDeterminateRemoteUrl(isServer?: boolean): (remote: string) => string;
|
|
6
|
+
export declare function getModuleFederationConfig(mfConfig: ModuleFederationConfig, options?: {
|
|
7
|
+
isServer: boolean;
|
|
8
|
+
determineRemoteUrl?: (remote: string) => string;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
sharedLibraries: import("../../utils").SharedWorkspaceLibraryConfig;
|
|
11
|
+
sharedDependencies: {
|
|
12
|
+
[x: string]: SharedLibraryConfig;
|
|
13
|
+
};
|
|
14
|
+
mappedRemotes: Record<string, string>;
|
|
15
|
+
}>;
|