简体   繁体   English

Webstorm 11 无需编译即可检查 typescript 个文件。 使用webpack进行编译

[英]Webstorm 11 check typescript files without compiling. Using webpack for compilation

i'm trying to use webstorm with webpack and typescript and i have stuck with errors checking.我正在尝试将 webstorm 与 webpack 和 typescript 一起使用,但我一直坚持错误检查。 I want to compile ts files with webpack, so i need to avoid compiling source files via Webstorm, but it seems like Webstorm perform error checking only during compilation process.我想用 webpack 编译 ts 文件,所以我需要避免通过 Webstorm 编译源文件,但似乎 Webstorm 只在编译过程中执行错误检查。

Corresponding to webstorm docs "Resolve objects using tsconfig.json" should activate Errors checking without compilation, but is doesnt.对应于 webstorm 文档“Resolve objects using tsconfig.json”应该在没有编译的情况下激活错误检查,但事实并非如此。

example code, which Webstorm doesnt highlight示例代码,Webstorm 没有突出显示

 { let z = 4;}
 console.log(z);

my tsconfig file:我的 tsconfig 文件:

 {
  "compilerOptions": {
    "module": "commonjs",
    "out": "build/tsc.js",
    "target": "es5",
    "sourceMap": true,
    "jsx": "react"
  },
  "exclude": [
    "node_modules"
  ]
}

In same time Visual studio code display errors fine.同时 Visual Studio 代码显示错误正常。 Do i have any errors in my configs?我的配置有任何错误吗? Is it possible to highlight errors correct with Webstorm or other JetBrains IDE?是否可以突出显示使用 Webstorm 或其他 JetBrains IDE 纠正的错误?

Typescript version 1.7, Webstorm 11. Typescript 版本 1.7,Webstorm 11。

Original Answer (outdated - see UPDATE below): 原始答案(过时 - 请参阅下面的更新):

As Valery correctly pointed out you can set the "noEmit" property to true to prevent the compiler creating any output files. 正如Valery正确指出的那样,您可以将“noEmit”属性设置为true以防止编译器创建任何输出文件。

However, if your Webpack setup uses the same tsconfig.json file it will also prevent webpack from creating the output files. 但是,如果您的Webpack安装程序使用相同的tsconfig.json文件,它还将阻止webpack创建输出文件。 You'll only notice this the next time webpack is restarted. 您只会在下次重新启动webpack时注意到这一点。

In my webpack setup I'm using the "ts-loader" Typescript loader. 在我的webpack设置中,我正在使用“ts-loader”Typescript加载器。 As mentioned on the ts-loader github page you can override compiler options. ts-loader github页面所述,您可以覆盖编译器选项。

So this is my setup: 所以这是我的设置:

tsconfig.json (used by IDE and Webpack) tsconfig.json(由IDE和Webpack使用)

"compilerOptions": {
    "noEmit": true, // do not emit js and map files
    ...

webpack.config.json (used by Webpack) webpack.config.json(由Webpack使用)

{
    test: /\.ts$/,
    loader: 'ts-loader',
    query: {
      'compilerOptions': {
        "noEmit": false // make sure js files do get emitted
      }
    },
    ...
}

Et voila: IDE compiler checks without js and js.map files polluting your source code folder! Et voila:IDE编译器检查没有js和js.map文件污染您的源代码文件夹!

UPDATE: My answer was a valid workaround in January but today the better solution is to use the Typescript service inside Webstorm/IDEA as suggested by anstarovoyt. 更新: 我的答案是1月份的有效解决方法,但今天更好的解决方案是使用anstarovoyt建议的Webstorm / IDEA内的Typescript服务。

Also don't forget to UNcheck "Enable TypeScript Compiler" as shown here: 另外不要忘记UNcheck“启用TypeScript编译器”,如下所示:

在此输入图像描述

WebStorm 2016.1 (and other IJ 2016.1 IDEs) supports "compileOnSave" option. WebStorm 2016.1(和其他IJ 2016.1 IDE)支持"compileOnSave"选项。 Example: 例:

{
  "compileOnSave": false,
  "compilerOptions": {
    "module": "commonjs"
  }
}


Update: WebStorm 2016.2 has the new 'TypeScript service' integration. 更新: WebStorm 2016.2具有新的“TypeScript服务”集成。 You don't need the 'TypeScript complier' integration at all. 您根本不需要“TypeScript编译器”集成。 Just check the option 'Use TypeScript service'. 只需选中“使用TypeScript服务”选项即可。 Moreover the new integration works much more faster. 此外,新集成的工作速度更快。

Update 2: The integration is enabled by default in WebStorm 2016.3 更新2: WebStorm 2016.3中默认启用集成

选项'使用TypeScript服务'

I found the way to prevent compiler output vis tsconfig - noEmit option. 我发现了阻止编译器输出的方法,而不是tsconfig - noEmit选项。

{
  "compilerOptions": {
    "module": "commonjs",
    "noEmit": true,
    "target": "es5",
    "sourceMap": true,
    "jsx": "react"
  },
  "exclude": [
    "node_modules"
  ]
}

With this config i have no extra file and correct error highlight in webstorm. 使用此配置,我没有额外的文件和webstorm中正确的错误突出显示。

In your Webpack configuration file add this in module.rules :在您的 Webpack 配置文件中,将此添加到module.rules中:

{
    test: /\.tsx?$/,
    use: [
        {
            loader: 'ts-loader',
            options: {
                compilerOptions: {
                    noEmit: false,
                },
            },
        },
    ],
}

Obviously, if you already have a rule for test: /\.tsx?$/ , you should combine it with the above.显然,如果您已经有一个test: /\.tsx?$/ ,您应该将它与上面的结合起来。

And note that module.rules is an array of objects, not an object.请注意, module.rules是一个对象数组,而不是 object。

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

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