utils.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict'
  2. const path = require('path')
  3. const config = require('../config')
  4. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  5. const pkg = 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. var 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. sass: generateLoaders('sass', { indentedSyntax: true }),
  54. scss: generateLoaders('sass'),
  55. stylus: generateLoaders('stylus'),
  56. styl: generateLoaders('stylus')
  57. }
  58. }
  59. // Generate loaders for standalone style files (outside of .vue)
  60. exports.styleLoaders = function (options) {
  61. const output = []
  62. const loaders = exports.cssLoaders(options)
  63. for (const extension in loaders) {
  64. const loader = loaders[extension]
  65. output.push({
  66. test: new RegExp('\\.' + extension + '$'),
  67. use: loader
  68. })
  69. }
  70. return output
  71. }
  72. exports.createNotifierCallback = function () {
  73. const notifier = require('node-notifier')
  74. return (severity, errors) => {
  75. if (severity !== 'error') {
  76. return
  77. }
  78. const error = errors[0]
  79. const filename = error.file && error.file.split('!').pop()
  80. notifier.notify({
  81. title: pkg.name,
  82. message: severity + ': ' + error.name,
  83. subtitle: filename || '',
  84. icon: path.join(__dirname, 'logo.png')
  85. })
  86. }
  87. }