SnitcH
SnitcH

Reputation: 51

Webpack SCSS @charset and @import problems

I have following lines in my scss files:

$google-fonts-url: 'https://fonts.googleapis.com/css?family=Source+Sans+Pro|Lato:300';
@import url($google-fonts-url);

The first line in my output-css:

@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro|Lato:300);@charset "UTF-8";
:root {
      --blue: #007bff;

That is totally wrong. I have no idea why but as soon as I delete my Import-statement it works correctly:

@charset "UTF-8";
:root {
  --blue: #007bff;

my webpack-config:

module.exports = {
    entry: { 
        app: './src/app.scss'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].css'
    },
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [ 'css-loader', 'sass-loader']
                }) 

                //['style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            },
            {
                test: /\.(woff(2)?|ttf|eot|svg|gif|jpg|jpeg|png)(\?v=\d+\.\d+\.\d+)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'fonts/'
                    }
                }]
            }
        ]
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                styles: {
                    name: 'styles',
                    test: /\.css$/,
                    chunks: 'all',
                    enforce: true
                }
            }
        }
    },
    plugins: [
        new CleanWebpackPlugin('dist'),
        new ExtractTextPlugin({
            filename: '[name].css'
        }),
        new CssoWebpackPlugin({
            pluginOutputPostfix: 'min'
        })
    ]
}

When I remove Csso&CleanWebpack it's still the same behaviour. What's wrong? I'm confused. It's the wrong order but I can't find whats wrong with my settings.

Versions:

Node: v8.9.4

extract-text-webpack-plugin: ^4.0.0-beta.0

webpack: ^4.28.3

webpack-cli: ^3.2.0

css-loader: ^2.1.0

file-loader: ^3.0.1

Any advice? I Googled for hours now :(

Thx in advance

EDIT: for clarification, my font-import is not the first line in my main-scss file. It comes very late

Upvotes: 5

Views: 2248

Answers (1)

joemaller
joemaller

Reputation: 20606

I had the same issue, it's not Sass, it's css-loader's import option.

import
Enables/Disables @import at-rules handling. Control @import resolving. Absolute urls in @import will be moved in runtime code.

Replacing 'css-loader' with { loader: "css-loader", options: { import: false } } in webpack.config will output your css at-rules in the correct order.

I disable this option since sass-loader is already handling all my imports which don't point to a url.

Upvotes: 3

Related Questions