信通院-灾备 图表源码(在产品图表库基础上进行修改),开发逻辑和产品图表库一致
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.

config.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. const path = require('path');
  2. const webpack = require('webpack');
  3. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  4. const glob = require("glob");
  5. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  6. const HtmlWebpackPlugin = require('html-webpack-plugin');
  7. // const CopyWebpackPlugin = require("copy-webpack-plugin");
  8. const CleanWebpackPlugin = require('clean-webpack-plugin');
  9. const packageJson = require("./../package.json");
  10. const HappyPack = require('happypack');
  11. const os = require('os');
  12. const happyThreadPool = HappyPack.ThreadPool({size: os.cpus().length});
  13. let pathVars = require("./pathVars");
  14. let config = {};
  15. /*----------------------------------- page ---------------------------------*/
  16. //测试页面
  17. // let files = glob.sync(pathVars.testPath + `/pie/ChartPie?(9|21|32)/index.html`);
  18. let files = glob.sync(pathVars.testPath + `/pie/ChartPie38/index.html`);
  19. // files = files.concat(glob.sync(pathVars.testPath + "/center/ChartCenterTop1/index.html"));
  20. // files = files.concat(glob.sync(pathVars.testPath + "/digit/ChartDigit5/index.html"));
  21. // files = files.concat(glob.sync(pathVars.testPath + "/digit/ChartDigit21/index.html"));
  22. // files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie38/index.html"));
  23. // files = files.concat(glob.sync(pathVars.testPath + "/map/ChartChinaMap5/index.html"));
  24. // files = files.concat(glob.sync(pathVars.testPath + "/map/ChartChinaMap1/index.html"));
  25. // files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie31/index.html"));
  26. // files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie39/index.html"));
  27. // files = files.concat(glob.sync(pathVars.testPath + "/pie/ChartPie40/index.html"));
  28. // files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartPie38blue/index.html"));
  29. // files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar5/index.html"));
  30. // files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar8/index.html"));
  31. // files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar40/index.html"));
  32. // files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartBar41/index.html"));
  33. // files = files.concat(glob.sync(pathVars.testPath + "/zaibei/ChartLine3/index.html"));
  34. // files = files.concat(glob.sync(pathVars.testPath + "/bar/ChartBar46/index.html"));
  35. console.log("测试页面\n", files);
  36. let entryDev = {
  37. vendor: Object.keys(packageJson.dependencies)
  38. };
  39. let pageAry = [];
  40. files.forEach(function (filePath) {
  41. //将单个文件路径(/test/ChartBar3D/index.htsml)路径拆分成数组
  42. let tempAry = filePath.split('/');
  43. //demo文件夹名
  44. let typeName = tempAry[tempAry.length - 3];
  45. let chunkName = tempAry[tempAry.length - 2];
  46. let htmlName = chunkName + "/index.html";
  47. let obj = {};
  48. obj["filePath"] = filePath;
  49. obj["chunkName"] = chunkName;
  50. obj["htmlName"] = htmlName;
  51. obj["entryJs"] = pathVars.testPath + "/" + typeName + "/" + chunkName + "/index.js";
  52. entryDev[chunkName] = obj["entryJs"];
  53. pageAry.push(obj);
  54. });
  55. //图表主js文件
  56. let chartFiles = [
  57. glob.sync(pathVars.srcPath + "/chart/pie/ChartPie38/ChartPie38.js")[0],
  58. // chartFiles = glob.sync(pathVars.srcPath + "/chart/digit/ChartDigit5/ChartDigit5.js"),
  59. // chartFiles = glob.sync(pathVars.srcPath + "/chart/pie/ChartPie39/ChartPie39.js"),
  60. // chartFiles = glob.sync(pathVars.srcPath + "/chart/zaibei/ChartBar40/ChartBar40.js"),
  61. // chartFiles = glob.sync(pathVars.srcPath + "/chart/zaibei/ChartLine3/ChartLine3.js"),
  62. // glob.sync(pathVars.srcPath + "/chart/map/ChartChinaMap5/ChartChinaMap5.js"),
  63. // chartFiles = glob.sync(pathVars.srcPath + "/chart/center/ChartCenterTop1/ChartCenterTop1.js")
  64. ]
  65. console.log("图表主js文件\n", chartFiles);
  66. let chartAry = [];
  67. let entryDist = {};
  68. chartFiles.forEach(function (filePath) {
  69. //将单个文件路径(/src/chart/ChartEarth/ChartEarth.js)路径拆分成数组
  70. let tempAry = filePath.split('/');
  71. let chunkName = tempAry[tempAry.length - 2];
  72. let obj = {};
  73. obj["chunkName"] = chunkName;
  74. chartAry.push(obj);
  75. //
  76. entryDist[chunkName] = filePath;
  77. });
  78. /*----------------------------- entry ------------------------------*/
  79. config["entryDev"] = entryDev;
  80. config["entryDist"] = entryDist;
  81. /*----------------------------- devServer ------------------------------*/
  82. let devServer = {
  83. // open: true,
  84. publicPath: "/",
  85. contentBase: pathVars.devPath,
  86. hot: true,
  87. inline: true,
  88. port: 3007,
  89. host: 'localhost'
  90. };
  91. config["devServer"] = devServer;
  92. /*----------------------------- output ------------------------------*/
  93. let outputDev = {
  94. path: pathVars.distPath,
  95. publicPath: "/", // 表示资源的发布地址,当配置过该属性后,打包文件中所有通过相对路径引用的资源都会被配置的路径所替换(如:css中背景图的路径)
  96. filename: "[name]/main.js"
  97. };
  98. config["outputDev"] = outputDev;
  99. let outputDist = {
  100. path: pathVars.distPath,
  101. publicPath: "/",
  102. filename: "[name]_[chunkhash:5].js",
  103. library: "[name]",
  104. libraryTarget: "umd"
  105. // libraryExport: "default"
  106. };
  107. config["outputDist"] = outputDist;
  108. /*----------------------------- externals ------------------------------*/
  109. if (process.env.NODE_ENV === "noThird") {
  110. console.log("==================", process.env.NODE_ENV);
  111. config["externals"] = {
  112. 'three': {
  113. commonjs: 'THREE',
  114. commonjs2: 'THREE',
  115. amd: 'THREE',
  116. root: 'THREE'
  117. },
  118. 'zrender': {
  119. commonjs: 'zrender',
  120. commonjs2: 'zrender',
  121. amd: 'zrender',
  122. root: 'zrender'
  123. },
  124. 'gsap': {
  125. commonjs: 'gsap',
  126. commonjs2: 'gsap',
  127. amd: 'gsap',
  128. root: 'gsap'
  129. }
  130. };
  131. } else {
  132. config["externals"] = {};
  133. config["outputDist"]["filename"]="[name].js"
  134. }
  135. /*----------------------------- module ------------------------------*/
  136. let rules = [
  137. // {
  138. // test: /\.js$/,
  139. // exclude: pathVars.nodeModulesPath,
  140. //
  141. // //use: ['babel-loader'],
  142. // use: ["happypack/loader?id=babel"]
  143. // },
  144. {
  145. test: /\.js$/,
  146. // exclude: pathVars.nodeModulesPath,
  147. loader: "babel-loader",
  148. },
  149. // {
  150. // test: /\.glsl$/,
  151. // loader: 'webpack-glsl-loader'
  152. // },
  153. {
  154. test: /\.(glsl|vs|fs|vert|frag)$/,
  155. // exclude: [pathVars.nodeModulesPath, pathVars.dllPath],
  156. use: ["raw-loader", "glslify-loader"]
  157. }
  158. ]
  159. // let jsonRuleDist={
  160. // test:/\.json$/,
  161. // type: 'javascript/auto',
  162. // use:[
  163. // {
  164. // loader: 'json-loader',
  165. //
  166. // options: {
  167. // publicPath: '../../static/',
  168. // name: 'json/[name]_[hash:5].[ext]'
  169. // }
  170. // }
  171. // ]
  172. // };
  173. let imgRuleDev = {
  174. test: /\.(png|jpg|gif)$/,
  175. // loader: 'url-loader?limit=1000&name=images/[name]_[hash:5].[ext]',
  176. use: [
  177. {
  178. loader: 'file-loader',
  179. options: {
  180. publicPath: '/',
  181. name: 'imgInChart/[name]_[hash:5].[ext]'
  182. }
  183. }
  184. ]
  185. }
  186. let imgRuleDist = {
  187. test: /\.(png|jpg|gif)$/,
  188. // loader: 'url-loader?limit=1000&name=images/[name]_[hash:5].[ext]',
  189. use: [
  190. {
  191. loader: 'file-loader',
  192. options: {
  193. publicPath: '../../static/',
  194. name: 'imgInChart/[name]_[hash:5].[ext]'
  195. }
  196. }
  197. ]
  198. };
  199. config["moduleBaseDev"] = {rules: rules.concat(imgRuleDev)};
  200. config["moduleBaseDist"] = {rules: rules.concat(imgRuleDist)};
  201. //---------------------------------- plugin -----------------------------------//
  202. let pluginBase = [
  203. /*new CopyWebpackPlugin([{
  204. from: pathVars.staticPath,
  205. //to 起始位置为发布目录
  206. to: "static"
  207. }]),*/
  208. new webpack.DllReferencePlugin({
  209. context: pathVars.rootPath,
  210. manifest: require(pathVars.dllPath + '/manifest.json')
  211. }),
  212. new HappyPack({
  213. id: 'babel',
  214. loaders: ["babel-loader"],
  215. threadPool: happyThreadPool
  216. }),
  217. new webpack.DefinePlugin({
  218. my_ENV: JSON.stringify(process.env.my_ENV)
  219. })
  220. ];
  221. const htmlBase = [];
  222. pageAry.forEach(function (page) {
  223. const htmlPlugin = new HtmlWebpackPlugin({
  224. title: '',
  225. template: page["filePath"],
  226. filename: page["htmlName"],
  227. inject: 'body',
  228. chunks: ["manifest", "vendor", "common", page["chunkName"]],
  229. chunksSortMode: 'none'
  230. });
  231. htmlBase.push(htmlPlugin);
  232. });
  233. // pageVars.forEach(function (page) {
  234. // let htmlPlugin = new HtmlWebpackPlugin({
  235. // title: 'page1',
  236. // template: page["filePath"],
  237. // filename: page["htmlName"],
  238. // inject: 'body',
  239. // chunks: /*isDev ? [page["chunkName"]] :*/ ["manifest", "vendor", "common", page["chunkName"]],
  240. // chunksSortMode: 'dependency'
  241. // });
  242. // pluginBase.push(htmlPlugin);
  243. // });
  244. //------------------- dev
  245. let pluginsDev = [
  246. new webpack.HotModuleReplacementPlugin()
  247. ];
  248. config["pluginsDev"] = pluginBase.concat(pluginsDev, htmlBase);
  249. //------------------- dist
  250. let pluginDist = [
  251. new CleanWebpackPlugin(pathVars.distPath,
  252. {
  253. root: pathVars.rootPath,
  254. verbose: true
  255. }
  256. ),
  257. new webpack.NoEmitOnErrorsPlugin(),
  258. ];
  259. config["pluginDist"] = pluginBase.concat(pluginDist);
  260. //----------------------------------- optimization ---------------------------//
  261. config["optimizationDev"] = {
  262. minimize: false,
  263. splitChunks: {
  264. chunks: "all", //initial
  265. minSize: 30 * 1024, //模块大于30k会被抽离到公共模块 也就是说每个页面的js不会大于30k
  266. minChunks: 1, //模块出现1次就会被抽离到公共模块
  267. maxAsyncRequests: 5, //异步模块,一次最多只能被加载5个
  268. maxInitialRequests: 3, //入口模块最多只能加载3个
  269. name: true,
  270. cacheGroups: {
  271. default: {
  272. chunks: "all",
  273. name: "common",
  274. test: /[\\/]src[\\/]/,
  275. minChunks: 3,
  276. priority: -20,
  277. reuseExistingChunk: true
  278. },
  279. vendor: {
  280. chunks: "all",
  281. name: "vendor",
  282. test: /[\\/]node_modules[\\/]/,
  283. minChunks: 1,
  284. priority: -10
  285. }
  286. }
  287. },
  288. runtimeChunk: {
  289. name: "manifest"
  290. }
  291. };
  292. //
  293. config["optimizationDist"] = {
  294. minimize: true,
  295. minimizer: [
  296. new UglifyJsPlugin({
  297. cache: true,
  298. parallel: true,
  299. uglifyOptions: {
  300. warnings: false,
  301. compress: {
  302. // warnings: false,
  303. drop_debugger: true,
  304. drop_console: true
  305. }
  306. }
  307. }),
  308. new OptimizeCssAssetsPlugin({})
  309. ],
  310. splitChunks: {}
  311. };
  312. module.exports = config;