utils.js 2.5 KB
Newer Older
L
lin-xin 已提交
1 2 3 4 5
'use strict'
const path = require('path')
const config = require('../config')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const packageConfig = require('../package.json')
L
lin-xin 已提交
6 7

exports.assetsPath = function (_path) {
L
lin-xin 已提交
8
  const assetsSubDirectory = process.env.NODE_ENV === 'production'
L
lin-xin 已提交
9 10
    ? config.build.assetsSubDirectory
    : config.dev.assetsSubDirectory
L
lin-xin 已提交
11

L
lin-xin 已提交
12 13 14 15 16
  return path.posix.join(assetsSubDirectory, _path)
}

exports.cssLoaders = function (options) {
  options = options || {}
L
lin-xin 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  const cssLoader = {
    loader: 'css-loader',
    options: {
      sourceMap: options.sourceMap
    }
  }

  const postcssLoader = {
    loader: 'postcss-loader',
    options: {
      sourceMap: options.sourceMap
    }
  }

L
lin-xin 已提交
32
  // generate loader string to be used with extract text plugin
L
lin-xin 已提交
33 34 35 36 37 38 39 40 41 42 43
  function generateLoaders (loader, loaderOptions) {
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }
L
lin-xin 已提交
44 45 46 47 48

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
L
lin-xin 已提交
49
        use: loaders,
L
lin-xin 已提交
50 51 52
        fallback: 'vue-style-loader'
      })
    } else {
L
lin-xin 已提交
53
      return ['vue-style-loader'].concat(loaders)
L
lin-xin 已提交
54 55 56
    }
  }

L
lin-xin 已提交
57
  // https://vue-loader.vuejs.org/en/configurations/extract-css.html
L
lin-xin 已提交
58
  return {
L
lin-xin 已提交
59 60 61 62 63 64 65
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass'),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
L
lin-xin 已提交
66 67 68 69 70
  }
}

// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
L
lin-xin 已提交
71 72 73 74 75
  const output = []
  const loaders = exports.cssLoaders(options)

  for (const extension in loaders) {
    const loader = loaders[extension]
L
lin-xin 已提交
76 77
    output.push({
      test: new RegExp('\\.' + extension + '$'),
L
lin-xin 已提交
78
      use: loader
L
lin-xin 已提交
79 80
    })
  }
L
lin-xin 已提交
81

L
lin-xin 已提交
82 83
  return output
}
L
lin-xin 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

exports.createNotifierCallback = () => {
  const notifier = require('node-notifier')

  return (severity, errors) => {
    if (severity !== 'error') return

    const error = errors[0]
    const filename = error.file && error.file.split('!').pop()

    notifier.notify({
      title: packageConfig.name,
      message: severity + ': ' + error.name,
      subtitle: filename || '',
      icon: path.join(__dirname, 'logo.png')
    })
  }
}