webpack.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 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://bm.jishutao.com'
  31. break
  32. default:
  33. break
  34. }
  35. let plugins = [
  36. //把入口文件里面的数组打包成verdors.js
  37. new webpack.optimize.CommonsChunkPlugin({
  38. name: 'vendors',
  39. }),
  40. /* 配置好Dll */
  41. new webpack.DllReferencePlugin({
  42. context: dirVars.staticRootDir, // 指定一个路径作为上下文环境,需要与DllPlugin的context参数保持一致,建议统一设置为项目根目录
  43. manifest: require('./dll/' + argv.env.deploy + '/manifest.json'), // 指定manifest.json
  44. }),
  45. // 把dll文件复制到打包后的文件中
  46. new CopyWebpackPlugin([
  47. {
  48. from: path.resolve('./dll/' + argv.env.deploy),
  49. to: (__dirname, './dll/'),
  50. ignore: ['.*'],
  51. },
  52. ]),
  53. // 将 dll.js 插入HTML里
  54. new HtmlWebpackIncludeAssetsPlugin({
  55. assets: [(__dirname, 'dll/dll.js'), (__dirname, 'dll/dll.css')],
  56. append: false,
  57. }),
  58. new ExtractTextPlugin({
  59. filename: '[name].css',
  60. disable: false,
  61. allChunks: true,
  62. }),
  63. //定义全局变量区分本地测试生产做跳转
  64. new webpack.DefinePlugin({
  65. jumpUrl: `'${jumlApi}'`,
  66. }),
  67. //admin
  68. new HtmlWebpackPlugin({
  69. title: '管理员-首页',
  70. filename: 'admin/index.html',
  71. template: './template/template.html',
  72. chunks: ['admin/index', 'vendors'],
  73. }),
  74. new HtmlWebpackPlugin({
  75. title: '管理员-登录',
  76. filename: 'admin/login.html',
  77. template: './template/template.html',
  78. chunks: ['admin/login', 'vendors'],
  79. }),
  80. ]
  81. if (!isDev) {
  82. //这个使用uglifyJs压缩你的js代码
  83. plugins.unshift(
  84. new webpack.DefinePlugin({
  85. 'process.env': {
  86. NODE_ENV: JSON.stringify('production'),
  87. },
  88. })
  89. )
  90. plugins.unshift(
  91. new webpack.optimize.UglifyJsPlugin({
  92. minimize: true,
  93. compress: {
  94. warnings: true,
  95. },
  96. })
  97. )
  98. }
  99. let staticHost = 'http://172.16.1.187:80'
  100. switch (argv.env.deploy) {
  101. case 'test':
  102. staticHost = '//static.jishutao.com'
  103. break
  104. case 'prod':
  105. staticHost = '//s.jishutao.com'
  106. break
  107. default:
  108. break
  109. }
  110. staticHost = staticHost + '/RD/' + version + '/'
  111. theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"'
  112. return {
  113. //devtool:"source-map",
  114. entry: isWatch ? entries.watch : entries.prod,
  115. output: {
  116. path: path.resolve(
  117. __dirname,
  118. './build/' + argv.env.deploy + '/' + version
  119. ),
  120. filename: '[name].js',
  121. publicPath: staticHost,
  122. chunkFilename: 'chunks/[name].[hash:8].js',
  123. },
  124. module: require('./webpack/module.config.js')(theme),
  125. resolve: {
  126. alias: {
  127. '@': __dirname + '/js/component',
  128. },
  129. extensions: ['.js', '.jsx'],
  130. },
  131. plugins: plugins,
  132. // devServer: {
  133. // disableHostCheck: true,
  134. // allowedHosts: ['bm.kedexinxi.com'],
  135. // headers: {
  136. // "Access-Control-Allow-Origin": "*"
  137. // }
  138. // }
  139. devServer: {
  140. disableHostCheck: true,
  141. host: '172.16.1.187',
  142. port: 80,
  143. allowedHosts: ['127.0.0.1', '192.168.0.20', '192.168.0.99'],
  144. headers: {
  145. 'Access-Control-Allow-Origin': '*',
  146. },
  147. },
  148. }
  149. })()