【问题标题】:Typescript > Can I have the tsconfig.json located somewhere else than project root?Typescript > 我可以将 tsconfig.json 放在项目根目录以外的其他地方吗?
【发布时间】:2017-07-22 00:33:11
【问题描述】:

Typescript 文档指出 tsconfig.json 文件应位于项目根目录:

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

由于我的项目要求,我必须有这个文件的多个版本(Angular AOT、Universal、Webpack 都需要不同的设置)。

这会使项目根目录混乱,因此理想情况下我想将它们移动到其他地方,例如 /config/...

但是,这样做会使 ts 编译器感到困惑,并且文件路径无法正确解析。例如,下面的排除设置仍将包含在编译中

  "exclude": [
    "node_modules",
    "compiled",
    "e2e/**/*.ts",
    "src/main.browser.universal.aot.ts",
    "src/app/app.module.universal.node.ts"
  ],

将 ../ 放在它们前面并不能解决问题。任何人都有任何想法或示例来演示如何将 tsconfig.json 放置在其他地方?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    对于这类事情,我建议使用任何可用的任务运行器,最流行的是:grunt 和 gulp

    有很多关于如何设置 typescript 编译的文章(example)。

    这是我如何从 gulp 使用 tsconfig 的示例:

    gulp.task('build.js.dev', () => 
    {
        var tsProject = ts.createProject('tsconfig.json', {
            typescript: require('typescript')
        });
    
        var tsResult = tsProject.src()
            .pipe(sourcemaps.init())   
            .pipe(tsProject());  
    
        return merge([
            //Write definitions 
            tsResult.dts.pipe(gulp.dest(TEMP_TARGET_FOLDER)),
            //Write compiled js
            tsResult.js.pipe(sourcemaps.write(
                ".", 
                { 
                    includeContent: false, 
                    sourceRoot: "../dist" 
                    // mapSources: function(sourcePath) 
                    // {
                    //     console.log(sourcePath);
                    //     return sourcePath;//.replace('../', '');
                    // } 
                })).pipe(gulp.dest(TEMP_TARGET_FOLDER))]);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 2013-03-09
      • 1970-01-01
      • 2014-12-05
      • 2010-09-23
      相关资源
      最近更新 更多