微信

webpack.base.conf.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var path = require('path')
  2. var config = require('../config')
  3. var utils = require('./utils')
  4. var projectRoot = path.resolve(__dirname, '../')
  5. var glob = require('glob');
  6. var entries = getEntry(['./src/module/*.js', './src/module/**/*.js']); // 获得入口js文件
  7. var env = process.env.NODE_ENV
  8. // check env & config/index.js to decide weither to enable CSS Sourcemaps for the
  9. // various preprocessor loaders added to vue-loader at the end of this file
  10. var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
  11. var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
  12. var useCssSourceMap = cssSourceMapDev || cssSourceMapProd
  13. module.exports = {
  14. entry: entries,
  15. output: {
  16. path: config.build.assetsRoot,
  17. publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
  18. filename: '[name].js'
  19. },
  20. resolve: {
  21. extensions: ['', '.js', '.vue'],
  22. fallback: [path.join(__dirname, '../node_modules')],
  23. alias: {
  24. 'vue$': 'vue/dist/vue',
  25. 'src': path.resolve(__dirname, '../src'),
  26. 'common': path.resolve(__dirname, '../src/common'),
  27. 'components': path.resolve(__dirname, '../src/components')
  28. }
  29. },
  30. resolveLoader: {
  31. fallback: [path.join(__dirname, '../node_modules')]
  32. },
  33. module: {
  34. loaders: [{
  35. test: /\.vue$/,
  36. loader: 'vue'
  37. },
  38. {
  39. test: /\.js$/,
  40. loader: 'babel',
  41. include: projectRoot,
  42. exclude: /node_modules/
  43. },
  44. {
  45. test: /\.json$/,
  46. loader: 'json'
  47. },
  48. {
  49. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  50. loader: 'url',
  51. query: {
  52. limit: 10000,
  53. name: utils.assetsPath('img/[name].[hash:7].[ext]')
  54. }
  55. },
  56. {
  57. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  58. loader: 'url',
  59. query: {
  60. limit: 10000,
  61. name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
  62. }
  63. }
  64. ]
  65. },
  66. vue: {
  67. loaders: utils.cssLoaders({
  68. sourceMap: useCssSourceMap
  69. }),
  70. postcss: [
  71. require('autoprefixer')({
  72. browsers: ['last 2 versions']
  73. })
  74. ]
  75. }
  76. }
  77. function getEntry(globPath) {
  78. var entries = {},
  79. basename, tmp, pathname;
  80. if (typeof (globPath) != "object") {
  81. globPath = [globPath]
  82. }
  83. globPath.forEach((itemPath) => {
  84. glob.sync(itemPath).forEach(function (entry) {
  85. basename = path.basename(entry, path.extname(entry));
  86. if (entry.split('/').length > 4) {
  87. tmp = entry.split('/').splice(-3);
  88. pathname = tmp.splice(0, 1) + '/' + basename; // 正确输出js和html的路径
  89. entries[pathname] = entry;
  90. } else {
  91. entries[basename] = entry;
  92. }
  93. });
  94. });
  95. return entries;
  96. }