自定义组件库-封装第三方图表,如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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. 'echarts': {
  111. commonjs: 'echarts',
  112. commonjs2: 'echarts',
  113. amd: 'echarts',
  114. root: 'echarts'
  115. }
  116. };
  117. } else {
  118. config["externals"] = {};
  119. config["outputDist"]["filename"]="[name].js"
  120. }
  121. let resolve = {
  122. alias: {
  123. utils: path.resolve(__dirname, '../src/utils/'),
  124. }
  125. };
  126. config["resolve"] = resolve;
  127. /*----------------------------- module ------------------------------*/
  128. let rules = [
  129. {
  130. test: /\.js$/,
  131. // exclude: pathVars.nodeModulesPath,
  132. loader: "babel-loader",
  133. },
  134. {
  135. test: /\.(glsl|vs|fs|vert|frag)$/,
  136. // exclude: [pathVars.nodeModulesPath, pathVars.dllPath],
  137. use: ["raw-loader", "glslify-loader"]
  138. }
  139. ]
  140. let imgRuleDev = {
  141. test: /\.(png|jpg|gif)$/,
  142. use: [
  143. {
  144. loader: 'file-loader',
  145. options: {
  146. publicPath: '/',
  147. name: 'imgInChart/[name]_[hash:5].[ext]'
  148. }
  149. }
  150. ]
  151. }
  152. let imgRuleDist = {
  153. test: /\.(png|jpg|gif)$/,
  154. use: [
  155. {
  156. loader: 'file-loader',
  157. options: {
  158. publicPath: '../../static/',
  159. name: 'imgInChart/[name]_[hash:5].[ext]'
  160. }
  161. }
  162. ]
  163. };
  164. config["moduleBaseDev"] = {rules: rules.concat(imgRuleDev)};
  165. config["moduleBaseDist"] = {rules: rules.concat(imgRuleDist)};
  166. //---------------------------------- plugin -----------------------------------//
  167. let pluginBase = [
  168. new webpack.DllReferencePlugin({
  169. context: pathVars.rootPath,
  170. manifest: require(pathVars.dllPath + '/manifest.json')
  171. }),
  172. new HappyPack({
  173. id: 'babel',
  174. loaders: ["babel-loader"],
  175. threadPool: happyThreadPool
  176. }),
  177. new webpack.DefinePlugin({
  178. my_ENV: JSON.stringify(process.env.my_ENV)
  179. })
  180. ];
  181. const htmlBase = [];
  182. pageAry.forEach(function (page) {
  183. const htmlPlugin = new HtmlWebpackPlugin({
  184. title: '',
  185. template: page["filePath"],
  186. filename: page["htmlName"],
  187. inject: 'body',
  188. chunks: ["manifest", "vendor", "common", page["chunkName"]],
  189. chunksSortMode: 'none'
  190. });
  191. htmlBase.push(htmlPlugin);
  192. });
  193. //------------------- dev
  194. let pluginsDev = [
  195. new webpack.HotModuleReplacementPlugin()
  196. ];
  197. config["pluginsDev"] = pluginBase.concat(pluginsDev, htmlBase);
  198. //------------------- dist
  199. let pluginDist = [
  200. new CleanWebpackPlugin(pathVars.distPath,
  201. {
  202. root: pathVars.rootPath,
  203. verbose: true
  204. }
  205. ),
  206. new webpack.NoEmitOnErrorsPlugin(),
  207. ];
  208. config["pluginDist"] = pluginBase.concat(pluginDist);
  209. //----------------------------------- optimization ---------------------------//
  210. config["optimizationDev"] = {
  211. minimize: false,
  212. splitChunks: {
  213. chunks: "all", //initial
  214. minSize: 30 * 1024, //模块大于30k会被抽离到公共模块 也就是说每个页面的js不会大于30k
  215. minChunks: 1, //模块出现1次就会被抽离到公共模块
  216. maxAsyncRequests: 5, //异步模块,一次最多只能被加载5个
  217. maxInitialRequests: 3, //入口模块最多只能加载3个
  218. name: true,
  219. cacheGroups: {
  220. default: {
  221. chunks: "all",
  222. name: "common",
  223. test: /[\\/]src[\\/]/,
  224. minChunks: 3,
  225. priority: -20,
  226. reuseExistingChunk: true
  227. },
  228. vendors: {
  229. chunks: "all",
  230. name: "vendor",
  231. test: /[\\/]node_modules[\\/]/,
  232. minChunks: 1,
  233. priority: -10
  234. }
  235. }
  236. },
  237. runtimeChunk: {
  238. name: "manifest"
  239. }
  240. };
  241. //
  242. config["optimizationDist"] = {
  243. minimize: false,
  244. minimizer: [
  245. new UglifyJsPlugin({
  246. cache: true,
  247. parallel: true,
  248. uglifyOptions: {
  249. warnings: false,
  250. compress: {
  251. // warnings: false,
  252. drop_debugger: true,
  253. drop_console: true
  254. }
  255. }
  256. }),
  257. new OptimizeCssAssetsPlugin({})
  258. ],
  259. splitChunks: {}
  260. };
  261. module.exports = config;