single-spa 在 Ember 中使用共享的实用模块

huangapple go评论66阅读模式
英文:

single-spa consume shared utility module in ember

问题

我想在React和Ember应用程序中使用single-spa,它们需要共享数据,我想使用https://single-spa.js.org/docs/module-types/#utilities 实用程序模块来公开可观察数据。

在React项目中,实用程序模块通过webpack的externals选项进行使用。

React的webpack配置:

module.exports = (webpackConfigEnv, argv) => {
  const defaultConfig = singleSpaDefaults({
    orgName: "company",
    projectName: "react-app",
    webpackConfigEnv,
    argv,
  });

  return merge(defaultConfig, {
    externals: {
      "react": "react",
      "react-dom": "react-dom",
      // 实用程序模块 
      "@company/utility": "@company/utility"
    }
  });
};

react中这样配置可以正常工作,但是Ember使用ember-cli/broccoli来构建应用程序,我不知道如何使用实用程序包,是否可能?或者使用npm发布包或在注册Ember微前端时通过props公开功能是唯一的方法,例如:

registerApplication(
  "ember",
  () => {
    const appName = "ember";
    const appUrl = `${domain}/ember/assets/ember.js`;
    const vendorUrl = `${domain}/ember/assets/vendor.js`;
    console.log({ appName, appUrl, vendorUrl });
    return loadEmberApp(appName, appUrl, vendorUrl);
  },
  (location) => {
    console.log(location.pathname.startsWith("/ember"))
    return location.pathname.startsWith("/ember");
  },
  { // 所有来自实用程序模块的数据应该在这里公开 }
);
英文:

I want to use single-spa with react and ember apps that will need to share data between them, I would like to use https://single-spa.js.org/docs/module-types/#utilities utility module for exposing observable data.

In react project utility module is consumed via webpacks externals option

reacts webpack:

module.exports = (webpackConfigEnv, argv) => {
  const defaultConfig = singleSpaDefaults({
    orgName: "company",
    projectName: "react-app",
    webpackConfigEnv,
    argv,
  });

  return merge(defaultConfig, {
      externals: {
        "react": "react",
        "react-dom": "react-dom",
        // utility module 
        "@company/utility": "@company/utility"
      }
  });
};

And it works just fine in react, but ember uses ember-cli/broccoli for building an app and I don't know how to consume utility package, is it even possible? Or the only way to use package is to publish it via npm or expose functionality via props when registering ember mf in root-config for example:

registerApplication(
  "ember",
  () => {
    const appName = "ember";
    const appUrl = `${domain}/ember/assets/ember.js`;
    const vendorUrl = `${domain}/ember/assets/vendor.js`;
    console.log({ appName, appUrl, vendorUrl });
    return loadEmberApp(appName, appUrl, vendorUrl);
  },
  (location) => {
    console.log(location.pathname.startsWith("/ember"))
    return location.pathname.startsWith("/ember");
  },
  { // all data from utility module should be exposed here }
);

答案1

得分: 1

这将在接下来的几个月内可能实现,因为即将发布 embroider 的新版本(前提是你的应用程序已经兼容今天的 embroider)。

今天,你需要单独构建你的 Ember 应用,并将你的配置指向 ember-cli 输出的 dist 文件夹。

有关 embroider 的更多信息,这是从 broccoli 过渡到现代 JS 工具的工具,请参见此处:https://github.com/embroider-build/embroider/

英文:

this'll be possible in the next couple months, with an upcoming embroider release (provided your app is already compatible with today's embroider).

Today, you'll need to build your ember app separately, and point your config at the dist folder that ember-cli outputs.

For more information embroider, the tool for transitioning from broccoli to modern JS tooling, see here: https://github.com/embroider-build/embroider/

huangapple
  • 本文由 发表于 2023年5月29日 23:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定