自定义组件库-封装第三方图表,如echarts
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

config.js 7.6KB

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