导入 ng2-bootstrap 更改我的 tsc 输出目录结构

Importing ng2-bootstrap changes my tsc output directory structure

我有以下文件夹结构

dist
src
  module1
    m1.ts
  module2
    m2.ts
.tsconfig.json
...

我将 tsc 配置为输出到 dist/js 所以在 运行 宁 npm run tsc 之后我有以下内容:

dist
    js
      module1
          m1.js
      module2
          m2.js
src
...

经过一番努力,我设法将 ng-bootstrap 集成到我的项目中。

如果我 import 来自 ng2-bootstrap 我应用中的某处,例如:

import { ACCORDION_DIRECTIVES } from 'ng2-bootstrap/components/accordion';

和运行 npm run tsc我的文件夹结构更改为:

dist
    js
      node_modules
          ng2-bootstrap
             components
                accordian
                ...
      src
          module1
             m1.js
          module2
             m2.js
src
....

为什么会这样?

我包括:<script src="/vendor/ng2-bootstrap/bundles/ng2-bootstrap.js"></script> 在我的 index.html

我可以用 angular2 做到这一点 <script src="/vendor/angular2/bundles/angular2.dev.js"></script>import {Component} from 'angular2/core' 并且这不会创建 dist/js/node_modules/angular2 文件夹

更新

这是我的tsconfig.json

的内容
{
  "compilerOptions": {
    "target": "ES5",
    "outDir": "./dist/js",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "dist",
    "typings/main",
    "typings/main.d.ts"
  ]
}

我想出了解决办法。显然,源 .ts 文件与 .d.ts 在同一目录中。因此它编译这些文件。 (我一路上在某个地方读到这个,我不确定原因/为什么会发生这种情况)但是,解决方案是删除 .ts 忽略 *.d.ts 中的所有文件 node_modules/ng2-bootstrap

我创建了一个 node script 来执行此操作:

// ./scripts/hack-remove-files.js

var fs = require('fs-extra')
var glob = require("glob");

var options = { ignore: '**/*.d.ts'};

glob("node_modules/ng2-bootstrap/**/*.ts", options, function (er, files) {
    for(i in files){
        removeFile(files[i]);
    }
});

glob("node_modules/ng2-file-upload/**/*.ts", options, function (er, files) {
    for(i in files){
        removeFile(files[i]);
    }
});

removeFile = function(file){
    fs.remove(file, function (err) {
        if (err) return console.error(err)
    })
}

我运行这是npm中的postinstall脚本:

"scripts": {
   ...
   "postinstall": "typings install && node ./scripts/hack-remove-files.js",
}

这只需要在下载包后完成 1 次。

另一个命令行方法是:

find node_modules/ng2-bootstrap -name '*.ts' | grep -v '\.d\.ts$' | xargs rm

编辑

我找到了提到这一切的原始问题:https://github.com/valor-software/ng2-bootstrap/issues/128