webpack.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const {join, resolve} = require('path');
  2. const webpack = require('webpack');
  3. const glob = require('glob');
  4. const vuxLoader = require('vux-loader');
  5. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  6. const HtmlWebpackPlugin = require('html-webpack-plugin');
  7. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  8. const packagejson = require('./package.json');
  9. // 频道(undefined/debug)
  10. let channel = undefined;
  11. let entries = {
  12. vendor: ["@antv/f2","qrcode","vue","vux"]
  13. // vendor: Object.keys(packagejson.dependencies)
  14. };
  15. if (process.env.NODE_ENV === 'development') {
  16. // channel = 'debug'
  17. }
  18. let chunks = [];
  19. let winMap = {};
  20. getEntriesAndChunks();
  21. let config = {
  22. entry: entries,
  23. output: {
  24. path: resolve(__dirname, './dist'),
  25. filename: '[name].js',
  26. publicPath: process.env.NODE_ENV === 'production' ? undefined : '/'
  27. },
  28. resolve: {
  29. //配置别名,在项目中可缩减引用路径
  30. extensions: ['.js', '.vue'],
  31. alias: {
  32. assets: join(__dirname, '/src/assets'),
  33. components: join(__dirname, '/src/components'),
  34. public: join(__dirname, '/src/public'),
  35. root: join(__dirname, 'node_modules')
  36. }
  37. },
  38. externals: [
  39. {api: "window.api"}
  40. ],
  41. module: {
  42. rules: [
  43. {
  44. test: /\.vue$/,
  45. loader: 'vue-loader'
  46. },
  47. {
  48. test: /\.js$/,
  49. use: 'babel-loader',
  50. exclude: /node_modules/
  51. },
  52. {
  53. test: /\.(less|css)$/,
  54. use: ['style-loader', 'css-loader', 'less-loader'],
  55. },
  56. {
  57. test: /\.html$/,
  58. use: [{
  59. loader: 'html-loader',
  60. options: {
  61. root: resolve(__dirname, 'src'),
  62. attrs: ['img:src', 'link:href']
  63. }
  64. }]
  65. },
  66. {
  67. test: /\.(png|jpe?g|gif|svg|svgz)(\?.+)?$/,
  68. exclude: /iconfont\.svg/,
  69. use: [{
  70. loader: 'url-loader',
  71. options: {
  72. limit: 10000,
  73. name: 'assets/img/[name].[ext]'
  74. }
  75. }]
  76. },
  77. {
  78. test: /\.(eot|woff|woff2|svg|ttf)([?]?.*)$/,
  79. use: 'file-loader'
  80. },
  81. {
  82. test: /muse-ui.src.*?js$/,
  83. use: 'babel-loader'
  84. }
  85. ]
  86. },
  87. plugins: [
  88. // new BundleAnalyzerPlugin(),
  89. new webpack.optimize.CommonsChunkPlugin({
  90. name: ["vendor", 'runtime'],
  91. filename: '[name].js',
  92. // minChunks: Infinity
  93. }),
  94. // new webpack.optimize.CommonsChunkPlugin({
  95. // children: true,
  96. // async: 'children-async'
  97. // }),
  98. new ExtractTextPlugin({
  99. filename: '[name].css',
  100. allChunks: true
  101. }),
  102. new webpack.DefinePlugin({
  103. "webpackConfigParams": JSON.stringify({
  104. winMap: winMap,
  105. channel: channel
  106. })
  107. })
  108. ],
  109. devServer: {
  110. host: '192.168.31.103',
  111. port: 8400,
  112. historyApiFallback: false,
  113. noInfo: true,
  114. proxy: {
  115. '/api': {
  116. target: 'http://127.0.0.1:8400',
  117. changeOrigin: true,
  118. pathRewrite: {'^/api': ''}
  119. }
  120. },
  121. },
  122. devtool: '#eval-source-map'
  123. };
  124. const pages = getHtmls();
  125. pages.forEach(function (pathname) {
  126. // filename 用文件夹名字
  127. let firstSlashIndex = pathname.indexOf('/');
  128. let lastSlashIndex = pathname.lastIndexOf("/");
  129. let fileBasename = pathname.substring(firstSlashIndex + 1, lastSlashIndex);
  130. var conf = {
  131. filename: fileBasename + '.html', //生成的html存放路径,相对于path
  132. template: 'src/' + pathname + '.html', //html模板路径
  133. };
  134. var chunk = fileBasename;
  135. if (chunks.indexOf(chunk) > -1) {
  136. conf.inject = 'body';
  137. conf.chunks = ['vendor', 'runtime', chunk];
  138. }
  139. if (process.env.NODE_ENV === 'production') {
  140. conf.hash = true;
  141. }
  142. config.plugins.push(new HtmlWebpackPlugin(conf));
  143. });
  144. module.exports = vuxLoader.merge(config, {
  145. plugins: [
  146. {
  147. name: 'vux-ui'
  148. },
  149. {
  150. name: 'duplicate-style'
  151. },
  152. {
  153. name: 'less-theme',
  154. path: 'src/assets/css/vux-style-variable.less'
  155. }
  156. ]
  157. });
  158. function getPackRange(ext) {
  159. let result = [];
  160. if (channel === 'debug') {
  161. result = result.concat(glob.sync("./src/pages/html/testdemo/*" + ext));
  162. result = result.concat(glob.sync("./src/pages/html/main/*" + ext));
  163. result = result.concat(glob.sync("./src/pages/html/login/*" + ext));
  164. result = result.concat(glob.sync("./src/pages/html/sctask-finish/*" + ext));
  165. result = result.concat(glob.sync("./src/pages/html/selection/**/*" + ext));
  166. result = result.concat(glob.sync("./src/pages/html/wrkgrp-baditem/**/*" + ext));
  167. } else {
  168. result = result.concat(glob.sync("./src/pages/**/*"+ext));
  169. }
  170. return result;
  171. }
  172. function getEntriesAndChunks() {
  173. getPackRange('.js').forEach(function (name) {
  174. let lastSlashIndex = name.lastIndexOf("/");
  175. var n = name.slice(name.lastIndexOf('src/') + 10, lastSlashIndex);
  176. entries[n] = [name];
  177. var winName = n.slice(n.lastIndexOf('/') + 1, n.length);
  178. if (!winMap[winName]) {
  179. winMap[winName] = n;
  180. }
  181. else {
  182. throw "页面名称重复:" + winName;
  183. }
  184. chunks.push(n);
  185. });
  186. }
  187. function getHtmls() {
  188. var htmls = [];
  189. getPackRange('.html').forEach(function (name) {
  190. var n = name.slice(name.lastIndexOf('src/') + 4, name.length - 5);
  191. htmls.push(n);
  192. });
  193. return htmls;
  194. }
  195. if (process.env.NODE_ENV === 'production') {
  196. module.exports.devtool = false;
  197. module.exports.plugins = (module.exports.plugins || []).concat([
  198. new webpack.DefinePlugin({
  199. 'process.env': {
  200. NODE_ENV: '"production"'
  201. }
  202. }),
  203. new webpack.optimize.UglifyJsPlugin({
  204. compress: {
  205. warnings: false
  206. }
  207. })
  208. ]);
  209. }