webpack.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. //管理员模块
  86. new HtmlWebpackPlugin({
  87. title: '管理员-登录',
  88. filename: 'admin/login.html',
  89. template: './template/template.html',
  90. chunks: ['admin/login', 'vendors']
  91. }),
  92. new HtmlWebpackPlugin({
  93. title: '管理员-首页',
  94. filename: 'admin/index.html',
  95. template: './template/template.html',
  96. chunks: ['admin/index', 'vendors']
  97. })
  98. ];
  99. if (!isDev) {
  100. //这个使用uglifyJs压缩你的js代码
  101. plugins.unshift(new webpack.DefinePlugin({
  102. "process.env": {
  103. NODE_ENV: JSON.stringify("production")
  104. }
  105. }));
  106. plugins.unshift(new webpack.optimize.UglifyJsPlugin({
  107. minimize: true,
  108. compress: {
  109. warnings: true
  110. }
  111. }));
  112. }
  113. let staticHost = 'http://127.0.0.1';
  114. switch (argv.env.deploy) {
  115. case 'test':
  116. staticHost = 'http://statics.jishutao.com';
  117. break;
  118. case 'prod':
  119. staticHost = 'http://ss.jishutao.com';
  120. break;
  121. default:
  122. break;
  123. }
  124. staticHost = staticHost + '/client/' + version + '/';
  125. theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"'
  126. return {
  127. //devtool:"source-map",
  128. entry: isWatch ? entries.watch : entries.prod,
  129. output: {
  130. path: path.resolve(__dirname, './build/' + argv.env.deploy + '/client/' + version),
  131. filename: '[name].js',
  132. publicPath: staticHost,
  133. chunkFilename: 'chunks/[name].[hash:8].js'
  134. },
  135. module: require('./webpack/module.config.js')(theme),
  136. resolve: {
  137. alias:{
  138. '@':__dirname+'/js/component',
  139. 'img':__dirname+'/image',
  140. '@css':__dirname+'css',
  141. 'js':__dirname+'js'
  142. },
  143. extensions: ['.js', '.jsx']
  144. },
  145. plugins: plugins,
  146. devServer: {
  147. disableHostCheck: true,
  148. allowedHosts: ['aft.hnzhiming.com', 'afts.hnzhiming.com'],
  149. headers: {
  150. "Access-Control-Allow-Origin": "*"
  151. }
  152. }
  153. };
  154. })();