自定义组件库-封装第三方图表,如echarts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.js 7.6KB

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