简体   繁体   English

TypeScript + Babel + Jest 不使用 moduleNameMapper 来减少文件

[英]TypeScript + Babel + Jest not using moduleNameMapper for less files

I have my jest config set up to map any.less and.css imports to the identity-obj-proxy module, as prescribed in basically every documentation cite/tutorial in existence.我将我的笑话配置设置为 map any.less 和.css 导入到identity-obj-proxy模块,基本上在现有的每个文档引用/教程中都有规定。

module.exports = {
  // ...
  moduleNameMapper: {
    '\\.(css|less)$': 'identity-obj-proxy',
  },
};

However, I've found that when a.less file includes some forms of invalid css (eg single-line comments, calling mixins, etc), the imported object is empty.但是,我发现当a.less文件包含一些无效css的forms(例如单行注释,调用mixins等)时,导入的object是空的。 This lead me to the discovery that the mapping isn't even being triggered, and some other mechanism (that I haven't figured out, since I didn't set up this project) has been generating class names from the stylesheets rather than simply using the proxy.这使我发现映射甚至没有被触发,并且一些其他机制(我还没有弄清楚,因为我没有设置这个项目)一直在从样式表生成 class 名称,而不是简单地使用代理。

import styles from './my-style-sheet.less'; // styles = {}
import * as styles from './my-style-sheet.less'); // styles = { default: {} }
import styles from './not-a-real-file.less'); // styles = {}
const styles = require('./my-style-sheet.less'); // styles = {}

What's really strange to me is that when I'm debugging and I evaluate the expression require('./my-style-sheet.less') it does import the proxy module.对我来说真正奇怪的是,当我在调试并评估表达式require('./my-style-sheet.less')时,它确实导入了代理模块。 In fact, I can require anything ending in .less and it will import the proxy, regardless of whether the file exists--which is what I would expect.事实上,我可以要求任何以.less结尾的东西,它会导入代理,不管文件是否存在——这是我所期望的。

Other patterns in moduleNameMapper are working correctly such as some path aliases. moduleNameMapper中的其他模式正常工作,例如一些路径别名。

module.exports = {
  // ...
  moduleNameMapper: {
    '^common': '<rootDir>/src/components/common',
  },
};

I have no idea what's going on nor how I can debug this.我不知道发生了什么,也不知道如何调试它。

It turns out it was an issue with our babel.config.js .事实证明这是我们的babel.config.js的问题。 We were adding the babel-plugin-css-modules-transform plugin, which apparently superseded the moduleNameMapper entry.我们正在添加babel-plugin-css-modules-transform插件,它显然取代了moduleNameMapper条目。

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

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