utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. require("babel-polyfill")
  7. exports.assetsPath = function (_path) {
  8. const assetsSubDirectory = 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 = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
  30. if (loader) {
  31. loaders.push({
  32. loader: loader + '-loader',
  33. options: Object.assign({}, loaderOptions, {
  34. sourceMap: options.sourceMap
  35. })
  36. })
  37. }
  38. // Extract CSS when that option is specified
  39. // (which is the case during production build)
  40. if (options.extract) {
  41. return ExtractTextPlugin.extract({
  42. use: loaders,
  43. fallback: 'vue-style-loader',
  44. publicPath: '../../'
  45. })
  46. } else {
  47. return ['vue-style-loader'].concat(loaders)
  48. }
  49. }
  50. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  51. return {
  52. css: generateLoaders(),
  53. postcss: generateLoaders(),
  54. less: generateLoaders('less'),
  55. sass: generateLoaders('sass', { indentedSyntax: true }),
  56. scss: generateLoaders('sass'),
  57. stylus: generateLoaders('stylus'),
  58. styl: generateLoaders('stylus')
  59. }
  60. }
  61. // Generate loaders for standalone style files (outside of .vue)
  62. exports.styleLoaders = function (options) {
  63. const output = []
  64. const loaders = exports.cssLoaders(options)
  65. for (const extension in loaders) {
  66. const loader = loaders[extension]
  67. output.push({
  68. test: new RegExp('\\.' + extension + '$'),
  69. use: loader
  70. })
  71. }
  72. return output
  73. }
  74. /* 这里是添加的部分 ---------------------------- 开始 */
  75. // glob是webpack安装时依赖的一个第三方模块,还模块允许你使用 *等符号, 例如lib/*.js就是获取lib文件夹下的所有js后缀名的文件
  76. var glob = require('glob')
  77. // 页面模板
  78. var HtmlWebpackPlugin = require('html-webpack-plugin')
  79. // 取得相应的页面路径,因为之前的配置,所以是src文件夹下的pages文件夹
  80. var PAGE_PATH = path.resolve(__dirname, '../src/pages')
  81. // 用于做相应的merge处理
  82. var merge = require('webpack-merge')
  83. //多入口配置
  84. // 通过glob模块读取pages文件夹下的所有对应文件夹下的js后缀文件,如果该文件存在
  85. // 那么就作为入口处理
  86. exports.entries = function () {
  87. var entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
  88. var map = {}
  89. entryFiles.forEach((filePath) => {
  90. var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
  91. map[filename] = ['babel-polyfill', filePath]
  92. })
  93. return map
  94. }
  95. //多页面输出配置
  96. // 与上面的多页面入口配置相同,读取pages文件夹下的对应的html后缀文件,然后放入数组中
  97. exports.htmlPlugin = function () {
  98. let entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
  99. let arr = []
  100. entryHtml.forEach((filePath) => {
  101. let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
  102. let conf = {
  103. // 模板来源
  104. template: filePath,
  105. // 文件名称
  106. filename: filename + '.html',
  107. // 页面模板需要加对应的js脚本,如果不加这行则每个页面都会引入所有的js脚本
  108. chunks: ['manifest', 'vendor', filename],
  109. inject: true
  110. }
  111. if (process.env.NODE_ENV === 'production') {
  112. conf = merge(conf, {
  113. minify: {
  114. removeComments: true,
  115. collapseWhitespace: true,
  116. removeAttributeQuotes: true
  117. },
  118. chunksSortMode: 'dependency'
  119. })
  120. }
  121. arr.push(new HtmlWebpackPlugin(conf))
  122. })
  123. return arr
  124. }
  125. /* 这里是添加的部分 ---------------------------- 结束 */
  126. exports.createNotifierCallback = () => {
  127. const notifier = require('node-notifier')
  128. return (severity, errors) => {
  129. if (severity !== 'error') return
  130. const error = errors[0]
  131. const filename = error.file && error.file.split('!').pop()
  132. notifier.notify({
  133. title: packageConfig.name,
  134. message: severity + ': ' + error.name,
  135. subtitle: filename || '',
  136. icon: path.join(__dirname, 'logo.png')
  137. })
  138. }
  139. }