简体   繁体   中英

How can one include external libraries in typescript

I would like to be able to create a library that is dependent on other libraries that works in both a node and in a browser environment.

Contrived example:

module Core.Libraries {
   export var lodash = require('lodash');
}

Usage:

var lodash = require('../../../../dist/core').Libraries.lodash;

var expect = require('chai').expect;

describe('Core.Libraries.lodash', function () {
  it('should exist', function () {
    expect(lodash).to.be.ok;
  });

  it('should be lodash', function () {
    var result = lodash([1, 2, 3])
      .map(function (i) {
        return i * 2;
      })
      .reduce(function (sum, current) {
        return sum + current;
      });

    expect(result).to.equal(12);
  });
});

But in the browser this does not work as it misses require. It works in node.

But in the browser this does not work as it misses require. It works in node.

you can use nodejs packages in the browser using browserify OR webpack. FYI Facebook uses webpack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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