Detailed explanation of the utils.js tool configuration file in the vue-cli scaffolding build directory

Detailed explanation of the utils.js tool configuration file in the vue-cli scaffolding build directory

This article is used to explain the utils.js configuration file in the vue-cli scaffolding build directory

  • 1. This configuration file is the wepack-related configuration file of the vue development environment, mainly used to process css-loader and vue-style-loader
  • 2. Regarding comments, when it comes to more complicated explanations, I will write the explanations to a separate comment module by means of identification (such as (1)) , please check it yourself
  • 3*. on the code
// nodejs 
var path = require('path')
// config index.js 
var config = require('../config')
// extract-text-webpack-plugin css css 
// (1)
var ExtractTextPlugin = require('extract-text-webpack-plugin')
//exports module.exports assetsPath
exports.assetsPath = function (_path) {
 // assetsSubDirectory 'static' 'static' 
  var assetsSubDirectory = process.env.NODE_ENV === 'production'
    ? config.build.assetsSubDirectory
    : config.dev.assetsSubDirectory
 //path.join path.posix.join 
 // path.join C:a/a/b/xiangmu/b path.posix.join b
  return path.posix.join(assetsSubDirectory, _path)
 // 
}
 
// cssLoaders 
exports.cssLoaders = function (options) {
 //options 
  options = options || {}
 //cssLoader 
  var cssLoader = {
    loader: 'css-loader',
    options: {
     //options loader 
     //minimize css 
      minimize: process.env.NODE_ENV === 'production',
     // cssmap false
      sourceMap: options.sourceMap
    }
  }
 
 //generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
   // cssLoader 
    var loaders = [cssLoader]
   // loader loaders loader less,sass 
    if (loader) {
      loaders.push({
       // loader
        loader: loader + '-loader',
       //Object.assign es6 
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }
 
   //Extract CSS when that option is specified
   //(which is the case during production build)
   // extract options true false vue 
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
   // loader 
  }
 
 //https://vue-loader.vuejs.org/en/configurations/extract-css.html
  return {
    css: generateLoaders(),//css  vue-style-loader   css-loader
    postcss: generateLoaders(),//postcss  vue-style-loader   css-loader
    less: generateLoaders('less'),//less  vue-style-loader   less-loader
    sass: generateLoaders('sass', { indentedSyntax: true }),//sass  vue-style-loader   sass-loader
    scss: generateLoaders('sass'),//scss  vue-style-loader   sass-loader
    stylus: generateLoaders('stylus'),//stylus  vue-style-loader   stylus-loader
    styl: generateLoaders('stylus')//styl  vue-style-loader   styl-loader 
  }
}
 
//Generate loaders for standalone style files (outside of .vue)
// import exports.cssLoaders 
exports.styleLoaders = function (options) {
  var output = []
 // css loader 
  var loaders = exports.cssLoaders(options)
  for (var extension in loaders) {
   // laoder 
    var loader = loaders[extension]
    output.push({
     // push output 
      test: new RegExp('\\.' + extension + '$'),
      use: loader
    })
  }
  return output
}
 

Note: The extract-text-webpack-pluginplug-in is used to extract the text from the bundle into a separate file. The basic usage is as follows

const ExtractTextPlugin = require("extract-text-webpack-plugin");
 module.exports = {
   module: {
     rules: [
       {
         test:/\.css$/,//css 
         use: ExtractTextPlugin.extract({
           fallback: "style-loader",//fallback css style-loader 
           use: "css-loader"// css-loader js css 
         })
       }
     ],
     plugins: [
       new ExtractTextPlugin("styles.css")//styles.css 
     ]
   }
 }