Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rspack): support getPublicPath #3422

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-geckos-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/rspack': patch
---

fix(rspack): support getPublicPath
2 changes: 0 additions & 2 deletions apps/router-demo/router-host-vue3-2100/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default defineConfig({
port: 2100,
},
dev: {
// It is necessary to configure assetPrefix, and in the production environment, you need to configure output.assetPrefix
assetPrefix: 'http://localhost:2100',
writeToDisk: true,
},
plugins: [
Expand Down
7 changes: 0 additions & 7 deletions apps/router-demo/router-remote1-2001/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ export default defineConfig({
port: 2001,
},
dev: {
// It is necessary to configure assetPrefix, and in the production environment, you need to configure output.assetPrefix
assetPrefix: 'http://localhost:2001',
writeToDisk: true,
},
tools: {
rspack: (config, { appendPlugins }) => {
delete config.optimization?.splitChunks;
},
},
plugins: [
pluginReact(),
pluginModuleFederation({
Expand Down
22 changes: 0 additions & 22 deletions apps/router-demo/router-remote2-2002/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ export default defineConfig({
server: {
port: 2002,
},
dev: {
// It is necessary to configure assetPrefix, and in the production environment, you need to configure output.assetPrefix
assetPrefix: 'http://localhost:2002',
},
tools: {
// cssExtract: {
// pluginOptions: {
// insert(element) {
// console.log('element22222233333333333333',document.querySelector("#remote2-style-component"));
// document.querySelector("#remote2-style-component")?.appendChild(element);
// },
// }
// },
// styleLoader: {
// insert: (element)=> {
// debugger
// const styleContainer = document.querySelector(".remote2_export-app") || document.head;
// styleContainer.appendChild(element);
// console.log('styleContainer', styleContainer)
// },
// },
},
plugins: [
pluginReact(),
pluginModuleFederation({
Expand Down
8 changes: 0 additions & 8 deletions apps/router-demo/router-remote3-2003/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@ export default defineConfig({
port: 2003,
},
dev: {
// It is necessary to configure assetPrefix, and in the production environment, you need to configure output.assetPrefix
assetPrefix: 'http://localhost:2003',
writeToDisk: true,
},
tools: {
rspack: (config, { appendPlugins }) => {
delete config.optimization?.splitChunks;
},
},

plugins: [
pluginVue(),
pluginModuleFederation({
Expand Down
29 changes: 4 additions & 25 deletions apps/router-demo/router-remote4-2004/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,11 @@ export default defineConfig({
'react-dom': path.resolve(__dirname, 'node_modules/react-dom'),
},
},
server: {
port: 2004,
},
dev: {
// It is necessary to configure assetPrefix, and in the production environment, you need to configure output.assetPrefix
assetPrefix: 'http://localhost:2004',
writeToDisk: true,
},
tools: {
rspack: (config, { appendPlugins }) => {
delete config.optimization?.splitChunks;
},
// cssExtract: {
// pluginOptions: {
// insert(element) {
// console.log('element22222233333333333333',document.querySelector("#remote2-style-component"));
// document.querySelector("#remote2-style-component")?.appendChild(element);
// },
// }
// },
// styleLoader: {
// insert: (element)=> {
// debugger
// const styleContainer = document.querySelector(".remote2_export-app") || document.head;
// styleContainer.appendChild(element);
// console.log('styleContainer', styleContainer)
// },
// },
server: {
port: 2004,
},
plugins: [
pluginReact(),
Expand All @@ -50,6 +28,7 @@ export default defineConfig({
'./export-app': './src/export-App.tsx',
},
shared: ['react', 'react-dom'],
getPublicPath: `return 'http://localhost:2004/'`,
}),
],
});
1 change: 0 additions & 1 deletion packages/data-prefetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"skipLibCheck": true,
"moduleResolution": "node",
"allowJs": false,
"strict": true,
"types": ["jest", "node"],
"experimentalDecorators": true,
"resolveJsonModule": true,
Expand Down
25 changes: 23 additions & 2 deletions packages/enhanced/src/lib/container/runtime/RemoteEntryPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import type { Compiler, WebpackPluginInstance } from 'webpack';
import pBtoa from 'btoa';

const charMap: Record<string, string> = {
'<': '\\u003C',
'>': '\\u003E',
'/': '\\u002F',
'\\': '\\\\',
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
'\0': '\\0',
'\u2028': '\\u2028',
'\u2029': '\\u2029',
};

function escapeUnsafeChars(str: string) {
return str.replace(/[<>\b\f\n\r\t\0\u2028\u2029]/g, (x) => charMap[x]);
Fixed Show fixed Hide fixed
}

export class RemoteEntryPlugin implements WebpackPluginInstance {
readonly name = 'VmokRemoteEntryPlugin';
private _name: string;
Expand All @@ -13,12 +32,14 @@

apply(compiler: Compiler): void {
let code;
const sanitizedPublicPath = escapeUnsafeChars(this._getPublicPath);

if (!this._getPublicPath.startsWith('function')) {
code = `${
compiler.webpack.RuntimeGlobals.publicPath
} = new Function(${JSON.stringify(this._getPublicPath)})()`;
} = new Function(${JSON.stringify(sanitizedPublicPath)})()`;
Fixed Show fixed Hide fixed
} else {
code = `(${this._getPublicPath})()`;
code = `(${sanitizedPublicPath})()`;
}
const base64Code = pBtoa(code);
const dataUrl = `data:text/javascript;base64,${base64Code}`;
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"module": "./dist/index.esm.mjs",
"types": "./dist/index.cjs.d.ts",
"dependencies": {
"btoa": "1.2.1",
"@module-federation/bridge-react-webpack-plugin": "workspace:*",
"@module-federation/dts-plugin": "workspace:*",
"@module-federation/managers": "workspace:*",
Expand All @@ -33,6 +34,7 @@
"@module-federation/sdk": "workspace:*"
},
"devDependencies": {
"@types/btoa": "1.2.5",
"@rspack/core": "^1.0.2"
},
"exports": {
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DtsPlugin } from '@module-federation/dts-plugin';
import ReactBridgePlugin from '@module-federation/bridge-react-webpack-plugin';
import path from 'node:path';
import fs from 'node:fs';
import { RemoteEntryPlugin } from './RemoteEntryPlugin';

type ExcludeFalse<T> = T extends undefined | false ? never : T;
type SplitChunks = Compiler['options']['optimization']['splitChunks'];
Expand Down Expand Up @@ -75,6 +76,13 @@ export class ModuleFederationPlugin implements RspackPluginInstance {
this._patchChunkSplit(compiler, options.name);
}

// must before ModuleFederationPlugin
if (options.getPublicPath && options.name) {
new RemoteEntryPlugin(options.name, options.getPublicPath).apply(
compiler,
);
}

if (options.experiments?.provideExternalRuntime) {
if (options.exposes) {
throw new Error(
Expand Down
47 changes: 47 additions & 0 deletions packages/rspack/src/RemoteEntryPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Compiler, RspackPluginInstance } from '@rspack/core';
import pBtoa from 'btoa';

const charMap = {
'<': '\\u003C',
'>': '\\u003E',
'/': '\\u002F',
'\\': '\\\\',
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
'\0': '\\0',
'\u2028': '\\u2028',
'\u2029': '\\u2029',
};

function escapeUnsafeChars(str) {
return str.replace(/[<>\b\f\n\r\t\0\u2028\u2029]/g, (x) => charMap[x]);
Fixed Show fixed Hide fixed
}

export class RemoteEntryPlugin implements RspackPluginInstance {
readonly name = 'VmokRemoteEntryPlugin';
private _name: string;
private _getPublicPath: string;

constructor(name: string, getPublicPath: string) {
this._name = name;
this._getPublicPath = getPublicPath;
}

apply(compiler: Compiler): void {
const sanitizedPublicPath = escapeUnsafeChars(this._getPublicPath);
const code = `${
compiler.webpack.RuntimeGlobals.publicPath
} = new Function(${JSON.stringify(sanitizedPublicPath)})()`;
Fixed Show fixed Hide fixed
const base64Code = pBtoa(code);
const dataUrl = `data:text/javascript;base64,${base64Code}`;

compiler.hooks.afterPlugins.tap('VmokRemoteEntryPlugin', () => {
new compiler.webpack.EntryPlugin(compiler.context, dataUrl, {
name: this._name,
}).apply(compiler);
});
}
}
1 change: 0 additions & 1 deletion packages/rspack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
"tsConfig": "packages/runtime-plugins/inject-external-runtime-core-plugin/tsconfig.lib.json",
"assets": [],
"project": "packages/runtime-plugins/inject-external-runtime-core-plugin/package.json",
"additionalEntryPoints": [
"packages/runtime-plugins/inject-external-runtime-core-plugin/src/normalize-webpack-path.ts"
],
"rollupConfig": "packages/runtime-plugins/inject-external-runtime-core-plugin/rollup.config.js",
"compiler": "swc",
"generatePackageJson": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
Expand Down
Loading
Loading