因此,由于这似乎不是一个受欢迎的问题,我设法找到了解决方案。希望这能帮助任何有困难的人ts-loader
+ css-loader
.
1) 添加处理 .css 扩展名的 .d.ts 文件
// I put it in root, but could be anywhere
// <root>/defs.d.ts
declare module "*.css" {
var styles: { [key: string]: string };
export = styles
}
2)由于我使用Webpack 3.x,更改style
to style-loader
in webpack.config.js
module: {
rules: [
//...
{
test: /\.css$/,
loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
}
]
}
3)导入样式为*
在组件文件中
// In Main.tsx
import * as styles from "./Main.css";
// Usage
<div className={styles.nameOfClass} />
4) In tsconfig.json
add .d.ts
文件到include
部分。就我而言,它...
"include": [
"src",
"./defs.d.ts"
],
重新启动 webpack-dev-server 或其他任何东西,应该就可以了(希望如此)。
快乐编码!