typescript 如何使用 ts-loader 忽略 webpack 中的测试文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41238711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:06:05  来源:igfitidea点击:

How to ignore test files in webpack with ts-loader

typescriptwebpack

提问by gaspard

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:

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

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

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

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

Here is the webpack config:

这是 webpack 配置:

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'
      }
    ]
  }
}

[EDIT]

[编辑]

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).

我使用 test 函数来记录看到的文件并且测试文件没有出现在那里,所以问题不在于 webpack 包含错误的文件,而在于 typescript 行为不端(因为测试在 jest 下工作正常)。

回答by Jonathan

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

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

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

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

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

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.

但看起来 TypeScript(至少从 2.3.2 开始)关注这些设置并忽略webconfig.config.js.

回答by Acidic

You can specify a function instead of the regular expression for the loader testproperty; this can give you a more control.

您可以为 loadertest属性指定一个函数而不是正则表达式;这可以给你更多的控制。

test: function (modulePath) {
  return modulePath.endsWith('.ts') && !modulePath.endsWith('test.ts');
}

回答by gaspard

Found it. My version of 'ts-loader' was behind and somehow did not manage to properly handle installed @types.

找到了。我的“ts-loader”版本落后了,不知何故无法正确处理已安装的@types。

Rule of thumb: pack, build, transpile issues: upgrade dev dependencies.

经验法则:打包、构建、转换问题:升级开发依赖项