简体   繁体   English

无法将ES6 NPM模块直接导入到Ember CLI中

[英]Cannot import ES6 NPM module directly in to Ember CLI

I'm trying to use d3-arrays in a project. 我正在尝试在项目中使用d3数组 The module itself includes both original ES6 modules and a UMD build. 该模块本身包括原始ES6模块和UMD构建。 I would expect to be able to add this directly as a dependency to my ember-cli project and have it available, but that is not the case. 我希望能够将其直接添加为我的ember-cli项目的依赖项并使其可用,但是事实并非如此。

I've seen suggestions that are over a year old saying to use ember-browserify , and others suggesting making a shim, but AFAIK this would really only be ideal if it were a bower dependency, and bower seems to be on the way out. 我已经看到有超过一年的老建议说使用ember-browserify ,还有其他建议做一个垫片,但AFAIK仅在有Bower依赖的情况下才是理想的选择,并且Bower似乎正在淘汰。

For the sake of correctness, how can I just import this module as though it were part of my project and use it as import {mean} from 'd3-arrays without needing to convert it to another package format first? 为了正确起见,我如何才能像import {mean} from 'd3-arrays项目一样直接导入该模块,并将其用作import {mean} from 'd3-arrays而无需先将其转换为其他包格式?

I've tried making a shim which just exports the imported UMD code: 我尝试制作一个仅导出导入的UMD代码的填充程序:

// index.js
var d3ArraysExports = require('d3-arrays');
d3ArraysExports.name = 'd3-arrays-shim';
module.exports = d3ArraysExports;

Ember finds this module just fine, but it never gets added to the require entries list. Ember认为此模块很好,但是永远不会将其添加到require条目列表中。

If there is some design decision in Ember CLI as to why this doesn't work, please point me to it. 如果Ember CLI中有一些设计决策,说明为什么此方法不起作用,请向我指出。

I figured out an elegant solution to this: 我想出了一个优雅的解决方案:

I created a shim library which imports the ES6 sources from d3-arrays and makes them available to broccoli. 我创建了一个垫片库,该垫片库从d3数组导入了ES6源并将其提供给西兰花。 Here's an example: 这是一个例子:

var path = require('path');

module.exports = {
  name: 'd3-arrays',

  treeForAddon: function() {
    var packagePath = path.join(this.project.addonPackages['ember-d3-arrays-shim'].path, 'node_modules', 'd3-arrays');
    var d3ArraysTree = this.treeGenerator(packagePath);
    return this._super.treeForAddon.call(this, d3ArraysTree);
  }
};

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

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