如何使用 ts-loader 忽略 webpack 中的测试文件 [英] How to ignore test files in webpack with ts-loader

查看:70
本文介绍了如何使用 ts-loader 忽略 webpack 中的测试文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它运行带有 jest 和 ts-jest 的名为*.test.ts"的测试文件.这很好,但是当我启动 webpack 时,我收到测试文件错误:

I have a project that runs the test files named "*.test.ts" with jest and ts-jest. This is fine but when I launch webpack, I get errors for the test files:

ERROR in /some/path/note.test.ts
(27,3): error TS2304: Cannot find name '

如何修复 webpack 配置以完全忽略测试文件?

How can I fix the webpack config to totally ignore test files ?

这里是 webpack 配置:

Here is the webpack config:

const path = require ( 'path' )

module.exports =
{ entry: './src/boot.tsx'
, node:
  { __dirname: false
  }
, output:
  { path: path.resolve ( __dirname, 'app', 'build' )
  , filename: 'app.js'
  , publicPath: '/live-reload/'
  }
, devtool: 'source-map'
, resolve:
  { extensions: ['', '.js', '.ts', '.tsx']
  }
, module:
  { loaders:
    [ { test: /\.tsx?$/
      , exclude: /node_modules/
      , loader: 'ts-loader'
      }
    ]
  }
}

我使用测试功能来记录看到的文件,但测试文件没有出现在那里,所以问题不在于包含错误文件的 webpack,而在于打字稿行为不当(因为测试在开玩笑时工作正常).

I used the test function to log seen files and the test files do not appear in there, so the problem is not on webpack including the wrong files but on typescript misbehaving (because tests work fine with jest).

推荐答案

我最终也解决了这个问题,但采用了不同的方式,即将以下设置添加到我的 .tsconfig 文件中:

I ended up resolving this too, but in a different way, namely, adding the following settings to my .tsconfig file:

"include": [
    "./lib/**/*"
],
"exclude": [
    "./lib/__tests__"
]

不知道为什么 OP 和公司没有想到这一点.也许他们的 .tslint 文件中已经有了这些设置(例如,因为他们的项目是从另一个项目生成或分叉的).

Not sure why this didn't come up for the OP and company. Perhaps they already had those settings in their .tslint file (e.g. because their project had been generated or forked from another project).

但看起来 TypeScript(至少从 2.3.2 开始)会注意这些设置并忽略 webconfig.config.js 中的等效设置.

But it looks like TypeScript (at least, as of 2.3.2) pays attention to these settings and ignores equivalent settings in webconfig.config.js.

这篇关于如何使用 ts-loader 忽略 webpack 中的测试文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆