webpack.config.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. var path = require("path");
  2. var webpack = require("webpack");
  3. var HtmlwebpackPlugin = require("html-webpack-plugin");
  4. // 引入happypack 实现多线程打包
  5. var HappyPack = require("happypack");
  6. var os = require("os");
  7. var happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });
  8. let BundleAnalyzerPlugin =
  9. require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
  10. function date() {
  11. let nowDate = new Date();
  12. let year = nowDate.getFullYear();
  13. let month =
  14. nowDate.getMonth() + 1 < 10
  15. ? "0" + (nowDate.getMonth() + 1)
  16. : nowDate.getMonth() + 1;
  17. let day =
  18. nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
  19. let hours =
  20. nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
  21. let dateStr = year + "_" + month + day + "_" + hours;
  22. return dateStr;
  23. }
  24. // 基础配置
  25. const config = {
  26. mode: process.env.NODE_ENV === "production" ? "production" : "development",
  27. entry: {
  28. // 注意:这里需要修改,应该只有一个 polyfill,并且作为第一个入口
  29. // 或者使用 babel-polyfill 在入口文件中引入
  30. gwDdIndex: ["babel-polyfill", "./src/pages/gwDdIndex.js"],
  31. jobAccount: ["babel-polyfill", "./src/pages/jobAccount.js"],
  32. problemSeeXsk: ["babel-polyfill", "./src/pages/problemSeeXsk.js"],
  33. problemSeeRy: ["babel-polyfill", "./src/pages/problemSeeRy.js"],
  34. problemSeeSh: ["babel-polyfill", "./src/pages/problemSeeSh.js"],
  35. problemSeeDxs: ["babel-polyfill", "./src/pages/problemSeeDxs.js"],
  36. problemSeeSz: ["babel-polyfill", "./src/pages/problemSeeSz.js"],
  37. home: ["babel-polyfill", "./src/pages/home.js"],
  38. map3D: ["babel-polyfill", "./src/pages/map3D.js"],
  39. inspectionPlanQuery: [
  40. "babel-polyfill",
  41. "./src/pages/inspectionPlanQuery.js",
  42. ],
  43. },
  44. output: {
  45. path: path.resolve(__dirname, "./dist"),
  46. publicPath: "./", // 关键:使用相对路径
  47. filename: "[name]_build.js",
  48. chunkFilename: "chunk_[name].[chunkhash].js",
  49. },
  50. optimization: {
  51. minimize: false,
  52. splitChunks: {
  53. chunks: "all",
  54. cacheGroups: {
  55. vendors: {
  56. test: /[\\/]node_modules[\\/]/,
  57. name: "vendors",
  58. chunks: "all",
  59. },
  60. commons: {
  61. name: "commons",
  62. chunks: "initial",
  63. minChunks: 2,
  64. },
  65. },
  66. },
  67. },
  68. plugins: [],
  69. module: {
  70. rules: [
  71. {
  72. test: /\.css$/,
  73. use: ["vue-style-loader", "css-loader"],
  74. },
  75. {
  76. test: /\.(woff|svg|eot|ttf)\??.*$/,
  77. loader: "url-loader",
  78. },
  79. {
  80. test: /\.vue$/,
  81. use: "happypack/loader?id=happyvue",
  82. },
  83. {
  84. test: /\.js$/,
  85. use: "happypack/loader?id=happybabel",
  86. exclude: /(node_modules|dist|static)/,
  87. },
  88. {
  89. test: /\.(png|jpg|gif|svg)$/,
  90. loader: "file-loader",
  91. options: {
  92. name: "[name].[ext]?[hash]",
  93. },
  94. },
  95. ],
  96. },
  97. resolve: {
  98. alias: {
  99. vue$: "vue/dist/vue.esm.js",
  100. "@api": path.resolve("src/api"),
  101. "@widget": path.resolve("src/widgets"),
  102. "@util": path.resolve("src/utils"),
  103. "@baseWidget": path.resolve("src/widgets/baseWidget"),
  104. "@scss": path.resolve("src/assets/sass"),
  105. },
  106. extensions: [".html", ".js", ".vue", ".json", ".scss"],
  107. modules: [path.resolve("src"), path.resolve("node_modules")],
  108. },
  109. performance: {
  110. hints: false,
  111. },
  112. };
  113. // 开发环境配置
  114. if (process.env.NODE_ENV !== "production") {
  115. config.devtool = "cheap-module-eval-source-map";
  116. config.plugins = [
  117. new webpack.HotModuleReplacementPlugin(),
  118. new webpack.ProvidePlugin({
  119. $: "jquery",
  120. jQuery: "jquery",
  121. jquery: "jquery",
  122. "window.jQuery": "jquery",
  123. }),
  124. new HappyPack({
  125. id: "happybabel",
  126. loaders: [
  127. {
  128. loader: "babel-loader",
  129. options: {
  130. presets: ["es2015"],
  131. plugins: ["syntax-dynamic-import"],
  132. cacheDirectory: true,
  133. },
  134. },
  135. ],
  136. threadPool: happyThreadPool,
  137. verbose: true,
  138. }),
  139. new HappyPack({
  140. id: "happyvue",
  141. loaders: ["vue-loader"],
  142. threadPool: happyThreadPool,
  143. verbose: true,
  144. }),
  145. // HTML 插件
  146. new HtmlwebpackPlugin({
  147. filename: "index.html",
  148. template: "./src/pages/gwDdIndex.html",
  149. chunks: ["gwDdIndex"],
  150. inject: "body", // 确保脚本注入到 body 末尾
  151. hash: true, // 添加 hash 防止缓存
  152. }),
  153. new HtmlwebpackPlugin({
  154. filename: "jobAccount.html",
  155. template: "./src/pages/jobAccount.html",
  156. chunks: ["jobAccount"],
  157. inject: "body",
  158. hash: true,
  159. }),
  160. new HtmlwebpackPlugin({
  161. filename: "problemSeeXsk.html",
  162. template: "./src/pages/problemSeeXsk.html",
  163. chunks: ["problemSeeXsk"],
  164. inject: "body",
  165. hash: true,
  166. }),
  167. new HtmlwebpackPlugin({
  168. filename: "problemSeeRy.html",
  169. template: "./src/pages/problemSeeRy.html",
  170. chunks: ["problemSeeRy"],
  171. inject: "body",
  172. hash: true,
  173. }),
  174. new HtmlwebpackPlugin({
  175. filename: "problemSeeSh.html",
  176. template: "./src/pages/problemSeeSh.html",
  177. chunks: ["problemSeeSh"],
  178. inject: "body",
  179. hash: true,
  180. }),
  181. new HtmlwebpackPlugin({
  182. filename: "problemSeeDxs.html",
  183. template: "./src/pages/problemSeeDxs.html",
  184. chunks: ["problemSeeDxs"],
  185. inject: "body",
  186. hash: true,
  187. }),
  188. new HtmlwebpackPlugin({
  189. filename: "problemSeeSz.html",
  190. template: "./src/pages/problemSeeSz.html",
  191. chunks: ["problemSeeSz"],
  192. inject: "body",
  193. hash: true,
  194. }),
  195. new HtmlwebpackPlugin({
  196. filename: "home.html",
  197. template: "./src/pages/home.html",
  198. chunks: ["home"],
  199. inject: "body",
  200. hash: true,
  201. }),
  202. new HtmlwebpackPlugin({
  203. filename: "map3D.html",
  204. template: "./src/pages/map3D.html",
  205. chunks: ["map3D"],
  206. inject: "body",
  207. hash: true,
  208. }),
  209. new HtmlwebpackPlugin({
  210. filename: "inspectionPlanQuery.html",
  211. template: "./src/pages/inspectionPlanQuery.html",
  212. chunks: ["inspectionPlanQuery"],
  213. inject: "body",
  214. hash: true,
  215. }),
  216. ];
  217. // 开发服务器配置
  218. config.devServer = {
  219. proxy: {
  220. "/pdcApi": {
  221. // target: "http://39.98.38.2:18089/dc-api/",
  222. target: "http://127.0.0.1:8500",
  223. changeOrigin: true,
  224. pathRewrite: {
  225. "^/pdcApi": "",
  226. },
  227. },
  228. "/trs": {
  229. target: "http://10.1.194.19:8080",
  230. changeOrigin: true,
  231. pathRewrite: {
  232. "^/trs": "",
  233. },
  234. },
  235. "/arcgisApi/": {
  236. target: "http://hzz-pad.goldenwater.com.cn/arcgisApi/",
  237. changeOrigin: true,
  238. pathRewrite: {
  239. "^/arcgisApi/": "",
  240. },
  241. },
  242. "/JsReport/": {
  243. target: "http://pdc.goldenwater.com.cn/JsReport/",
  244. changeOrigin: true,
  245. pathRewrite: {
  246. "^/JsReport": "",
  247. },
  248. },
  249. "/demo/": {
  250. target: "http://10.1.102.136:8888/demo/",
  251. changeOrigin: true,
  252. pathRewrite: {
  253. "^/demo": "",
  254. },
  255. },
  256. "/pageofficePath/": {
  257. // target: "http://pdc.goldenwater.com.cn/pageofficePath/",
  258. target: "http://127.0.0.1:9002/",
  259. changeOrigin: true,
  260. pathRewrite: {
  261. "^/pageofficePath": "",
  262. },
  263. },
  264. "/pageoffice/": {
  265. target: "http://127.0.0.1:9002",
  266. changeOrigin: true,
  267. pathRewrite: {
  268. "^/pageoffice": "",
  269. },
  270. },
  271. },
  272. host: "localhost",
  273. port: 8082,
  274. open: true,
  275. hot: true,
  276. inline: true,
  277. compress: true,
  278. contentBase: path.join(__dirname, "dist"),
  279. publicPath: "/",
  280. watchContentBase: true,
  281. // 关键:简化 historyApiFallback
  282. historyApiFallback: {
  283. rewrites: [
  284. { from: /./, to: "/index.html" }, // 所有路径都重定向到 index.html
  285. ],
  286. },
  287. overlay: {
  288. errors: true,
  289. warnings: false,
  290. },
  291. stats: "minimal",
  292. noInfo: true,
  293. };
  294. }
  295. // 生产环境配置
  296. if (process.env.NODE_ENV === "production") {
  297. config.devtool = false;
  298. config.optimization.minimize = true;
  299. config.output = {
  300. path: path.resolve(__dirname, "./dist"),
  301. publicPath: "./", // 生产环境也用相对路径
  302. filename: "[name]_build_v" + date() + ".js",
  303. chunkFilename: "chunk_[name]_v" + date() + ".[chunkhash].js",
  304. };
  305. config.plugins = [
  306. new webpack.ProvidePlugin({
  307. $: "jquery",
  308. jQuery: "jquery",
  309. jquery: "jquery",
  310. "window.jQuery": "jquery",
  311. }),
  312. new HappyPack({
  313. id: "happybabel",
  314. loaders: [
  315. {
  316. loader: "babel-loader",
  317. options: {
  318. presets: ["es2015"],
  319. plugins: ["syntax-dynamic-import"],
  320. cacheDirectory: true,
  321. },
  322. },
  323. ],
  324. threadPool: happyThreadPool,
  325. verbose: true,
  326. }),
  327. new HappyPack({
  328. id: "happyvue",
  329. loaders: ["vue-loader"],
  330. threadPool: happyThreadPool,
  331. verbose: true,
  332. }),
  333. new webpack.DefinePlugin({
  334. "process.env": {
  335. NODE_ENV: '"production"',
  336. },
  337. }),
  338. new webpack.LoaderOptionsPlugin({
  339. minimize: true,
  340. }),
  341. // HTML 插件
  342. new HtmlwebpackPlugin({
  343. filename: "index.html",
  344. template: "./src/pages/gwDdIndex.html",
  345. chunks: ["gwDdIndex"],
  346. inject: "body",
  347. minify: {
  348. removeComments: true,
  349. collapseWhitespace: true,
  350. removeAttributeQuotes: true,
  351. },
  352. hash: true,
  353. }),
  354. new HtmlwebpackPlugin({
  355. filename: "jobAccount.html",
  356. template: "./src/pages/jobAccount.html",
  357. chunks: ["jobAccount"],
  358. inject: "body",
  359. minify: {
  360. removeComments: true,
  361. collapseWhitespace: true,
  362. removeAttributeQuotes: true,
  363. },
  364. hash: true,
  365. }),
  366. new HtmlwebpackPlugin({
  367. filename: "problemSeeXsk.html",
  368. template: "./src/pages/problemSeeXsk.html",
  369. chunks: ["problemSeeXsk"],
  370. inject: "body",
  371. minify: {
  372. removeComments: true,
  373. collapseWhitespace: true,
  374. removeAttributeQuotes: true,
  375. },
  376. hash: true,
  377. }),
  378. new HtmlwebpackPlugin({
  379. filename: "problemSeeRy.html",
  380. template: "./src/pages/problemSeeRy.html",
  381. chunks: ["problemSeeRy"],
  382. inject: "body",
  383. minify: {
  384. removeComments: true,
  385. collapseWhitespace: true,
  386. removeAttributeQuotes: true,
  387. },
  388. hash: true,
  389. }),
  390. new HtmlwebpackPlugin({
  391. filename: "problemSeeSh.html",
  392. template: "./src/pages/problemSeeSh.html",
  393. chunks: ["problemSeeSh"],
  394. inject: "body",
  395. minify: {
  396. removeComments: true,
  397. collapseWhitespace: true,
  398. removeAttributeQuotes: true,
  399. },
  400. hash: true,
  401. }),
  402. new HtmlwebpackPlugin({
  403. filename: "problemSeeDxs.html",
  404. template: "./src/pages/problemSeeDxs.html",
  405. chunks: ["problemSeeDxs"],
  406. inject: "body",
  407. minify: {
  408. removeComments: true,
  409. collapseWhitespace: true,
  410. removeAttributeQuotes: true,
  411. },
  412. hash: true,
  413. }),
  414. new HtmlwebpackPlugin({
  415. filename: "problemSeeSz.html",
  416. template: "./src/pages/problemSeeSz.html",
  417. chunks: ["problemSeeSz"],
  418. inject: "body",
  419. minify: {
  420. removeComments: true,
  421. collapseWhitespace: true,
  422. removeAttributeQuotes: true,
  423. },
  424. hash: true,
  425. }),
  426. new HtmlwebpackPlugin({
  427. filename: "home.html",
  428. template: "./src/pages/home.html",
  429. chunks: ["home"],
  430. inject: "body",
  431. minify: {
  432. removeComments: true,
  433. collapseWhitespace: true,
  434. removeAttributeQuotes: true,
  435. },
  436. hash: true,
  437. }),
  438. new HtmlwebpackPlugin({
  439. filename: "map3D.html",
  440. template: "./src/pages/map3D.html",
  441. chunks: ["map3D"],
  442. inject: "body",
  443. minify: {
  444. removeComments: true,
  445. collapseWhitespace: true,
  446. removeAttributeQuotes: true,
  447. },
  448. hash: true,
  449. }),
  450. new HtmlwebpackPlugin({
  451. filename: "inspectionPlanQuery.html",
  452. template: "./src/pages/inspectionPlanQuery.html",
  453. chunks: ["inspectionPlanQuery"],
  454. inject: "body",
  455. minify: {
  456. removeComments: true,
  457. collapseWhitespace: true,
  458. removeAttributeQuotes: true,
  459. },
  460. hash: true,
  461. }),
  462. // 可选:分析插件
  463. new BundleAnalyzerPlugin({
  464. analyzerMode: "static",
  465. openAnalyzer: false,
  466. }),
  467. ];
  468. }
  469. module.exports = config;