diff --git a/packages/chrome-devtools/e2e/index.spec.ts b/packages/chrome-devtools/e2e/index.spec.ts index 317602dc7d3..c44985f81f4 100644 --- a/packages/chrome-devtools/e2e/index.spec.ts +++ b/packages/chrome-devtools/e2e/index.spec.ts @@ -79,22 +79,6 @@ test('test proxy', async ({ request }) => { targetPage.removeListener('request', beforeHandler); await sleep(3000); - // Check the page proxy status - let targetPageModuleInfo = await targetPage.evaluate(() => { - return (window as any)?.__FEDERATION__?.moduleInfo ?? {}; - }); - - expect(targetPageModuleInfo).toMatchObject({ - manifest_host: { - remotesInfo: { - webpack_provider: { - matchedVersion: proxyUrl, - }, - }, - }, - }); - await sleep(3000); - // Setting proxy logic const addButton = devtoolsPage.locator('[data-set-e2e=e2eAdd]'); await expect(addButton).toBeVisible({ timeout: 60000 }); @@ -130,27 +114,17 @@ test('test proxy', async ({ request }) => { await sleep(3000); await targetPage.bringToFront(); + await expect + .poll(() => afterProxyRequest.includes(mockUrl), { + timeout: 60000, + intervals: [500, 1000, 2000], + }) + .toBe(true); - expect(beforeProxyRequest).toContain(proxyUrl); expect(beforeProxyRequest).not.toContain(mockUrl); expect(afterProxyRequest).toContain(mockUrl); expect(afterProxyRequest).not.toContain(proxyUrl); - // check proxy snapshot - let targetPageModuleInfoNew = await targetPage.evaluate(() => { - return (window as any)?.__FEDERATION__?.moduleInfo ?? {}; - }); - - expect(targetPageModuleInfoNew).toMatchObject({ - manifest_host: { - remotesInfo: { - webpack_provider: { - matchedVersion: mockUrl, - }, - }, - }, - }); - console.log(beforeProxyRequest, afterProxyRequest); }); diff --git a/packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts b/packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts index 88a3e4c04f5..14ce18bffaa 100644 --- a/packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts +++ b/packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts @@ -34,17 +34,47 @@ const { mkdirpSync } = require( normalizeWebpackPath('webpack/lib/util/fs'), ) as typeof import('webpack/lib/util/fs'); -const RuntimeToolsPath = require.resolve( +function resolveModule( + candidates: string[], + options?: NodeJS.RequireResolveOptions, +): string { + let lastError: unknown; + for (const candidate of candidates) { + try { + return require.resolve(candidate, options); + } catch (error) { + lastError = error; + } + } + + throw ( + lastError ?? + new Error(`Unable to resolve any module from: ${candidates.join(', ')}`) + ); +} + +const RuntimeToolsPath = resolveModule([ + '@module-federation/runtime-tools', '@module-federation/runtime-tools/dist/index.esm.js', -); -const BundlerRuntimePath = require.resolve( - '@module-federation/webpack-bundler-runtime/dist/index.esm.js', + '@module-federation/runtime-tools/dist/index.cjs.cjs', +]); +const BundlerRuntimePath = resolveModule( + [ + '@module-federation/webpack-bundler-runtime', + '@module-federation/webpack-bundler-runtime/dist/index.js', + '@module-federation/webpack-bundler-runtime/dist/index.esm.js', + '@module-federation/webpack-bundler-runtime/dist/index.cjs.cjs', + ], { paths: [RuntimeToolsPath], }, ); -const RuntimePath = require.resolve( - '@module-federation/runtime/dist/index.esm.js', +const RuntimePath = resolveModule( + [ + '@module-federation/runtime', + '@module-federation/runtime/dist/index.esm.js', + '@module-federation/runtime/dist/index.cjs.cjs', + ], { paths: [RuntimeToolsPath], }, @@ -431,8 +461,13 @@ class FederationRuntimePlugin { } if (this.options?.implementation) { - this.bundlerRuntimePath = require.resolve( - '@module-federation/webpack-bundler-runtime/dist/index.esm.js', + this.bundlerRuntimePath = resolveModule( + [ + '@module-federation/webpack-bundler-runtime', + '@module-federation/webpack-bundler-runtime/dist/index.js', + '@module-federation/webpack-bundler-runtime/dist/index.esm.js', + '@module-federation/webpack-bundler-runtime/dist/index.cjs.cjs', + ], { paths: [this.options.implementation], }, diff --git a/packages/error-codes/package.json b/packages/error-codes/package.json index a2cb5f38b80..26f6c6bb4fb 100644 --- a/packages/error-codes/package.json +++ b/packages/error-codes/package.json @@ -22,6 +22,9 @@ "publishConfig": { "access": "public" }, + "browser": { + "url": false + }, "main": "./dist/index.cjs.js", "module": "./dist/index.esm.mjs", "types": "./dist/index.d.ts", diff --git a/packages/runtime-plugins/inject-external-runtime-core-plugin/package.json b/packages/runtime-plugins/inject-external-runtime-core-plugin/package.json index 83c646dd01f..31d3f40074e 100644 --- a/packages/runtime-plugins/inject-external-runtime-core-plugin/package.json +++ b/packages/runtime-plugins/inject-external-runtime-core-plugin/package.json @@ -22,20 +22,28 @@ }, "author": "zhanghang ", "sideEffects": false, - "main": "./dist/index.cjs.cjs", - "module": "./dist/index.esm.js", - "types": "./dist/index.cjs.d.ts", + "main": "./dist/index.cjs", + "module": "./dist/index.js", + "types": "./dist/index.d.ts", + "browser": { + "url": false + }, "exports": { ".": { - "types": "./dist/index.cjs.d.ts", - "import": "./dist/index.esm.js", - "require": "./dist/index.cjs.cjs" + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "require": { + "types": "./dist/index.d.ts", + "default": "./dist/index.cjs" + } } }, "typesVersions": { "*": { ".": [ - "./dist/index.cjs.d.ts" + "./dist/index.d.ts" ] } }, diff --git a/packages/runtime-plugins/inject-external-runtime-core-plugin/project.json b/packages/runtime-plugins/inject-external-runtime-core-plugin/project.json index a619dbd3e2e..0975da1d25d 100644 --- a/packages/runtime-plugins/inject-external-runtime-core-plugin/project.json +++ b/packages/runtime-plugins/inject-external-runtime-core-plugin/project.json @@ -6,20 +6,20 @@ "tags": ["type:pkg"], "targets": { "build": { - "executor": "@nx/rollup:rollup", - "outputs": ["{options.outputPath}"], + "executor": "nx:run-commands", + "outputs": [ + "{workspaceRoot}/packages/runtime-plugins/inject-external-runtime-core-plugin/dist" + ], "options": { - "outputPath": "packages/runtime-plugins/inject-external-runtime-core-plugin/dist", - "main": "packages/runtime-plugins/inject-external-runtime-core-plugin/src/index.ts", - "tsConfig": "packages/runtime-plugins/inject-external-runtime-core-plugin/tsconfig.lib.json", - "assets": [], - "project": "packages/runtime-plugins/inject-external-runtime-core-plugin/package.json", - "rollupConfig": "packages/runtime-plugins/inject-external-runtime-core-plugin/rollup.config.cjs", - "compiler": "tsc", - "generatePackageJson": false, - "format": ["cjs", "esm"], - "useLegacyTypescriptPlugin": false - } + "command": "rslib build", + "cwd": "packages/runtime-plugins/inject-external-runtime-core-plugin" + }, + "dependsOn": [ + { + "target": "build", + "dependencies": true + } + ] }, "lint": { "executor": "@nx/eslint:lint", diff --git a/packages/runtime-plugins/inject-external-runtime-core-plugin/rslib.config.ts b/packages/runtime-plugins/inject-external-runtime-core-plugin/rslib.config.ts new file mode 100644 index 00000000000..9310d589fbb --- /dev/null +++ b/packages/runtime-plugins/inject-external-runtime-core-plugin/rslib.config.ts @@ -0,0 +1,56 @@ +import { defineConfig } from '@rslib/core'; +import { readFileSync } from 'fs'; +import { join } from 'path'; + +const pkg = JSON.parse( + readFileSync(join(process.cwd(), 'package.json'), 'utf-8'), +); + +export default defineConfig({ + lib: [ + { + format: 'esm', + syntax: 'es2021', + bundle: false, + outBase: 'src', + dts: { + bundle: false, + distPath: './dist', + }, + }, + { + format: 'cjs', + syntax: 'es2021', + bundle: false, + outBase: 'src', + dts: false, + }, + ], + source: { + entry: { + index: [ + './src/**/*.{ts,tsx,js,jsx}', + '!./src/**/*.spec.*', + '!./src/**/*.test.*', + ], + }, + define: { + __VERSION__: JSON.stringify(pkg.version), + }, + tsconfigPath: './tsconfig.lib.json', + }, + output: { + target: 'node', + minify: false, + distPath: { + root: './dist', + }, + externals: [/@module-federation\//], + copy: [ + { + from: './LICENSE', + to: '.', + }, + ], + }, +}); diff --git a/packages/runtime-plugins/inject-external-runtime-core-plugin/tsconfig.lib.json b/packages/runtime-plugins/inject-external-runtime-core-plugin/tsconfig.lib.json index 33eca2c2cdf..4b75aca1f6b 100644 --- a/packages/runtime-plugins/inject-external-runtime-core-plugin/tsconfig.lib.json +++ b/packages/runtime-plugins/inject-external-runtime-core-plugin/tsconfig.lib.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "../../dist/out-tsc", + "rootDir": "./src", "declaration": true, "types": ["node"] }, diff --git a/packages/runtime-tools/package.json b/packages/runtime-tools/package.json index 9b294ed9206..1f211233f90 100644 --- a/packages/runtime-tools/package.json +++ b/packages/runtime-tools/package.json @@ -6,6 +6,9 @@ "main": "./dist/index.cjs.cjs", "module": "./dist/index.esm.js", "types": "./dist/index.d.ts", + "browser": { + "url": false + }, "license": "MIT", "repository": { "type": "git", diff --git a/packages/runtime-tools/src/webpack-bundler-runtime.ts b/packages/runtime-tools/src/webpack-bundler-runtime.ts index 7ada0a527ca..bf870059e25 100644 --- a/packages/runtime-tools/src/webpack-bundler-runtime.ts +++ b/packages/runtime-tools/src/webpack-bundler-runtime.ts @@ -1 +1,8 @@ -export { default } from '@module-federation/webpack-bundler-runtime'; +import webpackBundlerRuntime from '@module-federation/webpack-bundler-runtime'; + +const normalizedWebpackBundlerRuntime = + // Support both CJS module.exports payload and transpiled default payload. + (webpackBundlerRuntime as { default?: unknown }).default ?? + webpackBundlerRuntime; + +export default normalizedWebpackBundlerRuntime; diff --git a/packages/webpack-bundler-runtime/package.json b/packages/webpack-bundler-runtime/package.json index 308cc678a8d..f7c8c46dc7e 100644 --- a/packages/webpack-bundler-runtime/package.json +++ b/packages/webpack-bundler-runtime/package.json @@ -25,6 +25,9 @@ "main": "./dist/index.cjs.cjs", "module": "./dist/index.esm.js", "types": "./dist/index.d.ts", + "browser": { + "url": false + }, "dependencies": { "@module-federation/runtime": "workspace:*", "@module-federation/sdk": "workspace:*" diff --git a/packages/webpack-bundler-runtime/src/index.ts b/packages/webpack-bundler-runtime/src/index.ts index d611983f45a..dc9b130f0eb 100644 --- a/packages/webpack-bundler-runtime/src/index.ts +++ b/packages/webpack-bundler-runtime/src/index.ts @@ -28,4 +28,12 @@ const federation: Federation = { attachShareScopeMap, bundlerRuntimeOptions: {}, }; + +// Keep CJS interop stable for consumers that iterate required keys directly. +export { runtime, attachShareScopeMap }; +export const instance = federation.instance; +export const initOptions = federation.initOptions; +export const bundlerRuntime = federation.bundlerRuntime; +export const bundlerRuntimeOptions = federation.bundlerRuntimeOptions; + export default federation;