简体   繁体   English

在 ember 插件中管理第三方依赖项的正确方法是什么?

[英]What's the right way to manage a third party dependency in an ember addon?

I'm working on an emberjs application that uses several other custom ember addons, one of which we're adding is a new dependency on mathjs ( https://mathjs.org/ ) to do some expression parsing.我正在开发一个使用其他几个自定义 ember 插件的 emberjs 应用程序,我们要添加的插件之一是对 mathjs ( https://mathjs.org/ ) 的新依赖,以进行一些表达式解析。 It isn't clear though what is the correct way to keep the configuration of that dependency on mathjs with only the addon that uses it.目前尚不清楚仅使用使用它的插件来保持对 mathjs 的依赖配置的正确方法是什么。 Is there a "right" way to do this in ember?在余烬中有“正确”的方法吗?

What we ended up doing was just installing the mathjs package into our main app's node_modules, then exposing the library in the browser by doing an app.import('node_modules/mathjs/dist/math.min.js') in our ember-cli-build.js file.我们最终做的只是将 mathjs package 安装到我们的主应用程序的 node_modules 中,然后通过在我们的 ember-cli 中执行app.import('node_modules/mathjs/dist/math.min.js')在浏览器中公开该库-build.js 文件。 This is obviously non-ideal because it means that any app using the custom addon must also do this extra setup to expose this dependency of the addon.这显然是不理想的,因为这意味着任何使用自定义插件的应用程序也必须执行此额外设置以公开插件的这种依赖性。

FWIW, we initially tried to just install the mathjs dependency into the addon's node_modules, then we imported it in the component of the addon where it's used. FWIW,我们最初尝试将 mathjs 依赖项安装到插件的 node_modules 中,然后将其导入到使用它的插件组件中。 But then the browser said that the mathjs module couldn't be imported from the addon.但是然后浏览器说无法从插件中导入 mathjs 模块。

You should let ember-auto-import manage this for you.你应该让ember-auto-import为你管理它。 For this:为了这:

  • add math.js to your dependencies of your addon (not devDependencies ).math.js添加到插件的dependencies项中(不是devDependencies )。
  • use import syntax to import it in your addon.使用import语法将其导入到您的插件中。

For the import syntax you need to be aware that some modules only have a default export which is a object exporting properties while others have individual exports.对于import语法,您需要注意某些模块只有一个默认导出,即 object 导出属性,而其他模块则有单独的导出。 So its either所以它要么

import { pi, atan2 } from 'mathjs' or import mathjs from 'mathjs' . import { pi, atan2 } from 'mathjs'import mathjs from 'mathjs'

your host app should then have your addon in devDependencies .然后你的主机应用程序应该在devDependencies中有你的插件。

Only use app.import syntax if you use something that does not support modules at all.如果您使用根本不支持模块的东西,则只使用app.import语法。

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

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