utils.js 4.5 KB

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