utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. loaders.push({
  35. loader: MiniCssExtractPlugin.loader,
  36. options: {publicPath:'../../'}
  37. })
  38. } else {
  39. loaders.push('vue-style-loader')
  40. }
  41. loaders.push(cssLoader)
  42. if (options.usePostCSS) {
  43. loaders.push(postcssLoader)
  44. }
  45. if (loader) {
  46. loaders.push({
  47. loader: loader + '-loader',
  48. options: Object.assign({}, loaderOptions, {
  49. sourceMap: options.sourceMap
  50. })
  51. })
  52. }
  53. return loaders
  54. }
  55. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  56. return {
  57. css: generateLoaders(),
  58. postcss: generateLoaders(),
  59. less: generateLoaders('less'),
  60. sass: generateLoaders('sass', {
  61. indentedSyntax: true
  62. }),
  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_white.png')
  92. })
  93. }
  94. }