Skip to content

[Bug]: react ui lib build to Module Federation by esmodule cant be work #1003

@worker-xjm

Description

@worker-xjm

Version

System:
    OS: Linux 5.15 Ubuntu 24.04.2 LTS 24.04.2 LTS (Noble Numbat)
    CPU: (12) x64 Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
    Memory: 12.35 GB / 23.37 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  npmPackages:
    @rslib/core: >=0.5.3 => 0.7.1

Details

Get started quickly with the official Module Federation tutorial
Choose rslib to build UI library

Set lib>format: mf>Plugin>pluginModulus Federation>library>type=module in rslib-config.ts

Unable to package successfully

import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import pkg from './package.json';

const shared = {
  dts: {
    bundle: false,
  },
};

export default defineConfig({
  lib: [
    {
      ...shared,
      format: 'esm',
      output: {
        distPath: {
          root: './dist/esm',
        },
      },
    },
    {
      ...shared,
      format: 'cjs',
      output: {
        distPath: {
          root: './dist/cjs',
        },
      },
    },
    {
      ...shared,
      format: 'mf',
      output: {
        // set unpkg cdn as assetPrefix if you want to publish
        assetPrefix:
          process.env.NODE_ENV === 'production'
            ? `http://localhost:4000`
            : undefined,
        distPath: {
          root: './dist/mf',
        },
      },
      plugins: [
        pluginModuleFederation({
          name: pkg.name,
          // name: 'mf-components', // 组件库名称
          // filename: 'remoteEntry.js', // 远程入口文件名
          exposes: {
            '.': './src/index.tsx',
          },

          shared: {
            react: {
              singleton: true,
            },
            'react-dom': {
              singleton: true,
            },
          },
          library: {
            type: 'module',
          },
        })
      ]
    },
  ],
  server: {
    port: 4000,
  },
  plugins: [pluginReact(),],
});

The following error will occur


[ Module Federation DTS ] Federated types created correctly
error   Build error: 
× JavaScript parsing error: 'import', and 'export' cannot be used outside of module code
      ╭─[5036:0]
 5034 │ var __webpack_exports__get = __webpack_exports__.get;
 5035 │ var __webpack_exports__init = __webpack_exports__.init;
 5036 │ export { __webpack_exports__get as get, __webpack_exports__init as init };
      · ──────
 5037 │ })()
 5038 │ ;
      ╰────

Reproduce link

git@github.com:worker-xjm/rslib-mf-example.git

Reproduce Steps

I hope to support the packaging of esmodule in mf format to meet the needs of vitejs using module federation's esmodule package

At the same time, I hope that MF should not be a single format. MF supports both commonjs and esmodule, and in the future, esmodule should be used instead of traditional cmmonjs following nodejs

希望支持mf格式为esmodule的打包,以满足vitejs使用模块联邦的esmodule包

同时希望mf应该不是一种格式,mf同时支持commonjs和esmodule,未来应该是以esmodule的,而不是遵守nodejs传统cmmonjs

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions