utils.js 2.8 KB

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