webpack.base.conf.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "@uiSrcPath": resolve('node_modules/ui-component-v4/src'),
  30. }
  31. },
  32. module: {
  33. rules: [
  34. {
  35. test: /\.vue$/,
  36. loader: "vue-loader",
  37. options: vueLoaderConfig
  38. },
  39. {
  40. test: /\.js$/,
  41. loader: "babel-loader",
  42. // exclude: /node_modules/
  43. include: [
  44. resolve("src"),
  45. resolve("test"),
  46. resolve("node_modules/ui-jz-v4-common"),
  47. resolve("node_modules/webpack-dev-server/client")
  48. ]
  49. },
  50. {
  51. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  52. loader: "url-loader",
  53. options: {
  54. limit: 10000,
  55. name: utils.assetsPath("img/[name].[hash:7].[ext]")
  56. }
  57. },
  58. {
  59. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  60. loader: "url-loader",
  61. options: {
  62. limit: 10000,
  63. name: utils.assetsPath("media/[name].[hash:7].[ext]")
  64. }
  65. },
  66. {
  67. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  68. loader: "url-loader",
  69. options: {
  70. limit: 10000,
  71. name: utils.assetsPath("fonts/[name].[hash:7].[ext]")
  72. }
  73. },
  74. {
  75. test: /\.(txt|htm)$/,
  76. loader: "raw-loader"
  77. }
  78. ]
  79. },
  80. optimization: {
  81. splitChunks: {
  82. cacheGroups: {
  83. commons: {
  84. // 抽离自己写的公共代码
  85. chunks: "async", // async针对异步加载的chunk做切割,initial针对初始chunk,all针对所有chunk。
  86. name: "common", // 打包后的文件名,任意命名
  87. minChunks: 2, //最小引用2次
  88. minSize: 10000 // 只要超出30000字节就生成一个新包
  89. },
  90. vendor: {
  91. // 抽离第三方插件
  92. test: /[\\/]node_modules[\\/]/, // 指定是node_modules下的第三方包
  93. chunks: "initial",
  94. name: "vendor", // 打包后的文件名,任意命名
  95. priority: 10 // 设置优先级,防止和自定义的公共代码提取时被覆盖,不进行打包
  96. }
  97. }
  98. }
  99. // runtimeChunk: true // 持久缓存moduleID,ChunkID需要HashedModuleIdsPlugin等插件解决
  100. },
  101. node: {
  102. // prevent webpack from injecting useless setImmediate polyfill because Vue
  103. // source contains it (although only uses it if it's native).
  104. setImmediate: false,
  105. // prevent webpack from injecting mocks to Node native modules
  106. // that does not make sense for the client
  107. dgram: "empty",
  108. fs: "empty",
  109. net: "empty",
  110. tls: "empty",
  111. child_process: "empty"
  112. },
  113. plugins: [
  114. new VueLoaderPlugin(),
  115. new webpack.ProvidePlugin({
  116. $: "jquery",
  117. jQuery: "jquery"
  118. })
  119. //是否启用代码依赖分析
  120. // new BundleAnalyzerPlugin()
  121. ]
  122. };