utils.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = 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. const 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. publicPath:'../../',
  44. })
  45. } else {
  46. return ['vue-style-loader'].concat(loaders)
  47. }
  48. }
  49. // 全局文件引入 当然只想编译一个文件的话可以省去这个函数
  50. function resolveResource(name) {
  51. return path.resolve(__dirname, '../src/styles/' + name);
  52. }
  53. function generateSassResourceLoader() {
  54. var loaders = [
  55. cssLoader,
  56. 'sass-loader',
  57. {
  58. loader: 'sass-resources-loader',
  59. options: {
  60. // 多个文件时用数组的形式传入,单个文件时可以直接使用 path.resolve(__dirname, '../static/style/common.scss'
  61. resources: [resolveResource('variables.scss')]
  62. }
  63. }
  64. ];
  65. if (options.extract) {
  66. return ExtractTextPlugin.extract({
  67. use: loaders,
  68. fallback: 'vue-style-loader'
  69. })
  70. } else {
  71. return ['vue-style-loader'].concat(loaders)
  72. }
  73. }
  74. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  75. return {
  76. css: generateLoaders(),
  77. postcss: generateLoaders(),
  78. less: generateLoaders('less'),
  79. // sass: generateLoaders('sass', { indentedSyntax: true }),
  80. // scss: generateLoaders('sass'),
  81. sass: generateSassResourceLoader(),
  82. scss: generateSassResourceLoader(),
  83. stylus: generateLoaders('stylus'),
  84. styl: generateLoaders('stylus')
  85. }
  86. }
  87. // Generate loaders for standalone style files (outside of .vue)
  88. exports.styleLoaders = function (options) {
  89. const output = []
  90. const loaders = exports.cssLoaders(options)
  91. for (const extension in loaders) {
  92. const loader = loaders[extension]
  93. output.push({
  94. test: new RegExp('\\.' + extension + '$'),
  95. use: loader
  96. })
  97. }
  98. return output
  99. }
  100. exports.createNotifierCallback = () => {
  101. const notifier = require('node-notifier')
  102. return (severity, errors) => {
  103. if (severity !== 'error') return
  104. const error = errors[0]
  105. const filename = error.file && error.file.split('!').pop()
  106. notifier.notify({
  107. title: packageConfig.name,
  108. message: severity + ': ' + error.name,
  109. subtitle: filename || '',
  110. icon: path.join(__dirname, 'logo.png')
  111. })
  112. }
  113. }