6
votes

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:

  1. .tsx files are compiled to commonjs modules via gulp-typescript (ES5)
  2. commonjs modules are compiled & minified to JS via webpack

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?

2
You can transpile typescript with help of webpack, you need not use gulp. Check the article for more details: jbrantly.com/typescript-and-webpackTSV
I know, but that doesnt solve the problem. same with plain webpackFelix Hagspiel

2 Answers

7
votes

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;
}
2
votes

To load non-ts resources just declare the require function and use the imported resources (as any).

Documentation : https://github.com/TypeStrong/ts-loader#code-splitting-and-loading-other-resources