angular - 你如何包含/扩展带有 rollup、typescript、angular 2 的 JS 库

标签 angular typescript ionic2 rollupjs

我在这个项目上构建angular2-google-maps-test我想包含并扩展这个 JS 库 js-marker-clusterer

npm install --save js-marker-clusterer

这似乎没有写成模块

function MarkerClusterer(map, opt_markers, opt_options) {
  // MarkerClusterer implements google.maps.OverlayView interface. We use the
  // extend function to extend MarkerClusterer with google.maps.OverlayView
  // because it might not always be available when the code is defined so we
  // look for it at the last possible moment. If it doesn't exist now then
  // there is no point going ahead :)
  this.extend(MarkerClusterer, google.maps.OverlayView);
  this.map_ = map;
...

}
window['MarkerClusterer'] = MarkerClusterer;

我想做这样的事情:

// js-marker-clusterer.d.ts file
declare module "js-marker-clusterer" {
  export class MarkerClusterer {
    constructor(map: any, opt_markers?: any, opt_options?: any);
    map_: any;
    markers_: any[];
    clusters_: any[];
    ready_: boolean;
    addMarkers(markers: any[], opt_nodraw: boolean) : void;
    removeMarker(marker: any, opt_nodraw: boolean) : boolean;
    removeMarkers(markers: any[], opt_nodraw: boolean) : boolean;
  }
}

然后在 typescript 中扩展该类

/// <reference path="./js-marker-clusterer.d.ts" />
export class MyMarkerClusterer extends MarkerClusterer {
  constructor(map: any, opt_markers?: any, opt_options?: any) {
    super(map, opt_markers, opt_options);
  }   
}

但是在 rollupjs 中我一直收到这个错误:

[21:20:47]  bundle failed: 'MarkerClusterer' is not exported by node_modules/js-marker-clusterer/src/markerclusterer.js  MEM: 469.6MB
            (imported by src/angular2-marker-clusterer/my-marker-clusterer.ts). For help fixing this 
            error see https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module

我的猜测是我需要向 rollup.config.js 文件中添加一些内容,但我尝试将其作为 plugin 添加,但没有成功。

  /**
   * plugins: Array of plugin objects, or a single plugin object.
   * See https://github.com/rollup/rollup/wiki/Plugins for more info.
   */
  plugins: [
    builtins(),
    //commonjs(),
    commonjs({
      namedExports: {
        'node_modules/angular2-google-maps/core/index.js': ['AgmCoreModule'],
        'node_modules/js-marker-clusterer/src/markerclusterer.js': ['MarkerClusterer']
      }
    }),

最佳答案

实际上,我不知道 Typescript 部分,但我认为您应该尝试使用 rollup-plugin-legacy 而不是 commonjs 插件.

legacy({
    'node_modules/js-marker-clusterer/src/markerclusterer.js': 'MarkerClusterer'
})

请记住相应地更改导入语句。

关于angular - 你如何包含/扩展带有 rollup、typescript、angular 2 的 JS 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40225851/

相关文章:

css - Ionic 以编程方式设置焦点

angular - Ionic 3从app.component类重定向到另一个页面

angular - 使用来自 Observable 的 startWith 创建 Observable

angular - 在 Angular 2 中格式化日期时间

node.js - 无法使用 Express API 和 Node JS(Angular4) 将数据发布到 mongo db

angular - 我的应用程序的每一页上都使用一个组件。我如何声明它以便在全局范围内使用?

javascript - 如何从 typescript 中的对象中分离键和值对

typescript - 在 typescript 中扩展类时编译错误

javascript - HTML 表格上的枢轴对象

angular - ngx-translate:在单元测试中翻译字符串