utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: {
  37. publicPath: '../../'
  38. }
  39. })
  40. } else {
  41. loaders.push('vue-style-loader')
  42. }
  43. loaders.push(cssLoader)
  44. if (options.usePostCSS) {
  45. loaders.push(postcssLoader)
  46. }
  47. if (loader) {
  48. loaders.push({
  49. loader: loader + '-loader',
  50. options: Object.assign({}, loaderOptions, {
  51. sourceMap: options.sourceMap
  52. })
  53. })
  54. }
  55. return loaders
  56. }
  57. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  58. return {
  59. css: generateLoaders(),
  60. postcss: generateLoaders(),
  61. less: generateLoaders('less'),
  62. sass: generateLoaders('sass', {
  63. indentedSyntax: true
  64. }),
  65. scss: generateLoaders('sass').concat({
  66. loader: 'sass-resources-loader',
  67. options: {
  68. resources: path.resolve(__dirname, '../src/styles/xr-theme.scss')
  69. }
  70. }),
  71. stylus: generateLoaders('stylus'),
  72. styl: generateLoaders('stylus')
  73. }
  74. }
  75. // Generate loaders for standalone style files (outside of .vue)
  76. exports.styleLoaders = function (options) {
  77. const output = []
  78. const loaders = exports.cssLoaders(options)
  79. for (const extension in loaders) {
  80. const loader = loaders[extension]
  81. output.push({
  82. test: new RegExp('\\.' + extension + '$'),
  83. use: loader
  84. })
  85. }
  86. return output
  87. }
  88. exports.createNotifierCallback = () => {
  89. const notifier = require('node-notifier')
  90. return (severity, errors) => {
  91. if (severity !== 'error') return
  92. const error = errors[0]
  93. const filename = error.file && error.file.split('!').pop()
  94. notifier.notify({
  95. title: packageConfig.name,
  96. message: severity + ': ' + error.name,
  97. subtitle: filename || '',
  98. icon: path.join(__dirname, 'logo.png')
  99. })
  100. }
  101. }