webpack.config.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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;
  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;
  19. process.noDeprecation = true;
  20. module.exports = (function () {
  21. let plugins = [
  22. //把入口文件里面的数组打包成verdors.js
  23. new webpack.optimize.CommonsChunkPlugin({
  24. name: 'vendors'
  25. }),
  26. /* 配置好Dll */
  27. new webpack.DllReferencePlugin({
  28. context: dirVars.staticRootDir, // 指定一个路径作为上下文环境,需要与DllPlugin的context参数保持一致,建议统一设置为项目根目录
  29. manifest: require('./dll/'+argv.env.deploy+'/manifest.json') // 指定manifest.json
  30. }),
  31. // 把dll文件复制到打包后的文件中
  32. new CopyWebpackPlugin([
  33. {
  34. from: path.resolve('./dll/'+argv.env.deploy),
  35. to: (__dirname, './dll/'),
  36. ignore: ['.*']
  37. }
  38. ]),
  39. // 将 dll.js 插入HTML里
  40. new HtmlWebpackIncludeAssetsPlugin({
  41. assets: [(__dirname, 'dll/dll.js'),(__dirname, 'dll/dll.css')],
  42. append: false
  43. }),
  44. new webpack.ProvidePlugin({
  45. $: 'jquery',
  46. React:'react',
  47. Mock:'mockjs'
  48. }),
  49. //定义全局常量
  50. // new webpack.DefinePlugin({
  51. // "sub": {
  52. // "name":JSON.stringify("liting")
  53. // }
  54. // }),
  55. new ExtractTextPlugin({
  56. filename: "[name].css",
  57. disable: false,
  58. allChunks: true
  59. }),
  60. //user
  61. new HtmlWebpackPlugin({
  62. title: '用户首页',
  63. filename: 'user/index.html',
  64. template: './template/template.html',
  65. chunks: ['user/index', 'vendors']
  66. }),
  67. new HtmlWebpackPlugin({
  68. title: '用户登录',
  69. filename: 'user/login.html',
  70. template: './template/template.html',
  71. chunks: ['user/login', 'vendors']
  72. }),
  73. new HtmlWebpackPlugin({
  74. title: '用户注册',
  75. filename: 'user/signIn.html',
  76. template: './template/template.html',
  77. chunks: ['user/signIn', 'vendors']
  78. }),
  79. new HtmlWebpackPlugin({
  80. title: '用户中心管理',
  81. filename: 'user/account/index.html',
  82. template: './template/template.html',
  83. chunks: ['user/account/index', 'vendors']
  84. }),
  85. new HtmlWebpackPlugin({
  86. title: '在线评估',
  87. filename: 'user/account/evaluateInfo.html',
  88. template: './template/template.html',
  89. chunks: ['user/account/evaluateInfo', 'vendors']
  90. }),
  91. //管理员模块
  92. new HtmlWebpackPlugin({
  93. title: '管理员-登录',
  94. filename: 'admin/login.html',
  95. template: './template/template.html',
  96. chunks: ['admin/login', 'vendors']
  97. }),
  98. new HtmlWebpackPlugin({
  99. title: '管理员-首页',
  100. filename: 'admin/index.html',
  101. template: './template/template.html',
  102. chunks: ['admin/index', 'vendors']
  103. })
  104. ];
  105. if (!isDev) {
  106. //这个使用uglifyJs压缩你的js代码
  107. plugins.unshift(new webpack.DefinePlugin({
  108. "process.env": {
  109. NODE_ENV: JSON.stringify("production")
  110. }
  111. }));
  112. plugins.unshift(new webpack.optimize.UglifyJsPlugin({
  113. minimize: true,
  114. compress: {
  115. warnings: true
  116. }
  117. }));
  118. }
  119. let staticHost = 'http://172.16.0.253:80';
  120. switch (argv.env.deploy) {
  121. case 'test':
  122. staticHost = 'http://statics.jishutao.com';
  123. break;
  124. case 'prod':
  125. staticHost = 'http://ss.jishutao.com';
  126. break;
  127. default:
  128. break;
  129. }
  130. staticHost = staticHost + '/client/' + version + '/';
  131. theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"'
  132. return {
  133. //devtool:"source-map",
  134. entry: isWatch ? entries.watch : entries.prod,
  135. output: {
  136. path: path.resolve(__dirname, './build/' + argv.env.deploy+'/'+ version),
  137. filename: '[name].js',
  138. publicPath: staticHost,
  139. chunkFilename: 'chunks/[name].[hash:8].js'
  140. },
  141. module: require('./webpack/module.config.js')(theme),
  142. resolve: {
  143. alias:{
  144. '@':__dirname+'/js/component',
  145. 'img':__dirname+'/image',
  146. 'css':__dirname+'/css',
  147. 'js':__dirname+'/js'
  148. },
  149. extensions: ['.js', '.jsx']
  150. },
  151. plugins: plugins,
  152. devServer: {
  153. disableHostCheck: true,
  154. host: '172.16.0.253',
  155. port: 80,
  156. allowedHosts: ['127.0.0.1','192.168.0.20','192.168.0.99','192.168.0.17'],
  157. headers: {
  158. "Access-Control-Allow-Origin": "*"
  159. }
  160. }
  161. };
  162. })();