Caramelize Onions: In a large pot or Dutch oven, heat butter and olive oil over medium heat. Add onions, sugar, salt, and pepper. Cook, stirring frequently, for 30–40 minutes until onions are golden brown and caramelized.
Add Garlic and Flour: Stir in garlic and cook for 1 minute. Sprinkle flour over onions and cook for another 2–3 minutes to remove raw flour taste.
Add Liquids and Herbs: Slowly stir in wine (if using), scraping up browned bits from the bottom of the pot. Add broth, bay leaf, and thyme. Bring to a boil, then reduce heat and simmer uncovered for 20–30 minutes.
Prepare Bread: While soup simmers, preheat oven to 400°F (200°C). Arrange baguette slices on a baking sheet and toast until lightly golden, about 5–7 minutes.
Assemble and Broil: Preheat the broiler. Ladle soup into oven-safe bowls or crocks. Top each bowl with a toasted baguette slice and a generous amount of shredded Gruyère. Broil until cheese is melted and bubbly, about 2–3 minutes.
Serve: Carefully remove from oven and serve hot.