11

I have been using this seed project to as the basis for my new Angular 2 project. Unfortunately, it does not come configured to use scss, so I have to add that configuration myself.

I am using Angular Material so am importing a default theme in my app component like this:

@Component( {
    selector: 'my-app',
    styles: [require('./app.component.css'), require('./material2-app-theme.scss')],
    templateUrl: './app.component.html'
} )

However, this yields the following error in the browser at runtime:

Uncaught Error: Expected 'styles' to be an array of strings.

I have tried various configurations in my webpack config, and am currently going with this:

{ 
    test: /\.css$/, 
    loaders: ['to-string-loader', 'css-loader'] 
},
{
    test: /\.scss$/,
    use: ['style-loader', 'css-loader', 'sass-loader'],
    include: [helpers.root( 'src', 'styles' )]
}

There are a handful of references to this error on the interweb but none of them solve my problem. Any ideas how to fix this?

5 Answers 5

24

You are only passing the css files, and not the scss files, to to-string-loader, so require('./material2-app-theme.scss') is not returning a string

Change the scss loader to...

{
    test: /\.scss$/,
    loaders: ['to-string-loader', 'css-loader', 'sass-loader'],
    include: [helpers.root( 'src', 'styles' )]
}

Also make sure that ./material2-app-theme.scss is in the include folder


FYI You could easily combine these two loaders into one and simply use...

{
    test: /\.(css|scss)$/,
    loaders: ['to-string-loader', 'css-loader', 'sass-loader']
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, your second suggestion worked very well. Thanks! The first gave rise to the same error.
In the case of the first one, material2-app-theme.scss is probably not in the include path. You could just remove that include line altogether. The second is nicer anyway though
I am using angular-cli project, where is the location of scss loader in the project? I searched for that text and I wasn't able to find it
16

For completing the issue, if you use ExtractTextPlugin, the "webpack.config.js" is:

{
    test: /\.(css|scss)$/,
    loaders: ExtractTextPlugin.extract({
        fallback: "style-loader",
        use: ['css-loader?sourceMap', 'scss-loader?sourceMap']
    })
}

Then I solve issue with:

{
    test: /\.(css|scss)$/,
    loaders: ['to-string-loader'].concat(ExtractTextPlugin.extract({
        fallback: "style-loader",
        use: ['css-loader?sourceMap', 'scss-loader?sourceMap']
    }))
}

2 Comments

I've struggling with this for days now, and this solution worked. Thanks a ton, @riofly
This has helped me so much
0

Alternatively, add one inline loader to loaders array, with no extra installation needed

loaders: ['exports-loader?module.exports.toString()', other-loader]

Comments

0

This is the configuration that works for me to load .css files.

packages.json:

"devDependencies": {
    .....
    "to-string-loader": "^1.1.5",
    "css-loader": "^0.28.11"
}    

webpack.js:

module: {
    rules: [
        .....
        {
            test: /\.css$/,
            loaders: ["to-string-loader", "css-loader"]
        }
    ]
}

App.component:

@Component({
    .....
    styleUrls: ["./app.component.css"]
})

Comments

0

It's the raw-loader from webpack which is causing this error

If you were using raw-loader get rid of that and use to-string-load

so install it

npm i to-string-loader --save-dev
or
yarn add to-string-loader

Here is my config section

rules: [
 {
        test: /\.scss$/,
        exclude: [/node_modules/, /\.global\.scss$/],
        use: ["to-string-loader", "css-loader", "sass-loader"]
      }
]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.