Skip to content

Commit

Permalink
feat: support pass pkgName
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Aug 12, 2024
1 parent 93068b9 commit 4f4332a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/modernjs/src/cli/dataLoaderPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function writeMFModernRouteJson({
function addExpose(
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
baseName: string,
pkgName: string,
) {
const mfMetaExposeFilePath = path.join(
process.cwd(),
Expand All @@ -56,14 +57,14 @@ function addExpose(
const routeMetaKey = `./${MF_ROUTE_META_KEY}`;
if (!mfConfig.exposes) {
mfConfig.exposes = {
[routesExposeKey]: './node_modules/.modern-js/main/routes.js',
[routesExposeKey]: `./node_modules/.${pkgName}/main/routes.js`,
[routeMetaKey]: mfMetaExposeFilePath,
};
} else {
if (!Array.isArray(mfConfig.exposes)) {
if (!mfConfig.exposes[routesExposeKey]) {
mfConfig.exposes[routesExposeKey] =
'./node_modules/.modern-js/main/routes.js';
`./node_modules/.${pkgName}/main/routes.js`;
}
if (!mfConfig.exposes[routeMetaKey]) {
mfConfig.exposes[routeMetaKey] = mfMetaExposeFilePath;
Expand All @@ -73,26 +74,29 @@ function addExpose(
}
function addShared(
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
pkgName: string,
) {
const alias = `@${pkgName}/runtime/router`;
if (!mfConfig.shared) {
mfConfig.shared = {
'@modern-js/runtime/router': { singleton: true },
[alias]: { singleton: true },
};
} else {
if (!Array.isArray(mfConfig.shared)) {
mfConfig.shared['@modern-js/runtime/router'] = { singleton: true };
mfConfig.shared[alias] = { singleton: true };
} else {
mfConfig.shared.push('@modern-js/runtime/router');
mfConfig.shared.push(alias);
}
}
}

function _pathMfConfig(
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
baseName: string,
pkgName: string,
) {
addShared(mfConfig);
addExpose(mfConfig, baseName);
addShared(mfConfig, pkgName);
addExpose(mfConfig, baseName, pkgName);
}

async function _fetchSSRByRouteIds(
Expand Down Expand Up @@ -167,6 +171,7 @@ export const moduleFederationDataLoaderPlugin = (
partialSSRRemotes = [],
fetchSSRByRouteIds,
patchMFConfig,
pkgName = 'modern-js',
} = userConfig;

if (!baseName) {
Expand Down Expand Up @@ -204,9 +209,9 @@ export const moduleFederationDataLoaderPlugin = (
tools: {
rspack(_config, { isServer }) {
if (isServer) {
patchMFConfigFn(internalOptions.ssrConfig!, baseName);
patchMFConfigFn(internalOptions.ssrConfig!, baseName, pkgName);
} else {
patchMFConfigFn(internalOptions.csrConfig!, baseName);
patchMFConfigFn(internalOptions.csrConfig!, baseName, pkgName);
}
console.log('dataloader plugin rspack');
},
Expand Down
2 changes: 2 additions & 0 deletions packages/modernjs/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export type BundlerPlugin =
export type DataLoaderOptions = {
baseName: string;
partialSSRRemotes?: string[];
pkgName?: string;
fetchSSRByRouteIds?: (
partialSSRRemotes: string[],
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
) => Promise<string[] | undefined>;
patchMFConfig?: (
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
baseName: string,
pkgName: string,
) => void;
};

0 comments on commit 4f4332a

Please sign in to comment.