fb04cb08514c48c0205ff59f14c6e2e204cf0c4c.svn-base 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. changeOrigin: true,
  223. pathRewrite: {
  224. "^/pdcApi": "",
  225. },
  226. },
  227. "/trs": {
  228. target: "http://10.1.194.19:8080",
  229. changeOrigin: true,
  230. pathRewrite: {
  231. "^/trs": "",
  232. },
  233. },
  234. "/arcgisApi/": {
  235. target: "http://hzz-pad.goldenwater.com.cn/arcgisApi/",
  236. changeOrigin: true,
  237. pathRewrite: {
  238. "^/arcgisApi/": "",
  239. },
  240. },
  241. "/JsReport/": {
  242. target: "http://pdc.goldenwater.com.cn/JsReport/",
  243. changeOrigin: true,
  244. pathRewrite: {
  245. "^/JsReport": "",
  246. },
  247. },
  248. "/demo/": {
  249. target: "http://10.1.102.136:8888/demo/",
  250. changeOrigin: true,
  251. pathRewrite: {
  252. "^/demo": "",
  253. },
  254. },
  255. "/pageofficePath/": {
  256. target: "http://pdc.goldenwater.com.cn/pageofficePath/",
  257. changeOrigin: true,
  258. pathRewrite: {
  259. "^/pageofficePath": "",
  260. },
  261. },
  262. },
  263. host: "localhost",
  264. port: 8082,
  265. open: true,
  266. hot: true,
  267. inline: true,
  268. compress: true,
  269. contentBase: path.join(__dirname, "dist"),
  270. publicPath: "/",
  271. watchContentBase: true,
  272. // 关键:简化 historyApiFallback
  273. historyApiFallback: {
  274. rewrites: [
  275. { from: /./, to: "/index.html" }, // 所有路径都重定向到 index.html
  276. ],
  277. },
  278. overlay: {
  279. errors: true,
  280. warnings: false,
  281. },
  282. stats: "minimal",
  283. noInfo: true,
  284. };
  285. }
  286. // 生产环境配置
  287. if (process.env.NODE_ENV === "production") {
  288. config.devtool = false;
  289. config.optimization.minimize = true;
  290. config.output = {
  291. path: path.resolve(__dirname, "./dist"),
  292. publicPath: "./", // 生产环境也用相对路径
  293. filename: "[name]_build_v" + date() + ".js",
  294. chunkFilename: "chunk_[name]_v" + date() + ".[chunkhash].js",
  295. };
  296. config.plugins = [
  297. new webpack.ProvidePlugin({
  298. $: "jquery",
  299. jQuery: "jquery",
  300. jquery: "jquery",
  301. "window.jQuery": "jquery",
  302. }),
  303. new HappyPack({
  304. id: "happybabel",
  305. loaders: [
  306. {
  307. loader: "babel-loader",
  308. options: {
  309. presets: ["es2015"],
  310. plugins: ["syntax-dynamic-import"],
  311. cacheDirectory: true,
  312. },
  313. },
  314. ],
  315. threadPool: happyThreadPool,
  316. verbose: true,
  317. }),
  318. new HappyPack({
  319. id: "happyvue",
  320. loaders: ["vue-loader"],
  321. threadPool: happyThreadPool,
  322. verbose: true,
  323. }),
  324. new webpack.DefinePlugin({
  325. "process.env": {
  326. NODE_ENV: '"production"',
  327. },
  328. }),
  329. new webpack.LoaderOptionsPlugin({
  330. minimize: true,
  331. }),
  332. // HTML 插件
  333. new HtmlwebpackPlugin({
  334. filename: "index.html",
  335. template: "./src/pages/gwDdIndex.html",
  336. chunks: ["gwDdIndex"],
  337. inject: "body",
  338. minify: {
  339. removeComments: true,
  340. collapseWhitespace: true,
  341. removeAttributeQuotes: true,
  342. },
  343. hash: true,
  344. }),
  345. new HtmlwebpackPlugin({
  346. filename: "jobAccount.html",
  347. template: "./src/pages/jobAccount.html",
  348. chunks: ["jobAccount"],
  349. inject: "body",
  350. minify: {
  351. removeComments: true,
  352. collapseWhitespace: true,
  353. removeAttributeQuotes: true,
  354. },
  355. hash: true,
  356. }),
  357. new HtmlwebpackPlugin({
  358. filename: "problemSeeXsk.html",
  359. template: "./src/pages/problemSeeXsk.html",
  360. chunks: ["problemSeeXsk"],
  361. inject: "body",
  362. minify: {
  363. removeComments: true,
  364. collapseWhitespace: true,
  365. removeAttributeQuotes: true,
  366. },
  367. hash: true,
  368. }),
  369. new HtmlwebpackPlugin({
  370. filename: "problemSeeRy.html",
  371. template: "./src/pages/problemSeeRy.html",
  372. chunks: ["problemSeeRy"],
  373. inject: "body",
  374. minify: {
  375. removeComments: true,
  376. collapseWhitespace: true,
  377. removeAttributeQuotes: true,
  378. },
  379. hash: true,
  380. }),
  381. new HtmlwebpackPlugin({
  382. filename: "problemSeeSh.html",
  383. template: "./src/pages/problemSeeSh.html",
  384. chunks: ["problemSeeSh"],
  385. inject: "body",
  386. minify: {
  387. removeComments: true,
  388. collapseWhitespace: true,
  389. removeAttributeQuotes: true,
  390. },
  391. hash: true,
  392. }),
  393. new HtmlwebpackPlugin({
  394. filename: "problemSeeDxs.html",
  395. template: "./src/pages/problemSeeDxs.html",
  396. chunks: ["problemSeeDxs"],
  397. inject: "body",
  398. minify: {
  399. removeComments: true,
  400. collapseWhitespace: true,
  401. removeAttributeQuotes: true,
  402. },
  403. hash: true,
  404. }),
  405. new HtmlwebpackPlugin({
  406. filename: "problemSeeSz.html",
  407. template: "./src/pages/problemSeeSz.html",
  408. chunks: ["problemSeeSz"],
  409. inject: "body",
  410. minify: {
  411. removeComments: true,
  412. collapseWhitespace: true,
  413. removeAttributeQuotes: true,
  414. },
  415. hash: true,
  416. }),
  417. new HtmlwebpackPlugin({
  418. filename: "home.html",
  419. template: "./src/pages/home.html",
  420. chunks: ["home"],
  421. inject: "body",
  422. minify: {
  423. removeComments: true,
  424. collapseWhitespace: true,
  425. removeAttributeQuotes: true,
  426. },
  427. hash: true,
  428. }),
  429. new HtmlwebpackPlugin({
  430. filename: "map3D.html",
  431. template: "./src/pages/map3D.html",
  432. chunks: ["map3D"],
  433. inject: "body",
  434. minify: {
  435. removeComments: true,
  436. collapseWhitespace: true,
  437. removeAttributeQuotes: true,
  438. },
  439. hash: true,
  440. }),
  441. new HtmlwebpackPlugin({
  442. filename: "inspectionPlanQuery.html",
  443. template: "./src/pages/inspectionPlanQuery.html",
  444. chunks: ["inspectionPlanQuery"],
  445. inject: "body",
  446. minify: {
  447. removeComments: true,
  448. collapseWhitespace: true,
  449. removeAttributeQuotes: true,
  450. },
  451. hash: true,
  452. }),
  453. // 可选:分析插件
  454. new BundleAnalyzerPlugin({
  455. analyzerMode: "static",
  456. openAnalyzer: false,
  457. }),
  458. ];
  459. }
  460. module.exports = config;