如何使用汇总,typescript,angular2包含/扩展JS库

时间:2016-10-24 19:16:53

标签: angular typescript ionic2 rollupjs

我在这个项目angular2-google-maps-test上构建,我希望包含并扩展这个JS lib js-marker-clusterer

npm install --save js-marker-clusterer

似乎没有写成module

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']
      }
    }),

1 个答案:

答案 0 :(得分:0)

实际上,我不了解Typescript部分,但我认为应该尝试使用rollup-plugin-legacy而不是commonjs插件。

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

请记住相应地更改import语句。