I went through a lot of related questions/answers and external articles before finally finding a step-by-step walkthrough. While the answer by [alechill] eventually got me to my solution, I thought I would drop in my detailed solution for anyone that finds this post...
npm install sass-loader css-loader style-loader --save // this may differ
edit webpack.config.js to add a rule and an extension to resolve scss
module.exports = {
/* stuff */
module: {
rules: [
/* other rules, */
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
}
]
},
resolve: {
extensions: [/* file extensions, */ '.scss']
},
},
/* more stuff */
};
create/edit declaration.d.ts
declare module '*.scss' {
const content: { [className: string]: string };
export = content; // your IDE may warn of "no usages" here, mine did
}
Make it happen...
SomeComponent.tsx
import React from 'react';
import './SomeComponent.scss';
interface Props { /* ... */ }
interface State { /* ... */ }
class SomeComponent extends React.Component<Props, State> {
/* do awesome stuff */
}
SomeComponent.scss
body {
h1,h2,h3,h4,h5 {
background-color: red; /* test that it works */
}
}
Build and enjoy!
File structure described above
src/SomeComponent.tsx
src/SomeComponent.scss
declaration.d.ts
webpack.config.js
I'm still learning react typescript, so refactoring .scss files into the src/styles directory as an alias is, at this point, an exercise left to you