webpack.base.conf.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. const path = require("path");
  3. const utils = require("./utils");
  4. const config = require("../config");
  5. var webpack = require("webpack");
  6. const { VueLoaderPlugin } = require("vue-loader");
  7. const vueLoaderConfig = require("./vue-loader.conf");
  8. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
  9. function resolve(dir) {
  10. return path.join(__dirname, "..", dir);
  11. }
  12. module.exports = {
  13. context: path.resolve(__dirname, "../"),
  14. entry: {
  15. app: ["babel-polyfill", "./src/main.js"]
  16. },
  17. output: {
  18. path: config.build.assetsRoot,
  19. filename: "[name].js",
  20. publicPath: process.env.NODE_ENV === "production" ? config.build.assetsPublicPath : config.dev.assetsPublicPath
  21. },
  22. resolve: {
  23. extensions: [".js", ".vue", ".json"],
  24. alias: {
  25. vue$: "vue/dist/vue.esm.js", //包含编译,demo的时候使用
  26. "@": resolve("src"),
  27. "@@": resolve("src/components/base"),
  28. "@srcPath": resolve('node_modules/ui-jz-v4-common/src'),
  29. }
  30. },
  31. module: {
  32. rules: [
  33. {
  34. test: /\.vue$/,
  35. loader: "vue-loader",
  36. options: vueLoaderConfig
  37. },
  38. {
  39. test: /\.js$/,
  40. loader: "babel-loader",
  41. // exclude: /node_modules/
  42. include: [
  43. resolve("src"),
  44. resolve("test"),
  45. resolve("node_modules/ui-jz-v4-common"),
  46. resolve("node_modules/webpack-dev-server/client")
  47. ]
  48. },
  49. {
  50. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  51. loader: "url-loader",
  52. options: {
  53. limit: 10000,
  54. name: utils.assetsPath("img/[name].[hash:7].[ext]")
  55. }
  56. },
  57. {
  58. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  59. loader: "url-loader",
  60. options: {
  61. limit: 10000,
  62. name: utils.assetsPath("media/[name].[hash:7].[ext]")
  63. }
  64. },
  65. {
  66. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  67. loader: "url-loader",
  68. options: {
  69. limit: 10000,
  70. name: utils.assetsPath("fonts/[name].[hash:7].[ext]")
  71. }
  72. },
  73. {
  74. test: /\.(txt|htm)$/,
  75. loader: "raw-loader"
  76. }
  77. ]
  78. },
  79. optimization: {
  80. splitChunks: {
  81. cacheGroups: {
  82. commons: {
  83. // 抽离自己写的公共代码
  84. chunks: "async", // async针对异步加载的chunk做切割,initial针对初始chunk,all针对所有chunk。
  85. name: "common", // 打包后的文件名,任意命名
  86. minChunks: 2, //最小引用2次
  87. minSize: 10000 // 只要超出30000字节就生成一个新包
  88. },
  89. vendor: {
  90. // 抽离第三方插件
  91. test: /[\\/]node_modules[\\/]/, // 指定是node_modules下的第三方包
  92. chunks: "initial",
  93. name: "vendor", // 打包后的文件名,任意命名
  94. priority: 10 // 设置优先级,防止和自定义的公共代码提取时被覆盖,不进行打包
  95. }
  96. }
  97. }
  98. // runtimeChunk: true // 持久缓存moduleID,ChunkID需要HashedModuleIdsPlugin等插件解决
  99. },
  100. node: {
  101. // prevent webpack from injecting useless setImmediate polyfill because Vue
  102. // source contains it (although only uses it if it's native).
  103. setImmediate: false,
  104. // prevent webpack from injecting mocks to Node native modules
  105. // that does not make sense for the client
  106. dgram: "empty",
  107. fs: "empty",
  108. net: "empty",
  109. tls: "empty",
  110. child_process: "empty"
  111. },
  112. plugins: [
  113. new VueLoaderPlugin(),
  114. new webpack.ProvidePlugin({
  115. $: "jquery",
  116. jQuery: "jquery"
  117. })
  118. //是否启用代码依赖分析
  119. // new BundleAnalyzerPlugin()
  120. ]
  121. };