Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

webpack.dev.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const path = require("path");
  2. const webpack = require("webpack");
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  4. const HtmlWebpackPlugin = require("html-webpack-plugin");
  5. const HappyPack = require("happypack");
  6. const os = require("os");
  7. const happyThreadPool = HappyPack.ThreadPool({
  8. size: os.cpus().length,
  9. });
  10. const common = require("./common");
  11. const pathVars = require("./pathVars");
  12. const pageVars = require("./pageVars");
  13. //---------------------------------- output -----------------------------------//
  14. const packageJson = require("./../package.json");
  15. const entryConfig = {
  16. /*第三方库*/
  17. //vendor: Object.keys(packageJson.dependencies)
  18. };
  19. let pages = pageVars.getPages();
  20. //console.log("pages", pages);
  21. pages.forEach(function (page) {
  22. entryConfig[page["chunkName"]] = page["entryJs"];
  23. });
  24. //---------------------------------- output -----------------------------------//
  25. let outputConfig = {
  26. path: pathVars.distPath,
  27. //publicPath: "./", // 表示资源的发布地址,当配置过该属性后,打包文件中所有通过相对路径引用的资源都会被配置的路径所替换(如:css中背景图的路径)
  28. filename: "[name]/index.js",
  29. assetModuleFilename: "images/[name]_[hash][ext][query]",
  30. //chunkFilename: '[name]/index_[hash].chunk.js'
  31. };
  32. //---------------------------------- plugin -----------------------------------//
  33. let pluginConfig = [
  34. // new webpack.ProvidePlugin({
  35. // THREE: require.resolve("three"),
  36. // }),
  37. // new webpack.DllReferencePlugin({
  38. // context: pathVars.rootPath,
  39. // manifest: require(pathVars.dllPath + "/manifest.json"),
  40. // }),
  41. new MiniCssExtractPlugin({
  42. filename: "[name]/index.css",
  43. }),
  44. new HappyPack({
  45. id: "babel",
  46. loaders: ["babel-loader"],
  47. threadPool: happyThreadPool,
  48. }),
  49. ];
  50. pages.forEach(function (page) {
  51. let htmlPlugin = new HtmlWebpackPlugin({
  52. title: "page1",
  53. template: page["filePath"],
  54. filename: page["htmlName"],
  55. inject: "body",
  56. chunks: [page["chunkName"]],
  57. });
  58. console.log(page);
  59. pluginConfig.push(htmlPlugin);
  60. });
  61. //console.log("pluginConfig", pluginConfig);
  62. module.exports = {
  63. cache: {
  64. type: "filesystem",
  65. },
  66. mode: "development",
  67. entry: entryConfig,
  68. //output属性与webpack-dev-server有冲突
  69. output: outputConfig,
  70. resolve: common.resolve,
  71. devServer: {
  72. compress: true,
  73. static: {
  74. directory: pathVars.devPath,
  75. publicPath: "/",
  76. },
  77. hot: true,
  78. // host: "localhost",
  79. // host: '192.168.1.115',
  80. host: '192.168.1.63',
  81. port: 6002,
  82. proxy: {
  83. '/api': { // 代理路径
  84. target: 'http://192.168.1.33:9100', // 目标地址
  85. changeOrigin: true, // 允许跨域
  86. pathRewrite: { '^/api': '' }, // 重写路径
  87. },
  88. },
  89. },
  90. module: {
  91. rules: [
  92. {
  93. test: /\.(css|less)$/,
  94. include: [pathVars.srcPath],
  95. exclude: [pathVars.nodeModulesPath],
  96. use: [
  97. {
  98. loader: MiniCssExtractPlugin.loader,
  99. options: {
  100. /* 解决css背景图路径问题 由于图片目录已经定好了,因此这里可以用固定的相对路径*/
  101. publicPath: "../",
  102. },
  103. },
  104. //"css-loader?modules&localIdentName=[name]__[local]-[hash:base64:5]",
  105. "css-loader",
  106. "less-loader",
  107. ],
  108. },
  109. {
  110. test: /\.tpl$/i,
  111. use: [
  112. {
  113. loader: "raw-loader",
  114. options: {
  115. esModule: false,
  116. },
  117. },
  118. ],
  119. },
  120. {
  121. test: /\.js$/,
  122. exclude: pathVars.nodeModulesPath,
  123. include: pathVars.srcPath,
  124. use: ["babel-loader"],
  125. //use: ["happypack/loader?id=babel"]
  126. },
  127. {
  128. test: /\.(png|jpg|gif|woff|svg|eot|ttf|woff2)$/,
  129. type: "asset/resource",
  130. parser: {
  131. dataUrlCondition: {
  132. maxSize: 10 * 1024,
  133. },
  134. },
  135. //利用HappyPack打包,图片会损坏。暂时不使用
  136. //use: ["happypack/loader?id=url"]
  137. },
  138. {
  139. test: /\.(glsl|vs|fs|vert|frag)$/,
  140. exclude: [pathVars.nodeModulesPath, pathVars.dllPath],
  141. use: ["raw-loader", "glslify-loader"],
  142. },
  143. ],
  144. },
  145. plugins: pluginConfig,
  146. optimization: {
  147. runtimeChunk: "single",
  148. splitChunks: {
  149. chunks: "all",
  150. cacheGroups: {
  151. vendor: {
  152. test: /[\\/]node_modules[\\/]/,
  153. name: "vendor",
  154. chunks: "all",
  155. },
  156. },
  157. },
  158. },
  159. };