Skip to content

Commit

Permalink
feat(mf): resolve outdir to __mf_temp
Browse files Browse the repository at this point in the history
  • Loading branch information
zmzlois committed Jan 7, 2025
1 parent 1b8f20d commit c47e462
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/plugins/pluginModuleParseEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Dynamic shared modules, such as "react/" and "react-dom/", can only be parsed during the build process;
* This plugin allows me to wait until all modules are built, and then expose them together.
*/
import { Plugin } from 'vite';
import { Plugin, UserConfig } from 'vite';
import { VIRTUAL_EXPOSES } from '../virtualModules';

let _resolve: any,
Expand Down Expand Up @@ -30,6 +30,7 @@ export default function (excludeFn: Function): Plugin[] {
enforce: 'pre',
name: 'parseStart',
apply: 'build',
configResolved(config: UserConfig) {},
load(id) {
if (excludeFn(id)) {
return;
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/pluginProxySharedModule_preBuild.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Plugin, UserConfig } from 'vite';
import { NormalizedShared } from '../utils/normalizeModuleFederationOptions';
import {
NormalizedShared,
normalizeViteConfigOption,
} from '../utils/normalizeModuleFederationOptions';
import { PromiseStore } from '../utils/PromiseStore';
import VirtualModule from '../utils/VirtualModule';
import {
Expand All @@ -24,6 +27,10 @@ export function proxySharedModule(options: {
{
name: 'generateLocalSharedImportMap',
enforce: 'post',
config(config: UserConfig) {
_config = config;
normalizeViteConfigOption(config);
},
load(id) {
if (id.includes(getLocalSharedImportMapPath())) {
return parsePromise.then((_) => generateLocalSharedImportMap());
Expand Down
2 changes: 2 additions & 0 deletions src/utils/aliasToArrayPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UserConfig } from 'vite';
import { normalizeViteConfigOption } from './normalizeModuleFederationOptions';

export interface Command {
// define command properties here
Expand All @@ -7,6 +8,7 @@ export interface Command {
export default {
name: 'alias-transform-plugin',
config: (config: UserConfig, { command }: { command: Command }) => {
normalizeViteConfigOption(config);
if (!config.resolve) config.resolve = {};
if (!config.resolve.alias) config.resolve.alias = [];
const { alias } = config.resolve;
Expand Down
8 changes: 6 additions & 2 deletions src/utils/localSharedImportMap_temp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
*/
import { mkdirSync, writeFileSync } from 'fs';
import path from 'pathe';
import { getNormalizeModuleFederationOptions } from './normalizeModuleFederationOptions';
import {
getNormalizeModuleFederationOptions,
getNormalizeViteOptions,
} from './normalizeModuleFederationOptions';
import { packageNameEncode } from './packageNameUtils';

export function getLocalSharedImportMapPath_temp() {
const { name } = getNormalizeModuleFederationOptions();
return path.resolve('.__mf__temp', packageNameEncode(name), 'localSharedImportMap');
const { outDir } = getNormalizeViteOptions();
return path.resolve('.__mf__temp', packageNameEncode(name) + outDir, 'localSharedImportMap');
}
export function writeLocalSharedImportMap_temp(content: string) {
const localSharedImportMapId = getLocalSharedImportMapPath_temp();
Expand Down
37 changes: 34 additions & 3 deletions src/utils/normalizeModuleFederationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type RemoteEntryType =
| string;

import * as path from 'pathe';
import { UserConfig } from 'vite';
import { warn } from './logUtils';

interface ExposesItem {
Expand Down Expand Up @@ -252,6 +253,12 @@ export type ModuleFederationOptions = {
shareStrategy?: ShareStrategy;
};

export interface NormalizeViteOptions {
root?: string;
outDir?: string;
publicDir?: string | false | undefined;
}

export interface NormalizedModuleFederationOptions {
exposes: Record<string, ExposesItem>;
filename: string;
Expand All @@ -271,6 +278,14 @@ export interface NormalizedModuleFederationOptions {
getPublicPath?: string;
}

/** A combination of user's configuration in vite config and module federation config options */
export interface Config {
/** Module Federation configuration */
mfConfig: NormalizedModuleFederationOptions;
/** User-defined configuration in vite config */
envConfig: NormalizeViteOptions;
}

interface PluginDevOptions {
disableLiveReload?: boolean;
disableHotTypesReload?: boolean;
Expand Down Expand Up @@ -305,10 +320,10 @@ interface DtsHostOptions {
consumeAPITypes?: boolean;
}

let config: NormalizedModuleFederationOptions;
let config: Config;

export function getNormalizeModuleFederationOptions() {
return config;
return config.mfConfig;
}

export function getNormalizeShareItem(key: string) {
Expand All @@ -327,7 +342,7 @@ export function normalizeModuleFederationOptions(
`We are ignoring the getPublicPath options because they are natively supported by Vite\nwith the "experimental.renderBuiltUrl" configuration https://vitejs.dev/guide/build#advanced-base-options`
);
}
return (config = {
return (config.mfConfig = {

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > should set default values

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:13:12

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > exposes > should normalize an expose with a string value

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:34:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > exposes > should normalize an expose with an object value

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:49:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > remotes > should normalize a remote with a string value

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:68:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > remotes > should normalize a remote with an object value

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:87:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > shared > normalizes a string array

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:112:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > shared > normalizes an object

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:142:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > manifest > returns true if manifest is set to true

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:185:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > manifest > returns false if manifest is set to false

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:194:9

Check failure on line 345 in src/utils/normalizeModuleFederationOptions.ts

View workflow job for this annotation

GitHub Actions / Unit Test Package

src/utils/__tests__/normalizeModuleFederationOption.test.ts > normalizeModuleFederationOption > manifest > returns options in case manifest is an option object

TypeError: Cannot set properties of undefined (setting 'mfConfig') ❯ Module.normalizeModuleFederationOptions src/utils/normalizeModuleFederationOptions.ts:345:18 ❯ src/utils/__tests__/normalizeModuleFederationOption.test.ts:203:9
exposes: normalizeExposes(options.exposes),
filename: options.filename || 'remoteEntry-[hash]',
library: normalizeLibrary(options.library),
Expand All @@ -346,3 +361,19 @@ export function normalizeModuleFederationOptions(
shareStrategy: options.shareStrategy || 'version-first',
});
}

// TODO: encapsulate them (both mf config and user build config) into an object that's accessible within compiler scop
export function normalizeViteConfigOption(_config: UserConfig): Config['envConfig'] | undefined {
if (!_config.build?.outDir) {
return;
}
return (config.envConfig = {
root: _config.root,
outDir: _config.build.outDir,
publicDir: _config.publicDir,
});
}

export function getNormalizeViteOptions() {
return config.envConfig;
}

0 comments on commit c47e462

Please sign in to comment.