webpack.config.js 4.6 KB

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