webpack.base.conf.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var path = require('path')
  2. var fs = require('fs')
  3. var utils = require('./utils')
  4. var config = require('../config')
  5. var vueLoaderConfig = require('./vue-loader.conf')
  6. var MpvuePlugin = require('webpack-mpvue-asset-plugin')
  7. var glob = require('glob')
  8. var CopyWebpackPlugin = require('copy-webpack-plugin')
  9. var relative = require('relative')
  10. const MpvueEntry = require('mpvue-entry')
  11. function resolve (dir) {
  12. return path.join(__dirname, '..', dir)
  13. }
  14. function getEntry (rootSrc) {
  15. var map = {}
  16. glob.sync(rootSrc + '/pages/**/main.js')
  17. .forEach(file => {
  18. var key = relative(rootSrc, file).replace('.js', '')
  19. map[key] = file
  20. })
  21. return map
  22. }
  23. const appEntry = { app: resolve('./src/main.js') }
  24. const pagesEntry = getEntry(resolve('./src'), 'pages/**/main.js')
  25. const entry = Object.assign({}, appEntry, pagesEntry)
  26. module.exports = {
  27. // 如果要自定义生成的 dist 目录里面的文件路径,
  28. // 可以将 entry 写成 {'toPath': 'fromPath'} 的形式,
  29. // toPath 为相对于 dist 的路径, 例:index/demo,则生成的文件地址为 dist/index/demo.js
  30. entry: MpvueEntry.getEntry('src/router/router.js'),
  31. target: require('mpvue-webpack-target'),
  32. output: {
  33. path: config.build.assetsRoot,
  34. filename: '[name].js',
  35. publicPath: process.env.NODE_ENV === 'production'
  36. ? config.build.assetsPublicPath
  37. : config.dev.assetsPublicPath
  38. },
  39. resolve: {
  40. extensions: ['.js', '.vue', '.json'],
  41. alias: {
  42. 'vue': 'mpvue',
  43. '@': resolve('src')
  44. },
  45. symlinks: false,
  46. aliasFields: ['mpvue', 'weapp', 'browser'],
  47. mainFields: ['browser', 'module', 'main']
  48. },
  49. module: {
  50. rules: [
  51. {
  52. test: /\.(js|vue)$/,
  53. loader: 'eslint-loader',
  54. enforce: 'pre',
  55. include: [resolve('src'), resolve('test')],
  56. options: {
  57. formatter: require('eslint-friendly-formatter')
  58. }
  59. },
  60. {
  61. test: /\.vue$/,
  62. loader: 'mpvue-loader',
  63. options: vueLoaderConfig
  64. },
  65. {
  66. test: /\.sass$/,
  67. loaders: ['style', 'css', 'sass']
  68. },
  69. {
  70. test: /\.js$/,
  71. include: [resolve('src'), resolve('test')],
  72. use: [
  73. 'babel-loader',
  74. {
  75. loader: 'mpvue-loader',
  76. options: Object.assign({checkMPEntry: true}, vueLoaderConfig)
  77. }
  78. ]
  79. },
  80. {
  81. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  82. loader: 'url-loader',
  83. options: {
  84. limit: 20000,
  85. name: utils.assetsPath('img/[name].[ext]')
  86. }
  87. },
  88. {
  89. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  90. loader: 'url-loader',
  91. options: {
  92. limit: 10000,
  93. name: utils.assetsPath('media/[name].[ext]')
  94. }
  95. },
  96. {
  97. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  98. loader: 'url-loader',
  99. options: {
  100. limit: 10000,
  101. name: utils.assetsPath('fonts/[name].[ext]')
  102. }
  103. }
  104. ]
  105. },
  106. plugins: [
  107. new MpvueEntry(),
  108. new MpvuePlugin(),
  109. new CopyWebpackPlugin([
  110. {
  111. from: path.resolve(__dirname, '../static'),
  112. to: path.resolve(config.build.assetsRoot, './static'),
  113. ignore: ['.*']
  114. }
  115. ])
  116. ]
  117. }