简体   繁体   English

TypeScript找不到样式模块

[英]TypeScript not finding style modules

I have the following basic configuration in my webpack which is relevant to loading my styles via TypeScript: 我的webpack中具有以下基本配置,与通过TypeScript加载样式有关:

{
  module: {
    rules: [{
      test: /\.s?css$/,
      use: [
        { loader: 'style-loader' },
        {
          loader: 'typings-for-css-modules-loader',
          options: {
            sass: true,
            modules: true,
            camelCase: true,
            importLoaders: 1,
            namedExport: true,
            localIdentName: '[name]__[local]__[hash:base64:5]',
          }
        },
        { loader: 'sass-loader' },
      ]
    }],
  },
  resolve: {
    alias: {
      app: srcPath,
    },
    modules: ['node_modules', srcPath],
    extensions: ['.js', '.jsx', '.json', '.scss', '.css', '.tsx', '.ts', '.d.ts'],
  },
}

In my tsconfig.json , I have the following to match: 在我的tsconfig.json ,我要匹配以下内容:

{
  "compilerOptions": {
    "paths": {
      "app": ["./app"]
    }
  }
}

However, I am consistently getting an error when trying to load a file from app/styles/root.scss , with the following project structure: 但是,在尝试使用以下项目结构从app/styles/root.scss加载文件时,始终出现错误:

├── components
│   └── Root.tsx
├── styles
│   └── root.scss
├── index.hbs
├── index.scss
├── index.scss.d.ts
└── index.tsx

在此处输入图片说明

It seems like TypeScript is not able to resolve this path correctly. 看来TypeScript无法正确解析此路径。 If this were a plain JS based file, it would work fine. 如果这是基于JS的普通文件,则可以正常工作。

Strangely, I can add a root.scss.d.ts with the defintions for the CSS file and it will resolve correctly. 奇怪的是,我可以添加带有CSS文件定义的root.scss.d.ts ,它将正确解析。 I think this is a bit strange because I am specifically using typings-for-css-modules-loader for this task. 我认为这有点奇怪,因为我专门为此任务使用typings-for-css-modules-loader

How do I get TypeScript to resolve these scss files under the styles directory? 如何获得TypeScript解析样式目录下的这些scss文件?

Fix 固定

Simply 只是

declare module "*.scss";

More 更多

See the JavaScript to TypeScript migration guide : https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html 请参阅JavaScript到TypeScript的迁移指南: https : //basarat.gitbooks.io/typescript/content/docs/types/migrating.html

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

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