I want to use the .scss
in my project(bulid with react and typescript),so I use typings-for-css-modules-loader
with css-loader
and sass-loader
.
When I npm run dev
, I get this error:
./src/Common.scss
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/sass-loader/lib/loader.js):
.common {
^
Invalid CSS after "e": expected 1 selector or at-rule, was "exports = module.ex"
my.scss
like this:
.common {
height: 100px
}
and my wepack.config is:
module.exports = {
mode: 'development',
entry: [
'webpack-dev-server/client',
path.resolve(root, 'src/index.tsx')
],
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
publicPath: '/',
port: 8080,
historyApiFallback: true
},
resolve: {
extensions: [".ts", ".tsx", ".scss", ".js", ".json", "css"]
},
plugins: [
new CleanWebpackPlugin(['dist']),
new BundleAnalyzerPlugin(),
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
})
],
output: {
filename: 'bundle.js',
path: path.resolve(root, 'dist'),
publicPath: '/'
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader'
},
{
test: /\.(js|jsx)$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(sa|sc|c)ss$/,
use: [{
loader: devMode ? MiniCssExtractPlugin.loader : 'style-loader'
}, {
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
}
}, {
loader: 'sass-loader', options: {
sourceMap: true
}
}]
},
{ test: /\.(c|sc)ss$/, loader: 'typings-for-css-modules-loader?modules&sass' },
{
test: /\.(jpg|png|gif)$/,
use: [{
loader: 'file-loader',
options: {
name (file) {
if (devMode === 'development') {
return '[path][name].[ext]'
}
return '[hash].[ext]'
}
}
}]
}
]
},
optimization: {
splitChunks: {
chunks: 'all'
}
}
};
When I use css-loader
with sass-loader
or single use typings-for-css-modules-loader
, this error is fix.
But I get another error like this:
ERROR in /Users/hly/Bpeanut.github.io/src/app/HeaderBar.tsx
./src/app/HeaderBar.tsx
[tsl] ERROR in /Users/hly/Bpeanut.github.io/src/app/HeaderBar.tsx(40,39)
TS2339: Property 'header' does not exist on type 'typeof import("/Users/hly/Bpeanut.github.io/src/app/HeaderBar.scss")'.
HeaderBar.tsx
like this(the error is here):
import * as styles from './HeaderBar.scss';
class default HeaderBar extends Component {
render() {
return(<div className={styles.header}>123</div>)
^
}
}
and HeaderBar.scss
like:
.header {
height: 100px;
}
HeaderBar.scss.d.ts
:
export interface IHeaderBarScss {
'header': string;
}
export const locals: IHeaderBarScss;
Now I find another way to fix it.
Use const styles = require('./HeaderBar.scss');
instead of import * as styles from './HeaderBar.scss';
and it work.
thanks.
css-loader
withtypings-for-css-modules-loader
. Currently you use both which probably causes the error. – Endocarditis