Webpack-simple: SASS variables don't get translated into their values on the page

Viewed 1561

After installing Vue-CLI with webpack-simple, then doing npm install sass-loader node-sass webpack --save-dev, I do have a style.scss in my /src folder.

However despite following the Vue.js docs, SCSS doesn't work properly: if I create a SCSS variable $gray1: #dddddd then use it like background: $gray, $gray doesn't get translated into #dddddd in the browser (console shows background: $gray1, which of course makes no sense to Chrome).

Is there any special step to take in order to make SASS work in this case?

Content of my Webpack.config.js file:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader',
          {
          loader: 'sass-loader',
          options: {}
          }
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true,
    clientLogLevel: 'none'
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

Here is how I import my style.scss file in my main component:

<style>
@import '../style.scss';
</style>
7 Answers

Looking at my own configs and files, change the rule:

  {
    test: /\.css$/,
    use: [
      'vue-style-loader',
      'css-loader',
      {
      loader: 'sass-loader',
      options: {}
      }
    ],
  },

to

        {
            test: /\.s[a|c]ss$/,
            loader: "style!css!sass"
        },

for your <style> in your SFC, you also need the lang, i.e. <style lang="scss">

and if you're importing, you may need to put ~ in front of your path, e.g. @import "~../style.scss";

Make sure you have followed these steps:

In your webpack config import extract-text-webpack-plugin.

var ExtractTextPlugin = require('extract-text-webpack-plugin');

After put your entry scss into config:

module.exports = {
  entry: ['app.js', 'anotherfile.scss', './scss/your-main-file.scss'],
...

And in module..

...
module: {

    rules: [

      {
        test: /\.(sass|scss)$/,
        loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
      }
    ]
  },
...

Fulfilling these steps and having the modules correctly installed should work for you. Good luck with that!

Try something like this:

{
  test: /\.(css|scss)$/,
  use: [isProduction ? MiniCssExtractPlugin.loader : "style-loader", "css-loader", "sass-loader"],
}

I can't find the source right now. But if the SASS file you're exporting includes variables you are attempting to use in other SASS files/ style tags, the filename has to start with an underscore.

style.scss -> _style.scss

If the SASS file only uses SASS variables from other SASS files, then it doesn't require the starting underscore.

Try changing the rules for sass something like this

{ test: /\.scss$/, use: [{ loader: "style-loader" // creates style nodes from JS strings }, { loader: "css-loader" // translates CSS into CommonJS }, { loader: "sass-loader" // compiles Sass to CSS }] }

With your rules, it should work... If your style.scss is in your src folder, try to change your import path instead:

<style lang="scss">
  @import './style.scss';
</style>

In case anyone with the same issue comes across that thread, the problem was that I forgot to specify lang="scss" when calling my SCSS file:

<style lang="scss">
  @import './style.scss';
</style>

All the CSS contained in style.scss still worked. As for the SCSS variables defined or used in it, they didn't work. Now they do.

For some reason, nobody spotted that :)

Related