utils.js 2.5 KB

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