问题
I'm trying to create a React project with typescript using webpack. But I can't manage to get CSS modules working. I keep getting this error when trying to import the CSS files in typescript:
ERROR in src/components/Button.module.css:1:0
[unknown]: Parsing error: Declaration or statement expected.
> 1 | .myButton {
2 | box-shadow: 0px 0px 0px -1px #1c1b18;
3 | background:linear-gradient(to bottom, #eae0c2 5%, #ccc2a6 100%);
4 | background-color:#eae0c2;
ℹ 「wdm」: Failed to compile.
To me this looks like webpack is expecting the css file to contain valid typescript but I don't know for sure. I tried looking around and while a lot of people seem to be struggeling with using typescript and CSS modules together I couldn't find anyone with a similar issue.
I imported the CSS file like this: Button.tsx
import React from "react";
import "./Button.module.css";
export class Button extends React.Component{
...
}
And this is the CSS file that I'm trying to import. Button.module.css:
.myButton {
box-shadow: 0px 0px 0px -1px #1c1b18;
background:linear-gradient(to bottom, #eae0c2 5%, #ccc2a6 100%);
background-color:#eae0c2;
border-radius:22px;
border:4px solid #706752;
display:inline-block;
cursor:pointer;
color:#505739;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:11px 22px;
text-decoration:none;
text-shadow:0px 1px 0px #ffffff;
}
.myButton:hover {
background:linear-gradient(to bottom, #ccc2a6 5%, #eae0c2 100%);
background-color:#ccc2a6;
}
.myButton:active {
position:relative;
top:1px;
}
The generated Button.module.css.d.ts
// This file is automatically generated.
// Please do not change this file!
interface CssExports {
}
export const cssExports: CssExports;
export default cssExports;
I also have a type declaration for css modules called styles.d.ts
declare module "*.module.css" {
const classes: { [key: string]: string };
export default classes;
}
While Webpack does seem to generate some typings for the css module it looks weirdly empty to me. I think I doing something wrong with the css-modules-typescript-loader, I tried with a bunch of the avaliable plugins but keep running into the same error reguardless.
Here is what I configured in my webpack.config.ts:
import webpack from "webpack";
const config: webpack.Configuration = {
entry: "./src/index.tsx",
resolve: {
extensions: [".tsx", ".ts", ".js", ".css"],
},
module: {
rules: [
{
test: /\.module.css$/,
use: [
"css-modules-typescript-loader",
{
loader: 'css-loader',
options: {
modules: true,
sourceMap: true,
}
},
]
},
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript",
],
},
},
},
],
},
tsconfig.json
{
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"target": "ES5",
"plugins": [
{
"name": "typescript-plugin-css-modules"
}
]
},
"include": [
"src"
],
}
回答1:
I had same issue. In my case, it was not the webpack's problem. It was the problem of typescript-eslint. I solved the problem by adding "*.css" in .eslintignore file.
来源:https://stackoverflow.com/questions/64126764/how-do-i-fix-typescript-compiler-errors-on-css-files