简体   繁体   English

如何在打字稿中包含外部库

[英]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. 您可以使用browserify或webpack在浏览器中使用nodejs软件包。 FYI Facebook uses webpack. 仅供参考Facebook使用webpack。

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

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