Skip to content

Commit

Permalink
feat: support rspack
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Jul 24, 2024
1 parent 6154a5d commit d9cb521
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 132 deletions.
9 changes: 9 additions & 0 deletions apps/modernjs-ssr/nested-remote/modern.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { appTools, defineConfig } from '@modern-js/app-tools';
import { moduleFederationPlugin } from '@module-federation/modern-js';
import { config } from 'process';

// https://modernjs.dev/en/configure/app/usage
export default defineConfig({
Expand All @@ -9,12 +10,20 @@ export default defineConfig({
runtime: {
router: true,
},
source: {
// enableAsyncEntry:true,
},
server: {
ssr: {
mode: 'stream',
},
port: 3052,
},
tools: {
rspack: (config) => {
// config.resolve?.extensions?.unshift('.server.jsx')
},
},
plugins: [
appTools({
bundler: 'experimental-rspack',
Expand Down
10 changes: 5 additions & 5 deletions apps/modernjs-ssr/nested-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
"dist/"
],
"dependencies": {
"@modern-js/runtime": "2.56.1",
"@modern-js/runtime": "0.0.0-next-20240724071459",
"react": "~18.2.0",
"react-dom": "~18.2.0",
"@module-federation/modern-js": "workspace:*",
"@babel/runtime": "7.24.4",
"antd": "4.24.15"
},
"devDependencies": {
"@modern-js/app-tools": "2.56.1",
"@modern-js/eslint-config": "2.56.1",
"@modern-js/tsconfig": "2.56.1",
"@modern-js-app/eslint-config": "2.56.1",
"@modern-js/app-tools": "0.0.0-next-20240724071459",
"@modern-js/eslint-config": "0.0.0-next-20240724071459",
"@modern-js/tsconfig": "0.0.0-next-20240724071459",
"@modern-js-app/eslint-config": "0.0.0-next-20240724071459",
"typescript": "~5.0.4",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
Expand Down
1 change: 1 addition & 0 deletions apps/modernjs-ssr/nested-remote/src/routes/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Outlet } from '@modern-js/runtime/router';

export default function Layout() {
console.log('layout');
return (
<div>
<Outlet />
Expand Down
4 changes: 2 additions & 2 deletions apps/modernjs-ssr/remote/module-federation.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default createModuleFederationConfig({
'./Button': './src/components/Button.tsx',
},
shared: {
react: { singleton: true, eager: true },
'react-dom': { singleton: true, eager: true },
react: { singleton: true },
'react-dom': { singleton: true },
},
});
10 changes: 5 additions & 5 deletions apps/modernjs-ssr/remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
"dist/"
],
"dependencies": {
"@modern-js/runtime": "2.56.1",
"@modern-js/runtime": "0.0.0-next-20240724071459",
"react": "~18.2.0",
"react-dom": "~18.2.0",
"@module-federation/modern-js": "workspace:*",
"@babel/runtime": "7.24.4",
"antd": "4.24.15"
},
"devDependencies": {
"@modern-js/app-tools": "2.56.1",
"@modern-js/eslint-config": "2.56.1",
"@modern-js/tsconfig": "2.56.1",
"@modern-js-app/eslint-config": "2.56.1",
"@modern-js/app-tools": "0.0.0-next-20240724071459",
"@modern-js/eslint-config": "0.0.0-next-20240724071459",
"@modern-js/tsconfig": "0.0.0-next-20240724071459",
"@modern-js-app/eslint-config": "0.0.0-next-20240724071459",
"typescript": "~5.0.4",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
Expand Down
85 changes: 2 additions & 83 deletions apps/modernjs-ssr/remote/src/routes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,92 +1,11 @@
// @ts-nocheck
import { Helmet } from '@modern-js/runtime/head';
import Image from '../components/Image';
import './index.css';

const Index = () => (
<div className="container-box">
<Helmet>
<link
rel="icon"
type="image/x-icon"
href="https://lf3-static.bytednsdoc.com/obj/eden-cn/uhbfnupenuhf/favicon.ico"
/>
</Helmet>
<main>
<div className="title">
Welcome to
<img
className="logo"
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/modern-js-logo.svg"
alt="Modern.js Logo"
/>
<p className="name">Modern.js</p>
</div>
<p className="description">
Get started by editing <code className="code">src/routes/page.tsx</code>
</p>
<div className="grid">
<a
href="https://modernjs.dev/guides/get-started/introduction.html"
target="_blank"
rel="noopener noreferrer"
className="card"
>
<h2>
Guide
<img
className="arrow-right"
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/arrow-right.svg"
/>
</h2>
<p>Follow the guides to use all features of Modern.js.</p>
</a>
<a
href="https://modernjs.dev/tutorials/foundations/introduction.html"
target="_blank"
className="card"
rel="noreferrer"
>
<h2>
Tutorials
<img
className="arrow-right"
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/arrow-right.svg"
/>
</h2>
<p>Learn to use Modern.js to create your first application.</p>
</a>
<a
href="https://modernjs.dev/configure/app/usage.html"
target="_blank"
className="card"
rel="noreferrer"
>
<h2>
Config
<img
className="arrow-right"
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/arrow-right.svg"
/>
</h2>
<p>Find all configuration options provided by Modern.js.</p>
</a>
<a
href="https://github.com/web-infra-dev/modern.js"
target="_blank"
rel="noopener noreferrer"
className="card"
>
<h2>
Github
<img
className="arrow-right"
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/arrow-right.svg"
/>
</h2>
<p>View the source code of Github, feel free to contribute.</p>
</a>
</div>
</main>
<Image />
</div>
);

Expand Down
3 changes: 2 additions & 1 deletion packages/dts-plugin/src/core/configurations/remotePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const readTsConfig = (
typescript.sys,
dirname(resolvedTsConfigPath),
);

const rootDir = getEffectiveRootDir(configContent);

const outDir = resolve(
Expand All @@ -106,9 +105,11 @@ const readTsConfig = (
};

delete rawTsConfigJson.compilerOptions?.paths;
delete rawTsConfigJson.compilerOptions?.baseUrl;

const filesToCompile = [
...Object.values(mapComponentsToExpose),
...configContent.fileNames.filter((filename) => filename.endsWith('.d.ts')),
...additionalFilesToCompile,
];

Expand Down
10 changes: 0 additions & 10 deletions packages/modernjs/src/cli/configPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ export function modifyBundlerConfig<T extends Bundler>(options: {
modernjsConfig,
mfConfig,
});

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

export const moduleFederationConfigPlugin = (
Expand Down
14 changes: 13 additions & 1 deletion packages/modernjs/src/cli/ssrPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const moduleFederationSSRPlugin = (
'@modern-js/plugin-module-federation-config',
'@modern-js/plugin-module-federation',
],
setup: async ({ useConfigContext }) => {
setup: async ({ useConfigContext, useAppContext }) => {
const modernjsConfig = useConfigContext();
const enableSSR = Boolean(modernjsConfig?.server?.ssr);
if (!enableSSR) {
Expand All @@ -43,7 +43,16 @@ export const moduleFederationSSRPlugin = (
return { entrypoint, plugins };
},
config: async () => {
const bundlerType =
useAppContext().bundlerType === 'rspack' ? 'rspack' : 'webpack';

return {
source: {
enableAsyncEntry:
bundlerType === 'rspack'
? modernjsConfig.source?.enableAsyncEntry ?? true
: modernjsConfig.source?.enableAsyncEntry,
},
tools: {
rspack(config, { isServer }) {
if (isServer) {
Expand Down Expand Up @@ -126,6 +135,9 @@ export const moduleFederationSSRPlugin = (
],
},
bundlerChain(chain, { isServer }) {
if (isServer) {
chain.target('async-node');
}
if (isDev && !isServer) {
chain.externals({
'@module-federation/node/utils': 'NOT_USED_IN_BROWSER',
Expand Down
37 changes: 37 additions & 0 deletions packages/modernjs/src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,43 @@ export function patchBundlerConfig<T extends Bundler>(options: {

delete bundlerConfig.optimization?.runtimeChunk;

bundlerConfig.ignoreWarnings = bundlerConfig.ignoreWarnings || [];
const ignoredMsgs = [
'external script',
'process.env.WS_NO_BUFFER_UTIL',
`Can't resolve 'utf-8-validate`,
];
bundlerConfig.ignoreWarnings.push((warning) => {
if (ignoredMsgs.some((msg) => warning.message.includes(msg))) {
return true;
}
return false;
});

bundlerConfig.watchOptions = bundlerConfig.watchOptions || {};
if (!Array.isArray(bundlerConfig.watchOptions.ignored)) {
if (bundlerConfig.watchOptions.ignored) {
bundlerConfig.watchOptions.ignored = [
bundlerConfig.watchOptions.ignored as string,
];
} else {
bundlerConfig.watchOptions.ignored = [];
}
}
if (mfConfig.dts !== false) {
if (
typeof mfConfig.dts === 'object' &&
typeof mfConfig.dts.consumeTypes === 'object' &&
mfConfig.dts.consumeTypes.remoteTypesFolder
) {
bundlerConfig.watchOptions.ignored.push(
mfConfig.dts.consumeTypes.remoteTypesFolder,
);
} else {
bundlerConfig.watchOptions.ignored.push('@mf-types');
}
}

if (bundlerConfig.output) {
if (!bundlerConfig.output?.chunkLoadingGlobal) {
bundlerConfig.output.chunkLoadingGlobal = `chunk_${mfConfig.name}`;
Expand Down
Loading

0 comments on commit d9cb521

Please sign in to comment.