tjn*_*k24 1 typescript reactjs webpack tslint css-modules
我正在使用css-modules
和构建一个 Gatsby 项目gatsby-config.js
,我在其中解决了一个.scss
扩展。
它构建并且工作得很好,但是我的 linter 不同意这一点,当我尝试导入这样的样式时,会引发错误“无法找到模块'./style'或其相应的类型声明.ts(2307) ”:
如果我从 js/ts/jsx/tsx 文件导入,则不会出现错误,但是当我尝试导入模块之类的样式(或图像)时,如果没有正确的扩展名,则会引发此错误。
我已经尝试在项目根目录的“typings”文件夹内将扩展声明为模块,例如“声明模块“*.scss””,但不起作用。
我的gatsby-config.js
const path = require('path');
const isDev = process.env.NODE_ENV === 'development';
module.exports = {
siteMetadata: {
title: 'Title from siteMetadata',
},
plugins: [
{
resolve: `gatsby-plugin-sass`,
options: {
cssLoaderOptions: {
sourceMap: isDev,
modules: {
mode: 'local',
localIdentName: '[local]--[hash:base64:5]',
},
},
},
},
{
resolve: 'gatsby-plugin-alias-imports',
options: {
alias: {
'@containers': path.resolve(__dirname, 'src/containers'),
'@components': path.resolve(__dirname, 'src/components'),
'@pages': path.resolve(__dirname, 'src/pages'),
},
extensions: ['.js', '.jsx', '.ts', '.tsx', '.css', '.scss'],
},
},
],
};
Run Code Online (Sandbox Code Playgroud)
我的tsconfig.json
{
"compilerOptions": {
"strict": true,
"outDir": "./build/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es6",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"baseUrl": ".",
"paths": {
"@containers/*": ["src/containers/*"],
"@components/*": ["src/components/*"],
"@pages/*": ["src/pages/*"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的.eslintrc.js
module.exports = {
env: {
browser: true,
es2020: true,
},
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended',
'airbnb',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 11,
sourceType: 'module',
},
plugins: ['react', '@typescript-eslint', 'prettier'],
rules: {
'prettier/prettier': ['error'],
indent: ['error', 2],
'no-unused-vars': 'warn',
'import/no-unresolved': 'off',
'import/extensions': 'off',
'react/prop-types': 'off',
'react/jsx-props-no-spreading': 'off',
'react/jsx-filename-extension': [
'error',
{
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
],
},
};
Run Code Online (Sandbox Code Playgroud)
我将非常感谢你的帮助。至少,也许有某种方法可以关闭这个 ts 规则?
小智 5
在根目录创建一个文件 next-env.d.ts 并将以下语句添加到该文件中
declare module '*.css'
Run Code Online (Sandbox Code Playgroud)
添加上述语句之前
添加语句后