utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict'
  2. const path = require('path')
  3. const config = require('../config')
  4. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  5. const packageConfig = require('../package.json')
  6. exports.assetsPath = function (_path) {
  7. const assetsSubDirectory = process.env.NODE_ENV === 'production'
  8. ? config.build.assetsSubDirectory
  9. : config.dev.assetsSubDirectory
  10. return path.posix.join(assetsSubDirectory, _path)
  11. }
  12. exports.cssLoaders = function (options) {
  13. options = options || {}
  14. const cssLoader = {
  15. loader: 'css-loader',
  16. options: {
  17. sourceMap: options.sourceMap
  18. }
  19. }
  20. const postcssLoader = {
  21. loader: 'postcss-loader',
  22. options: {
  23. sourceMap: options.sourceMap
  24. }
  25. }
  26. // generate loader string to be used with extract text plugin
  27. function generateLoaders(loader, loaderOptions) {
  28. const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
  29. if (loader) {
  30. loaders.push({
  31. loader: loader + '-loader',
  32. options: Object.assign({}, loaderOptions, {
  33. sourceMap: options.sourceMap
  34. })
  35. })
  36. }
  37. // Extract CSS when that option is specified
  38. // (which is the case during production build)
  39. if (options.extract) {
  40. return ExtractTextPlugin.extract({
  41. use: loaders,
  42. fallback: 'vue-style-loader'
  43. })
  44. } else {
  45. return ['vue-style-loader'].concat(loaders)
  46. }
  47. }
  48. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  49. return {
  50. css: generateLoaders(),
  51. postcss: generateLoaders(),
  52. less: generateLoaders('less', {
  53. modifyVars: {
  54. // 直接覆盖变量
  55. 'button-info-border-color': '#00c4b8',
  56. 'button-info-background-color': '#00c4b8',
  57. 'checkbox-checked-icon-color': '#00c4b8',
  58. 'radio-checked-icon-color': '#00c4b8',
  59. 'tabs-default-color': '#00c4b8',
  60. },
  61. }),
  62. sass: generateLoaders('sass', { indentedSyntax: true }),
  63. scss: generateLoaders('sass'),
  64. stylus: generateLoaders('stylus'),
  65. styl: generateLoaders('stylus')
  66. }
  67. }
  68. // Generate loaders for standalone style files (outside of .vue)
  69. exports.styleLoaders = function (options) {
  70. const output = []
  71. const loaders = exports.cssLoaders(options)
  72. for (const extension in loaders) {
  73. const loader = loaders[extension]
  74. output.push({
  75. test: new RegExp('\\.' + extension + '$'),
  76. use: loader
  77. })
  78. }
  79. return output
  80. }
  81. exports.createNotifierCallback = () => {
  82. const notifier = require('node-notifier')
  83. return (severity, errors) => {
  84. if (severity !== 'error') return
  85. const error = errors[0]
  86. const filename = error.file && error.file.split('!').pop()
  87. notifier.notify({
  88. title: packageConfig.name,
  89. message: severity + ': ' + error.name,
  90. subtitle: filename || '',
  91. icon: path.join(__dirname, 'logo.png')
  92. })
  93. }
  94. }