dev.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type { UserConfigExport } from '@tarojs/cli';
  2. import path from 'path';
  3. import dotenv from 'dotenv';
  4. import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
  5. // 加载 .env.development 文件中的环境变量
  6. dotenv.config({ path: path.resolve(__dirname, '../.env.development') });
  7. export default {
  8. logger: {
  9. quiet: false,
  10. stats: true,
  11. },
  12. defineConstants: {
  13. MQTT_BROKER_URL_FROM_WEBPACK: '"ws://192.168.110.13:8083/mqtt"',
  14. },
  15. mini: {},
  16. h5: {
  17. sourceMapType: 'eval-source-map',
  18. publicPath: '/',
  19. devServer: {
  20. proxy: {
  21. '/dr': {
  22. target: 'http://192.168.110.85:6001', // 你的后端服务地址
  23. changeOrigin: true, // 允许跨域
  24. // pathRewrite: {
  25. // '^/dr/api': '' // 可选,用于重写路径
  26. // }
  27. },
  28. '/mqtt': {
  29. target: 'ws://192.168.110.85:8083', // MQTT WebSocket 服务地址
  30. changeOrigin: true,
  31. ws: true, // 启用 WebSocket 代理
  32. // pathRewrite: {
  33. // '^/mqtt': '/mqtt' // 保持路径不变,或根据需要调整
  34. // }
  35. },
  36. },
  37. // server: 'https', // 启用 HTTPS ,为了开发环境测试打开摄像头功能
  38. host: '0.0.0.0', // 监听所有网络接口
  39. open: false, // 可选:是否自动打开浏览器
  40. port: 10086, // 可选:指定端口号
  41. static: {
  42. directory: path.resolve(__dirname, '../public'),
  43. },
  44. },
  45. // Use webpackChain to customize Webpack
  46. // eslint-disable-next-line
  47. webpackChain(chain, webpack) {
  48. // 读取环境变量,告诉webpack在打包时,使用环境变量中定义的值 替换掉代码中的process.env.USE_MSW
  49. // 打印环境变量,检查是否正确读取
  50. console.log('环境变量 process.env.USE_MSW:', process.env.USE_MSW);
  51. console.log('环境变量 process.env.NODE_ENV:', process.env.NODE_ENV);
  52. // 确保使用正确的值,直接从.env文件读取
  53. const useMSW = JSON.stringify(
  54. process.env.USE_MSW === 'true' ? 'true' : 'false'
  55. );
  56. console.log('使用的 useMSW 值:', useMSW);
  57. // 检查 define 插件是否已存在,如果不存在则先创建它
  58. const hasDefinePlugin = chain.plugins.has('define');
  59. if (!hasDefinePlugin) {
  60. chain.plugin('define').use(webpack.DefinePlugin, [{}]);
  61. }
  62. /* 1. 把 Taro 可能注入的所有 react-refresh 插件先删掉 */
  63. chain.plugins.delete('react-refresh') // 3.5 之前
  64. chain.plugins.delete('ReactRefreshWebpackPlugin') // 3.6+ 可能叫这个
  65. /* 2. 重新注册,强制关闭 overlay */
  66. chain
  67. .plugin('ReactRefreshWebpackPlugin') // 名字随意,保证唯一
  68. .use(ReactRefreshWebpackPlugin, [
  69. {
  70. overlay: false, // 👈 关键:关掉灰色蒙层
  71. },
  72. ])
  73. // 然后再修改插件配置
  74. chain.plugin('define').tap((args) => {
  75. // 确保args[0]存在
  76. if (!args[0]) {
  77. args[0] = {};
  78. }
  79. args[0]['process.env.USE_MSW'] = useMSW;
  80. console.log('DefinePlugin配置:', args[0]);
  81. return args;
  82. });
  83. chain.devServer.merge({
  84. setupMiddlewares: (middlewares, devServer) => {
  85. devServer.app.get('/mockServiceWorker.js', (req, res) => {
  86. res.set('Content-Type', 'application/javascript');
  87. res.sendFile(
  88. path.resolve(__dirname, '../public/mockServiceWorker.js')
  89. );
  90. });
  91. return middlewares;
  92. },
  93. });
  94. },
  95. },
  96. } satisfies UserConfigExport<'webpack5'>;