【问题标题】:Error while transpiling typescript angular app using gulp使用 gulp 转换打字稿角度应用程序时出错
【发布时间】:2016-03-05 15:10:06
【问题描述】:

我在使用 angular/gulp 在应用程序上将 typescript 转换为 js 时出错。

src/app/app.component.ts(1,25): error TS2307: Cannot find module 'angular2/core'.
src/app/main.ts(5,28): error TS2307: Cannot find module 'angular2/platform/browser'.

但一切正常:编译正常,包含和 systemjs 正常,因此应用程序启动没有问题。但我想避免出现这个错误!

这是我使用的 gulpfile:

const TS_CONFIG = {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
};
gulp.task('build.js.dev', ['clean.dev'], function () {
    gulp.src(['./' + APP_DIR + '/**/*.ts'])
        .pipe(ts(TS_CONFIG))
        .pipe(gulp.dest('./' + APP_DEST));
});

这个错误可能与打字有关,但我不知道如何在这里设置它......

感谢您的帮助。

注意:我使用的是 jspm_packages,但与使用 npm 相比没有太多变化。

【问题讨论】:

    标签: typescript gulp angular gulp-typescript


    【解决方案1】:

    消除这些错误的最快和最简单的方法是提供定义作为打字稿编译的来源。

    让你的任务适应这样的事情:

    gulp.task('build.js.dev', ['clean.dev'], function () {
        gulp.src([
                 'node_modules/angular2/core.d.ts',
                 'node_modules/angular2/typings/browser.d.ts',
                 './' + APP_DIR + '/**/*.ts'
             ])
            .pipe(ts(TS_CONFIG))
            .pipe(gulp.dest('./' + APP_DEST));
    });
    

    【讨论】:

    • 这更糟糕! gist.github.com/Nek-/ecd2d8080e649e9c7b74 我也尝试过类似 'web/jspm_packages/**/*.d.ts' 但结果相同。
    • 我不确定这种方法是否适用于 jspm 依赖项 - 我一直只使用 npm - 你在 typescript 编译器中使用 systemjs 作为模块系统吗?
    • 不,实际上我更喜欢使用 gulp 进行编译,它的观察者有很多原因(这也在 angularjs 文档中的某处进行了描述)。 jspm 下载 exact 与 npm 相同的包(文件夹是 jspm_packages/npm/angular2/...!)。所以它应该工作,但没有;不知道为什么。
    • 是的,无论您使用哪个任务运行器,您的 ts onfig 都必须是正确的。我会检查什么适合 jspm。
    • 我在 npm 依赖项中添加了 angular,仍然不使用它......但它有效!我不知道怎么做,而且这是一个丑陋的修复......所以如果有人有解决这个问题的方法,我很乐意。
    【解决方案2】:

    我建议不要手动提供所有文件,而是实际使用已经包含所有必需数据的 tsconfig.json:

    gulp.task('build.js.dev', function() 
    {
        var tsProject = ts.createProject('tsconfig.json');
    
        var tsResult = tsProject.src()
            .pipe(sourcemaps.init())   
            .pipe(ts(tsProject));  
    
        return tsResult.js.pipe(gulp.dest('./' + APP_DEST));
    });
    

    tsconfig.json 示例:

    { 
        "compilerOptions": { 
            "target": "es5",
            "module": "system",
            "moduleResolution": "node",
            "sourceMap": true,
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true,
            "removeComments": false,
            "noImplicitAny": false
        },
        "exclude": [
            "node_modules",
            "dist",
            "typings/browser.d.ts",
            "typings/browser"
        ]
    } 
    

    【讨论】:

    猜你喜欢
    • 2017-02-08
    • 2020-06-04
    • 2016-05-02
    • 2019-11-12
    • 2018-08-12
    • 2021-12-07
    • 1970-01-01
    • 2017-09-17
    • 2013-08-16
    相关资源
    最近更新 更多