vue project uses scss

1. Install sass dependencies

npm  install sass-loader --save-dev
npm install node-sass --sava-dev
npm install vue-style-loader --sava-dev

vue-loader is a webpack loader. Used to convert vue components into deployable js, html, css modules. So if we want to use scss in the vue project, we must tell vue-loader how to parse my scss file.

In webpack, all preprocessors must match the corresponding loaders. Does vue-loader allow other webpack-loaders to process part of the component, and then it automatically determines the loaders to be used based on the lang attribute. So, just install the loader that handles sass/scss. You can use scss in vue.

No need to configure loader for scss/sass, because vue-loader will automatically use the corresponding loader according to the file format suffix name

2. Why no need to configure

As we mentioned earlier, vue-loader allows automatic determination of loaders to be used based on the lang attribute. How does it do it? Is it so amazing? Let’s take a look at the source code of the core part

exports.cssLoaders = function (options) {
    
    
 options = options || {
    
    }
 
 var cssLoader = {
    
    
  loader: 'css-loader',
  options: {
    
    
   minimize: process.env.NODE_ENV === 'production',
   sourceMap: options.sourceMap
  }
 }
 
 // generate loader string to be used with extract text plugin
 function generateLoaders (loader, loaderOptions) {
    
    
  var loaders = [cssLoader]
  if (loader) {
    
    
   loaders.push({
    
    
    loader: loader + '-loader',
    options: Object.assign({
    
    }, loaderOptions, {
    
    
     sourceMap: options.sourceMap
    })
   })
  }
 
  // Extract CSS when that option is specified
  // (which is the case during production build)
  if (options.extract) {
    
    
   return ExtractTextPlugin.extract({
    
    
    use: loaders,
    fallback: 'vue-style-loader'
   })
  } else {
    
    
   return ['vue-style-loader'].concat(loaders)
  }
 }
 
 // https://vue-loader.vuejs.org/en/configurations/extract-css.html
 return {
    
    
  css: generateLoaders(),
  postcss: generateLoaders(),
  less: generateLoaders('less'),
  sass: generateLoaders('sass', {
    
     indentedSyntax: true }),
  scss: generateLoaders('sass'),
  stylus: generateLoaders('stylus'),
  styl: generateLoaders('stylus')
 }
}

It is the above code that gives vue-loader this ability. It will use different loaders according to different files.

3. The next step is to use scss normally

Guess you like

Origin blog.csdn.net/qq_17627195/article/details/109047073