webpack.dev.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * @Author: liyxt
  3. * @Date: 2019-09-26 09:50:38
  4. * @LastEditors: liyxt
  5. * @LastEditTime: 2019-10-30 17:28:12
  6. * @Description: file content
  7. */
  8. /**
  9. * 开发环境配置
  10. */
  11. const OpenBrowserPlugin = require('open-browser-webpack-plugin');
  12. const CopyWebpackPlugin = require('copy-webpack-plugin');
  13. const webpack = require('webpack');
  14. const path = require('path');
  15. const common = require('./webpack.common');
  16. const merge = require('webpack-merge');
  17. const configJSON = require('../config.json');
  18. const buildEntry = require('./buildEntry');
  19. const port = configJSON.devPort || 3006;
  20. const host = 'localhost';
  21. const fs = require('fs');
  22. module.exports = function(env, argv) {
  23. let { mode, buildPath, client = 'pc' } = env;
  24. if (client === 'mobile') {
  25. process.env.PROJECT_CLIENT = 'mobile';
  26. }
  27. buildPath = buildPath || configJSON.buildEntryPath || './src/*/*/*/*/index.js';
  28. let buildWithoutHTML = configJSON.buildWithoutHTML;
  29. buildWithoutHTML && typeof buildWithoutHTML === 'string' && (buildWithoutHTML = [buildWithoutHTML]);
  30. let devConfig = {
  31. mode,
  32. entry: {},
  33. output: {
  34. filename: '[name].js',
  35. path: path.resolve(__dirname, './dist'),
  36. publicPath: '/',
  37. library: '[name]',
  38. libraryTarget: 'umd',
  39. chunkFilename: '[name].js'
  40. },
  41. devtool: 'source-map',
  42. devServer: {
  43. contentBase: path.join(__dirname, '../src'),
  44. port, // 端口号
  45. host: '0.0.0.0', // 主机地址
  46. inline: false, // 控制台是否显示构建信息
  47. clientLogLevel: 'error', // 控制台显示什么log信息
  48. open: false, // 开始构建时是否打开浏览器,使用OpenBrowserPlugin在构建完成时打开浏览器
  49. hot: true, // 是否启用热替换
  50. lazy: false, // 是否请求时才编译包
  51. historyApiFallback: {
  52. // 404时的页面
  53. rewrites: { from: /./, to: '/404.html' }
  54. },
  55. overlay: {
  56. // 报错时浏览器是否显示错误信息
  57. warnings: true,
  58. errors: true
  59. },
  60. stats: 'errors-only', // 开启报错提示
  61. proxy: {
  62. // 请求代理
  63. '/nccloud': {
  64. target: configJSON.proxy
  65. }
  66. }
  67. },
  68. plugins: [
  69. new webpack.DefinePlugin({
  70. NODE_ENV: JSON.stringify(mode),
  71. ISMA: configJSON.isMA,
  72. LOGIN_INFO: JSON.stringify(configJSON.directConnectInfo),
  73. MA_INFO: JSON.stringify(configJSON.maInfo)
  74. }),
  75. new webpack.NamedModulesPlugin(), // 当开启 HMR 的时候使用该插件会显示模块的相对路径
  76. new webpack.HotModuleReplacementPlugin(), // 模块热替换插件
  77. new OpenBrowserPlugin({ url: `http://${host}:${port}/nccloud` }) // 构建完成打开浏览器插件
  78. ]
  79. };
  80. // 二开相关
  81. let extendBuildEntryPath = configJSON.extendBuildEntryPath || [];
  82. let { entries: extendEntries, plugins: extendPlugins } = buildEntry({
  83. buildPath: extendBuildEntryPath,
  84. buildWithoutHTML,
  85. hash: false,
  86. mode,
  87. client,
  88. fse: true
  89. });
  90. // console.log(extendEntries);
  91. // let { entries = [], plugins = [], externals = [] } = {};
  92. let { entries, plugins, externals } = buildEntry({
  93. buildPath,
  94. buildWithoutHTML,
  95. hash: true,
  96. mode,
  97. client
  98. });
  99. // 合并 二开
  100. Object.assign(entries, extendEntries);
  101. Object.assign(common.externals, externals);
  102. Object.assign(devConfig.entry, entries);
  103. devConfig.plugins.push(...plugins);
  104. devConfig = merge(common, devConfig);
  105. return devConfig;
  106. };