dev.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.245: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.245:6001', // 你的后端服务地址
  23. changeOrigin: true, // 允许跨域
  24. // pathRewrite: {
  25. // '^/dr/api': '' // 可选,用于重写路径
  26. // }
  27. },
  28. },
  29. //server: 'https', // 启用 HTTPS ,为了开发环境测试打开摄像头功能
  30. host: '0.0.0.0', // 监听所有网络接口
  31. open: false, // 可选:是否自动打开浏览器
  32. port: 10086, // 可选:指定端口号
  33. static: {
  34. directory: path.resolve(__dirname, '../public'),
  35. },
  36. },
  37. // Use webpackChain to customize Webpack
  38. // eslint-disable-next-line
  39. webpackChain(chain, webpack) {
  40. // 读取环境变量,告诉webpack在打包时,使用环境变量中定义的值 替换掉代码中的process.env.USE_MSW
  41. // 打印环境变量,检查是否正确读取
  42. console.log('环境变量 process.env.USE_MSW:', process.env.USE_MSW);
  43. console.log('环境变量 process.env.NODE_ENV:', process.env.NODE_ENV);
  44. // 确保使用正确的值,直接从.env文件读取
  45. const useMSW = JSON.stringify(
  46. process.env.USE_MSW === 'true' ? 'true' : 'false'
  47. );
  48. console.log('使用的 useMSW 值:', useMSW);
  49. // 检查 define 插件是否已存在,如果不存在则先创建它
  50. const hasDefinePlugin = chain.plugins.has('define');
  51. if (!hasDefinePlugin) {
  52. chain.plugin('define').use(webpack.DefinePlugin, [{}]);
  53. }
  54. /* 1. 把 Taro 可能注入的所有 react-refresh 插件先删掉 */
  55. chain.plugins.delete('react-refresh') // 3.5 之前
  56. chain.plugins.delete('ReactRefreshWebpackPlugin') // 3.6+ 可能叫这个
  57. /* 2. 重新注册,强制关闭 overlay */
  58. chain
  59. .plugin('ReactRefreshWebpackPlugin') // 名字随意,保证唯一
  60. .use(ReactRefreshWebpackPlugin, [
  61. {
  62. overlay: false, // 👈 关键:关掉灰色蒙层
  63. },
  64. ])
  65. // 然后再修改插件配置
  66. chain.plugin('define').tap((args) => {
  67. // 确保args[0]存在
  68. if (!args[0]) {
  69. args[0] = {};
  70. }
  71. args[0]['process.env.USE_MSW'] = useMSW;
  72. console.log('DefinePlugin配置:', args[0]);
  73. return args;
  74. });
  75. chain.devServer.merge({
  76. setupMiddlewares: (middlewares, devServer) => {
  77. devServer.app.get('/mockServiceWorker.js', (req, res) => {
  78. res.set('Content-Type', 'application/javascript');
  79. res.sendFile(
  80. path.resolve(__dirname, '../public/mockServiceWorker.js')
  81. );
  82. });
  83. return middlewares;
  84. },
  85. });
  86. },
  87. },
  88. } satisfies UserConfigExport<'webpack5'>;