2016-12-01 37 views
1

我试图去抓住用打字稿2的WebPack和角1.5,但建设时的WebPack不断抱怨它:说服的WebPack使用node_modules/@类型

无法解析模块“@类型/角'in C:\ play \ gt \ src

我很难过。一直在谷歌搜索和搞乱,但我无处可去。

我app.ts样子:

import * as angular from "@types/angular"; 
let app = angular.module ("GT.module", []); 

我tsconfig样子:

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "target": "es5", 
    "sourceMap": true, 
    "typeRoots" : [ 
     "node_modules/@types" 
     ] 
    }, 
    "exclude": [ 
    "node_modules", 
    "**/*.spec.ts" 
    ], 
    "include" : [ 
    "src/**/*" 
    ] 
} 

...,最后的WebPack配置:

module.exports = { 
     entry: './src/app.ts', 
     output: { 
      filename: './dist/app.js' 
     }, 
     devtool: "source-map", 
     resolve: { 
      extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'], 
      modulesDirectories: ["node_modules"] 
     }, 
     module: { 
      loaders: [ 
       { test: /\.ts$/, loader: 'ts-loader' } 
      ] 
     } 
    } 

回答

1

@types模块是不是你可以导入,它们是供编译器参考的typedef。

@types/angular包实际上并没有实现.module并且您的代码将会中断(如果它曾经编译过)。

你可能想要做更多的东西一样:

/// <reference path="../node_modules/@types/angular/index.d.ts" /> 
import * as angular from "angular"; 
let app = angular.module ("GT.module", []); 

,你通常会甚至不需要reference,因为编译器会自动包括@types/*包的typedef,除非你告诉它不要(通过在您的tsconfig.json中设置typestypeRoot)。此行为已添加到v2中,并且相当新近。

2

你的目录树是什么样的? 它应该是这样的:

node_modules/ 
    @types/ 
     angular/ 
src/ 
tsconfig.json 
webpack.config.js 

那么你不需要"typeRoots" : ["node_modules/@types"]

import * as angular from "@types/angular";应该成为import * as angular from "angular";

打字稿编译器2.0将知道去哪里找找到在正确的分型地方(即:在node_modules/@types

+0

谢谢!这已经解决了它。 Typescript目标帖子似乎在不停地移动,很难找到最新的文章。 – jeffeld

+0

是啊,我知道,JavaScript生态系统发展得非常快,尝试官方文档,它保持相当不错。例如,这个链接将使用typescript包装webpack:https://www.typescriptlang.org/docs/handbook/react-&-webpack.html –

相关问题