智慧城市代码库
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 9.5KB

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