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.0KB

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