简体   繁体   English

使用Webpack,如何动态加载使用另一个Webpack构建的外部模块

[英]Using Webpack, how to dynamically load an external module built with another Webpack

Let's say I manage 2 JavaScript projects both built with Webpack:假设我管理着 2 个使用 Webpack 构建的 JavaScript 项目:

  • A Website called User-Website一个名为User-Web 的网站
  • A JavaScript module called External-Module一个名为External-Module 的JavaScript模块

Note that I'm using 2 separate projets for the same reasons described by the Micro Front end architecture.请注意,出于与微前端架构所述相同的原因,我使用了 2 个单独的项目。

I want my User-Website to be able to dynamically load External-Module on demand (using any JavaScript module technology).我希望我的用户网站能够按需动态加载外部模块(使用任何 JavaScript 模块技术)。 My User-Website knows at build-time the URL where to reach External-Module .我的用户网站在构建时知道到达External-Module的 URL。

I can chose whatever technology is needed on both User-Website and External-Module .我可以选择User-WebsiteExternal-Module上需要的任何技术。

I'm looking for a solution:我正在寻找解决方案:

  • That is easy to implement (maybe JSONP that Webpack already handles to dynamically load chunks?)这很容易实现(也许 Webpack 已经处理了动态加载块的 JSONP?)
  • That doesn't add to much overhead on both User-Website and External-Module (For example JavaScript modules looks good but requires a lot of polyfilling)这不会给用户网站外部模块增加太多开销(例如JavaScript 模块看起来不错,但需要大量的 polyfilling)

My question is related to https://github.com/webpack/webpack/issues/7526我的问题与https://github.com/webpack/webpack/issues/7526有关


I tried to use JSONP library output on my External-Module but I don't know how to load it in User-Website .我试图在我的External-Module上使用 JSONP 库输出,但我不知道如何在User-Website 中加载它。

I'm also thinking about using the SystemJS in User-Website to dynamically load External-Module :我也在考虑在User-Website 中使用SystemJS来动态加载External-Module

  • I could also replace internal JSONP mechanism with SystemJS within Webpack (to save having JSONP mechanism in m bundles).我还可以在 Webpack 中用 SystemJS 替换内部 JSONP 机制(以节省在 m 个包中使用 JSONP 机制)。
  • SystemJS looks better and more modern than RequireJS SystemJS看起来比RequireJS更好更现代
  • This will require to add SystemJS ( s.js only) overhead.. I'm trying to use less dependencies as possible.这将需要添加SystemJS (仅s.js )开销。我正在尝试使用尽可能少的依赖项。

I'm sorry if I did not get the question but I've recently made a project based on the article from the link You've provided.如果我没有得到这个问题,我很抱歉,但我最近根据你提供的链接中的文章做了一个项目。 Can't You just host both of the applications and load the part of chunk.js on the runtime ?你不能只托管两个应用程序并在运行时加载chunk.js的一部分吗? It's how this particular article's example works.这就是这篇特定文章的示例的工作原理。 There is an example provided in the article.文章中提供了一个例子。 Some restaurant app with different microfrontends loaded dynamically on the runtime.一些具有不同微前端的餐厅应用程序在运行时动态加载。 Isn't it exactly what You need ?这不正是您所需要的吗?

Below code is the 'heart' of this particular example.下面的代码是这个特定示例的“核心”。 Take a look.看一看。

componentDidMount() {
    const { name, host } = this.props;
    const scriptId = `micro-frontend-script-${name}`;

    if (document.getElementById(scriptId)) {
      this.renderMicroFrontend();
      return;
    }

    fetch(`${host}/asset-manifest.json`)
      .then(res => res.json())
      .then(manifest => {
        const script = document.createElement('script');
        script.id = scriptId;
        script.src = `${host}${manifest['main.js']}`;
        script.onload = this.renderMicroFrontend;
        document.head.appendChild(script);
      });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM