Skip to content

Plugin fails when using an webpack aliases and TS paths to @import other scss files inside scss modules #78

Closed
@mfpopa

Description

@mfpopa

Describe the bug
The plugin does not work when the seem to work with webpack aliases and TS paths.

Relative paths work fine, namely if I convert from alias/my-styles.scss to ../../styles/my-styles.scss the plugin works fine.

I have looked at the #26 #54 which looked like they were supposed to solve my problem, but they don't, unless I am doing something wrong.

Below is my config. Note that ./client is the folder where I have all my client-side source code for this particular project.

// tsconfig.json
...
"baseUrl": ".",
"paths": {
  "@client/*": ["client/*"]
}
...
// webpack.config.js
...
alias: {
  '@client': path.resolve(__dirname, 'client'),
},
...
// FormComponent.tsx
import { Button } from '@client/components/Button';
import styles from './FormComponent.module.scss';
...
<div className={styles.form} {...otherProps}>...</div>
...
// FormComponent.module.scss
@import '@client/styles/variables.scss';
...
.form {
  font-size: $font-size;
}
...
// variables.scss
$font-size: 14px;
...

Because I used the @client alias to import inside the SCSS file, now the className={styles.form} show a squiggly line under the word form and I get this error: Property 'form' does not exist on type '{}'.ts(2339). If I use the relative import instead (@import '../../styles/variables.scss';) the plugin works fine.

Webpack bundles everything properly and no complaints from VS Code and TypeScript for using the @client alias when importing in either TS/TSX files or SCSS files. The only thing that does not work with the alias is this plugin, typescript-plugin-css-modules.


To Reproduce
I can set up a test repo if the description of the problem does not provide enough info.


Expected behavior
I expect the plugin to work with imports that use aliases just like it works when using imports with relative paths.


Desktop (please complete the following information):


Additional context

Looking at the TS server log in VS Code, the plugin fails with:

Info 84   [17:47:0.941] [typescript-plugin-css-modules] Failed Error: Can't find stylesheet to import.
  ╷
1 │ @import '@client/styles/variables.scss';
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  stdin 1:9  root stylesheet
Info 85   [17:47:0.954] [typescript-plugin-css-modules] Stack trace: Error: Can't find stylesheet to import.
  ╷
1 │ @import '@client/styles/variables.scss';
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  stdin 1:9  root stylesheet
    at Object._newRenderError (...\node_modules\sass\sass.dart.js:13621:19)
    at Object._wrapException (...\node_modules\sass\sass.dart.js:13467:16)
    at StaticClosure._renderSync (...\node_modules\sass\sass.dart.js:13442:18)
    at Object.Primitives_applyFunction (...\node_modules\sass\sass.dart.js:1064:30)
    at Object.Function_apply (...\node_modules\sass\sass.dart.js:4898:16)
    at _callDartFunctionFast (...\node_modules\sass\sass.dart.js:6580:16)
    at Object.renderSync (...\node_modules\sass\sass.dart.js:6558:18)
    at Object.exports.getClasses (...\node_modules\typescript-plugin-css-modules\lib\helpers\getClasses.js:75:18)
    at ...

Any help would be appreciated. Meanwhile, I will try to troubleshoot the plugin as much as I can. Is there a way to execute node --inspect on the plugin to debug it?

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions