webpack.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const CopyWebpackPlugin = require('copy-webpack-plugin');
  6. const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
  7. const WebpackDevServer = require('webpack-dev-server');
  8. const entries = require('./webpack/entry.config.js');
  9. const version = require('./package.json').version;
  10. const argv = require('yargs').argv;//yargs模块能够解决如何处理命令行参数。yargs模块提供了argv对象,用来读取命令行参数
  11. const dirVars = require('./webpack/dir.config.js');
  12. let theme = {
  13. '@primary-color': '#58a3ff',
  14. '@link-color': '#58a3ff'
  15. };
  16. let isWatch = argv.env.watch == 'watch';
  17. let isDev = isWatch || argv.env.deploy == 'dev';
  18. process.traceDeprecation = true;//process.traceDeprecation 属性被设为 true,则废弃的函数首次被调用时会把警告与堆栈追踪打印到 stderr
  19. process.noDeprecation = true;// process.noDeprecation 属性在首次废弃警告之前被设为 true,则 util.deprecate() 方法什么也不做
  20. module.exports = (function () {
  21. let jumlApi = ''
  22. switch (argv.env.deploy) {
  23. case 'dev':
  24. jumlApi = 'http://sb.jishutao.com'
  25. break
  26. case 'test':
  27. jumlApi = 'http://uat.jishutao.com'
  28. break
  29. case 'prod':
  30. jumlApi = 'http://count.jishutao.com'
  31. break
  32. default:
  33. break
  34. }
  35. let plugins = [
  36. //定义全局变量区分本地测试生产做跳转
  37. new webpack.DefinePlugin({
  38. jumpUrl: `'${jumlApi}'`,
  39. }),
  40. //把入口文件里面的数组打包成verdors.js
  41. new webpack.optimize.CommonsChunkPlugin({
  42. name: 'vendors'
  43. }),
  44. /* 配置好Dll */
  45. new webpack.DllReferencePlugin({
  46. context: dirVars.staticRootDir, // 指定一个路径作为上下文环境,需要与DllPlugin的context参数保持一致,建议统一设置为项目根目录
  47. manifest: require('./dll/'+argv.env.deploy+'/manifest.json') // 指定manifest.json
  48. }),
  49. // 把dll文件复制到打包后的文件中
  50. new CopyWebpackPlugin([
  51. {
  52. from: path.resolve('./dll/'+argv.env.deploy),
  53. to: (__dirname, './dll/'),
  54. ignore: ['.*']
  55. }
  56. ]),
  57. // 将 dll.js 插入HTML里
  58. new HtmlWebpackIncludeAssetsPlugin({
  59. assets: [(__dirname, 'dll/dll.js'),(__dirname, 'dll/dll.css')],
  60. append: false
  61. }),
  62. //使用时被配置的文件,将不再需要import和require进行引入,直接使用即可
  63. new webpack.ProvidePlugin({
  64. $: 'jquery',
  65. React:'react',
  66. Mock:'mockjs'
  67. }),
  68. //extract-text-webpack-plugin该插件的主要是为了抽离css样式,防止将样式打包在js中引起页面样式加载错乱的现象;
  69. new ExtractTextPlugin({
  70. filename: "[name].css",
  71. disable: false,
  72. allChunks: true
  73. }),
  74. //user HtmlWebpack
  75. new HtmlWebpackPlugin({
  76. title: '用户首页',//设置生成的html文件的<title>值
  77. filename: 'user/index.html',//设置子文件夹路径
  78. template: './template/template.html',//顾名思义,是生成文件的模板
  79. chunks: ['user/index', 'vendors']//测试模块
  80. }),
  81. new HtmlWebpackPlugin({
  82. title: '用户登录',
  83. filename: 'user/login.html',
  84. template: './template/template.html',
  85. chunks: ['user/login', 'vendors']
  86. }),
  87. new HtmlWebpackPlugin({
  88. title: '用户注册',
  89. filename: 'user/signIn.html',
  90. template: './template/template.html',
  91. chunks: ['user/signIn', 'vendors']
  92. }),
  93. new HtmlWebpackPlugin({
  94. title: '用户中心管理',
  95. filename: 'user/account/index.html',
  96. template: './template/template.html',
  97. chunks: ['user/account/index', 'vendors']
  98. }),
  99. new HtmlWebpackPlugin({
  100. title: '在线评估',
  101. filename: 'user/account/evaluateInfo.html',
  102. template: './template/template.html',
  103. chunks: ['user/account/evaluateInfo', 'vendors']
  104. }),
  105. //管理员模块
  106. new HtmlWebpackPlugin({
  107. title: '管理员-登录',
  108. filename: 'admin/login.html',
  109. template: './template/template.html',
  110. chunks: ['admin/login', 'vendors']
  111. }),
  112. new HtmlWebpackPlugin({
  113. title: '管理员-首页',
  114. filename: 'admin/index.html',
  115. template: './template/template.html',
  116. chunks: ['admin/index', 'vendors']
  117. })
  118. ];
  119. if (!isDev) {
  120. //这个使用uglifyJs压缩你的js代码
  121. plugins.unshift(new webpack.DefinePlugin({
  122. "process.env": {
  123. NODE_ENV: JSON.stringify("production")
  124. }
  125. }));
  126. plugins.unshift(new webpack.optimize.UglifyJsPlugin({
  127. minimize: true,
  128. compress: {
  129. warnings: true
  130. }
  131. }));
  132. }
  133. let staticHost = 'http://172.16.0.74:80';
  134. switch (argv.env.deploy) {
  135. case 'test':
  136. staticHost = 'http://static.jishutao.com'
  137. break
  138. case 'prod':
  139. staticHost = 'http://count.jishutao.com'
  140. break
  141. default:
  142. break
  143. }
  144. staticHost = staticHost + '/client/' + version + '/';
  145. theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"'
  146. return {
  147. entry: isWatch ? entries.watch : entries.prod,
  148. output: {
  149. path: path.resolve(__dirname, './build/' + argv.env.deploy+'/'+ version),
  150. filename: '[name].js',
  151. publicPath: staticHost,
  152. chunkFilename: 'chunks/[name].[hash:8].js'
  153. },
  154. module: require('./webpack/module.config.js')(theme),
  155. resolve: {
  156. alias:{
  157. '@':__dirname+'/js/component',
  158. 'img':__dirname+'/image',
  159. 'css':__dirname+'/css',
  160. 'js':__dirname+'/js'
  161. },
  162. extensions: ['.js', '.jsx']
  163. },
  164. plugins: plugins,
  165. devServer: {
  166. disableHostCheck: true,
  167. host: '172.16.0.74',
  168. port: 80,
  169. allowedHosts: ['127.0.0.1','192.168.0.20','192.168.0.99'],
  170. headers: {
  171. "Access-Control-Allow-Origin": "*"
  172. }
  173. }
  174. };
  175. })();