Skip to content

Commit

Permalink
chore: refactor modernjs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Jul 15, 2024
1 parent f37d43e commit 74e82af
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 306 deletions.
17 changes: 15 additions & 2 deletions packages/modernjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
"./ssr-runtime": {
"types": "./dist/types/ssr-runtime/index.d.ts",
"default": "./dist/esm/ssr-runtime/index.js"
},
"./config-plugin": {
"import": "./dist/esm/cli/configPlugin.js",
"require": "./dist/cjs/cli/configPlugin.js",
"types": "./dist/types/cli/configPlugin.d.ts"
},
"./ssr-plugin": {
"import": "./dist/esm/cli/ssrPlugin.js",
"require": "./dist/cjs/cli/ssrPlugin.js",
"types": "./dist/types/cli/ssrPlugin.d.ts"
}
},
"typesVersions": {
Expand All @@ -35,8 +45,11 @@
"runtime": [
"./dist/types/runtime/index.d.ts"
],
"ssr-runtime": [
"./dist/types/ssr-runtime/index.d.ts"
"config-plugin": [
"./dist/types/cli/configPlugin.d.ts"
],
"ssr-plugin": [
"./dist/types/cli/ssrPlugin.d.ts"
]
}
},
Expand Down
179 changes: 179 additions & 0 deletions packages/modernjs/src/cli/configPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import path from 'path';
import type {
CliPlugin,
AppTools,
UserConfig,
Bundler,
} from '@modern-js/app-tools';
import {
StreamingTargetPlugin,
EntryChunkTrackerPlugin,
} from '@module-federation/node';
import type { BundlerConfig } from '../interfaces/bundler';
import type { InternalModernPluginOptions } from '../types';
import {
patchBundlerConfig,
getIPV4,
getMFConfig,
patchMFConfig,
} from './utils';
import { moduleFederationPlugin } from '@module-federation/sdk';
import { isDev } from './constant';

export function setEnv(enableSSR: boolean) {
if (enableSSR) {
process.env['MF_DISABLE_EMIT_STATS'] = 'true';
process.env['MF_SSR_PRJ'] = 'true';
}
}

export function modifyBundlerConfig<T extends Bundler>(options: {
bundlerType: Bundler;
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
config: BundlerConfig<T>;
isServer: boolean;
modernjsConfig: UserConfig<AppTools>;
}) {
const { mfConfig, config, isServer, modernjsConfig, bundlerType } = options;

patchMFConfig(mfConfig, isServer);

// let browserPlugin: BundlerPlugin | undefined = undefined;
// let nodePlugin: BundlerPlugin | undefined= undefined;
// let distOutputDir = '';
// const envConfig = getTargetEnvConfig(mfConfig, isServer);
// if (isServer) {
// // nodePlugin = new MFBundlerPlugin(envConfig);
// // config.plugins?.push(nodePlugin);
// config.plugins?.push(new StreamingTargetPlugin(mfConfig));
// if (isDev) {
// config.plugins?.push(new EntryChunkTrackerPlugin());
// }
// } else {
// // distOutputDir =
// // config.output?.path || path.resolve(process.cwd(), 'dist');
// // browserPlugin = new MFBundlerPlugin(envConfig);
// // config.plugins?.push(browserPlugin);
// }

patchBundlerConfig({
bundlerConfig: config,
isServer,
modernjsConfig,
mfConfig,
});

if (bundlerType === 'webpack') {
config.ignoreWarnings = config.ignoreWarnings || [];
config.ignoreWarnings.push((warning) => {
if (warning.message.includes('external script')) {
return true;
}
return false;
});
}

// return {
// distOutputDir
// }
}

export const moduleFederationConfigPlugin = (
userConfig: InternalModernPluginOptions,
): CliPlugin<AppTools> => ({
name: '@modern-js/plugin-module-federation-config',
post: ['@modern-js/plugin-module-federation'],
setup: async ({ useConfigContext, useAppContext }) => {
console.log('config plugin');

const modernjsConfig = useConfigContext();
const mfConfig = await getMFConfig(userConfig.originPluginOptions);
const csrConfig = JSON.parse(JSON.stringify(mfConfig));
const ssrConfig = JSON.parse(JSON.stringify(mfConfig));
userConfig.ssrConfig = ssrConfig;
userConfig.csrConfig = csrConfig;

return {
config: async () => {
const bundlerType =
useAppContext().bundlerType === 'rspack' ? 'rspack' : 'webpack';

// const WebpackPluginConstructor =
// userConfig.webpackPluginImplementation ||
// WebpackModuleFederationPlugin;
// const RspackPluginConstructor =
// userConfig.rspackPluginImplementation || RspackModuleFederationPlugin;

// const MFBundlerPlugin =
// bundlerType === 'rspack'
// ? RspackPluginConstructor
// : WebpackPluginConstructor;
const ipv4 = getIPV4();

return {
tools: {
rspack(config, { isServer }) {
modifyBundlerConfig({
bundlerType,
mfConfig: isServer ? ssrConfig : csrConfig,
config,
isServer,
// MFBundlerPlugin,
modernjsConfig,
});
userConfig.distOutputDir =
config.output?.path || path.resolve(process.cwd(), 'dist');
// if(res.browserPlugin){
// userConfig.browserPlugin = res.browserPlugin;
// }
// if(res.nodePlugin){
// userConfig.nodePlugin = res.nodePlugin;
// }
// if(res.distOutputDir){
// userConfig.distOutputDir = res.distOutputDir
// }
},
webpack(config, { isServer }) {
modifyBundlerConfig({
bundlerType,
mfConfig: isServer ? ssrConfig : csrConfig,
config,
isServer,
modernjsConfig,
});

userConfig.distOutputDir =
config.output?.path || path.resolve(process.cwd(), 'dist');
},
devServer: {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods':
'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers':
'X-Requested-With, content-type, Authorization',
},
},
},
source: {
alias: {
'@modern-js/runtime/mf': require.resolve(
'@module-federation/modern-js/runtime',
),
},
define: {
FEDERATION_IPV4: JSON.stringify(ipv4),
},
},
dev: {
assetPrefix: modernjsConfig?.dev?.assetPrefix
? modernjsConfig.dev.assetPrefix
: true,
},
};
},
};
},
});

export default moduleFederationConfigPlugin;
1 change: 1 addition & 0 deletions packages/modernjs/src/cli/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isDev = process.env.NODE_ENV === 'development';
Loading

0 comments on commit 74e82af

Please sign in to comment.