简体   繁体   English

打字稿:导入jspm库

[英]typescript: import jspm libraries

So most examples I found on importing jspm packages to typescript assumed that I wanted to use Systemjs to load and interpret them in the browser. 因此,我在将jspm软件包导入typescript时发现的大多数示例都假设我想使用Systemjs在浏览器中加载和解释它们。 However, I would rather like to use tsc to build commonjs modules and only import the js code, since it seems to me to be the more general and error-resistent approach to me. 但是,我想使用tsc来构建commonjs模块并仅导入js代码,因为对我来说这似乎是更通用且更抗错误的方法。

So my directory structure looks like this: 所以我的目录结构如下所示:

src/index.ts
jspm_packages/...
config.js
tsconfig.json

With tsconfig having the following content: tsconfig具有以下内容:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "rootDir": "src",
    "outDir": "target/app",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declaration": true
  },
  "exclude": [
    "jspm_packages",
    "node_modules",
    "typings",
    "target"
  ]
}

For testing purposes, I installed angular 2 with jspm install npm:angular2 and tried to import it in my index.ts via import { bootstrap } from 'angular2/platform/browser'; 为了进行测试,我将jspm install npm:angular2 2与jspm install npm:angular2并尝试import { bootstrap } from 'angular2/platform/browser';通过import { bootstrap } from 'angular2/platform/browser';将其导入我的index.ts import { bootstrap } from 'angular2/platform/browser';

When running tsc , I get the error 运行tsc出现错误

src/index.ts(1,27): error TS2307: Cannot find module 'angular2/platform/browser'.

Now I wonder, can I make jspm-packages known to typescript? 现在我想知道,我可以让打字稿知道jspm包吗? I feel like I tried it all, removing the jspm_packages from the tsconfig exclude list, switching to node module resolution or systemjs module generation. 我觉得我已经尝试了全部,从tsconfig exclude列表中删除了jspm_packages,切换到节点模块解析或systemjs模块生成。 Maybe I just haven't found the right combination. 也许我只是找不到合适的组合。 Any hints on what to try next? 关于下一步尝试的任何提示?

I am also struggling with this same issue and after some research, I learned that there no good solutions to allow this as of yet. 我也在同一个问题上苦苦挣扎,经过一番研究,我知道到目前为止还没有好的解决方案。

Workarounds: 解决方法:

A) You can duplicate your dependencies and install it with npm. A)您可以复制依赖项,并使用npm进行安装。 tsc should not throw any errors since its resolving from npm. 自从npm解析以来,tsc不应引发任何错误。

B) Change your tsconfig.json and map angular to the jspm path. B)更改您的tsconfig.json并将angular映射到jspm路径。 For example 例如

"baseUrl": ".",
"paths": {
     "angular2/*": [
        "jspm_packages/npm/angular2@2.0.0-beta.7/*"
     ],
     "rxjs/*": [
        "jspm_packages/npm/rxjs@5.0.0-beta.2/*"
     ]
  }

See https://github.com/frankwallis/plugin-typescript/tree/master/examples/angular2 for a full example. 有关完整示例,请参见https://github.com/frankwallis/plugin-typescript/tree/master/examples/angular2

Note that "baseUrl" and "paths" are not official properties and only on the beta version of typescript compiler. 请注意,“ baseUrl”和“ paths”不是官方属性,仅在打字稿编译器的beta版本上。

The issue is currently being tracked here: https://github.com/Microsoft/TypeScript/issues/6012 该问题当前正在此处跟踪: https : //github.com/Microsoft/TypeScript/issues/6012

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

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