I want to use css-loader with :local()
function in my dev-setup.
No error: import './sidebar.css';
it compiles without problem, but then I dont know how to access the local classnames inside my .tsx
file.
Error: import classNames from './sidebar.css';
Here I get: error TS2307: Cannot find module './sidebar.css'
Explaining my setup:
.tsx
files are compiled to commonjs modules via gulp-typescript (ES5)sidebar.tsx (gets imported in app.tsx
if that matters)
import classNames from './sidebar.css';
sidebar.css
.sl-sidebar {
background-color: yellow;
}
gulpfile.js
Gulp task compiling .tsx
files to commonJS modules (runs before webpack-task of course):
gulp.task('gui-tsx', function () {
return gulp.src(config.guiTsxPath + '**/*.tsx')
.pipe(ts({
jsx: 'react',
outDir: config.guiCompiledPath,
module: 'commonjs',
target: 'ES5'
}))
.pipe(gulp.dest(config.guiCompiledPath));
});
My gulp-webpack task:
gulp.task('gui-webpack', function () {
webpack({
bail: false,
debug: true,
entry: './' + config.guiCompiledPath + 'app.js',
output: {
filename: "gui.js",
path: './' + config.pubPath + 'js'
},
devtool: "#inline-source-map",
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false,
semicolons: true
},
sourceMap: true
})
],
module: {
loaders: [ {
test: /\.css$/,
loader: 'style-loader!css-loader?modules'
}]
}
}, function (err, stats) {
if (stats.compilation.errors.length > 0) {
console.log(stats.compilation.errors[0].message);
}
});
});
What am I doing wrong?
Edit: I just found this: https://github.com/Microsoft/TypeScript/issues/2709 but I dont quite understand it. Does it mean I have to declare my CSS as a module?
module. scss is SCSS file with CSS modules. According to the repo, CSS modules are: CSS files in which all class names and animation names are scoped locally by default.
typescript cannot load or understand css files, but it will work with webpack. To tell TypeScript that there are files with an ending other than *.ts you have to declare a wildcard module for the according file type. Just put it in a *.d.ts file which you include in the project.
// declaration.d.ts
declare module '*.css' {
const content: any;
export default content;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With