导入散列集模块不适用于typescript / systemjs?

时间:2017-11-08 16:00:50

标签: javascript typescript systemjs

我正在开展aurelia项目,使用typescript创建javascript。现在我尝试添加另一个自定义库,' hash-set' (使用jspm install npm:hash-set --save)。但是,我似乎无法实际使用此包(使用systemjs作为加载程序)。

我的文档结构如下:

\
dist\
    src\
        app.html
        app.js
        main.js
jsp_packages\
    npm\
        hash-set@1.0.1\
node_modules\
index.html
config.js
package.json
tsconfig.json

重要文件(我想,如果我遗漏了某些内容,请在评论中说明):

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>

  <body aurelia-app="src/main">

    <script src="jspm_packages/system.js"></script>
    <script src="config.js"></script>
    <script>
      SystemJS.import('aurelia-bootstrapper');
    </script>
  </body>
</html>

app.ts 这被编译为app.js作为预建步骤。使用es2015作为目标配置。

import {hashSet} from 'hash-set';

export class App {
  public myText: string;
  hashFn(value) {
    return value.toString();
  }

  constructor() {
    alert("oh");
    const h = hashSet;
    const StringSet = hashSet(this.hashFn);
    alert('oh2');
  }
}

config.js

System.config({
  defaultJSExtensions: true,
  transpiler: false,
  paths: {
    "*": "dist/*",
    "github:*": "jspm_packages/github/*",
    "npm:*": "jspm_packages/npm/*"
  },
  meta: {
    "bootstrap": {
      "deps": [
        "jquery"
      ]
    }
  },
  map: { /*lots of aurelia and other library stuff*/
    "hash-set": "npm:hash-set@1.0.1"
    }
  }
});

它也列在package.json @ {"jspm":{"dependencies":"hash-set": "npm:hash-set@^1.0.1"}}}

现在,当我尝试运行上面的代码(typescript编译为app.js作为预建步骤)时,app.js/app.ts按预期加载。 (坦率地说,删除特定于哈希集的代码会使一切按预期工作)。

然而在施工期间&#34;哦&#34;显示,但&#34; oh2&#34;永远不会。调试代码显示&#34; hashSet&#34;是&#34;未定义&#34;。这让我相信那个系统没有正确包含哈希集? 我错过了什么吗?

<小时/> 编辑:深入研究生成的js(app.js)文件我注意到一些奇怪的事情:

define(["require", "exports", "hash-set"], function (require, exports, hash_set_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    class App {
        hashFn(value) {
            return value.toString();
        }
        constructor() {
            alert('oh');
            const h = hash_set_1.hashSet;
            const StringSet = hash_set_1.hashSet(this.hashFn);
            alert('oh2');
        }
    }
    exports.App = App;
});
//# sourceMappingURL=app.js.map

调试时,hash_set_1实际上是我期望的hash_set_1.hashSet类型。实际上,手动编辑javascript以不使用hash_set_1.hashSet,而只使用hash_set_1

尝试 import hashSet from 'hash-set';(注意缺少{})会将生成的javascript违规行更改为const StringSet = hash_set_1.default(this.hashFn);,但仍然不正确(默认情况下也未定义)。

1 个答案:

答案 0 :(得分:-1)

如果查看code,您会看到它导出为:

module.exports = function hashSet(hashFn) {

执行import { hashSet } from 'hash-set';无效,因为导出必须是module.exports.hashSet = ...

如果您这样做,它应该有效:

import hashSet = require("hash-set");