Tuesday night was a massacre for Republicans in Virginia.

Going in, it looked to be a close race for the governor’s seat between Republican Ed Gillespie and Democrat Ralph Northam, with Gillespie even looking like he had the momentum on his side to win it.

Instead, Northam won by a comfortable 9-point margin and his party nearly won control of Virginia’s House of Delegates, a prospect that was thought highly unlikely going into Tuesday.

Why this happened will be the talk for weeks to come.

Democrats are painting it as a major rebuke of Trump and a sign of a coming blue wave in 2018. Trump supporters are seeing it a demonstration of how poorly the GOP establishment connects with voters (Gillespie is a former Republican National Committee chair and lobbyist) and that Trumpism needs to be doubled-down on. Trump skeptics in the GOP are turning the loss into evidence Trumpism is disastrous for the party.

The overwhelming consensus among pundits and journalists is that the election was a referendum on Trumpism — and the president’s movement lost big time.

This was best summarized by University of Virginia political scientist Larry Sabato, who exhorted on Twitter, “Dear Pundit Friends, please stop attributing this D landslide in VA to ‘changing demographics’. VA hasn’t changed that much since last Nov. 8 (Hillary by 5%). The bigger explanation is a backlash to Trump and Trumpism, pure and simple.”

‘PROTESTOR GAMEDAY’ PITS RALPH NORTHAM SUPPORTERS AGAINST ED GILLESPIE’S

While it is true that Virginia’s demographics haven’t changed much from 2016, they have changed significantly from the days when Republicans won statewide office.

Contrary to Sabato’s wish casting, Tuesday night’s slaughter is more of a representation of Virginia’s rapidly changing demographics have made it a solid blue state — regardless of Trump.

In 2017, 67 percent of the electorate was white.

In 2009, when Republican Bob McDonnell won an overwhelming victory over his Democratic opponent, whites composed 78 percent of the electorate.

If the demographics in 2017 matched those of 2009, Gillespie would have barely lost instead of suffering a blowout.

Things have also changed from the last governor’s race that was a much closer match.

When Republican Ken Cuccinelli lost to Democrat Terry McAuliffe by less than 3 percentage points in 2013 for the governor’s seat, the electorate was 72 percent white.

But the demographics in Virginia don’t just hinge on race, they are also heavily determined by class.

The D.C. suburbs of Northern Virginia are filled with young professionals with college degrees who lean Democratic, and their number has grown substantially in recent years. In 2017, NoVa denizens turned out in droves to vote for Northam, giving him the dominating finish he ended up with.

Fewer whites with no college degrees voted in Virginia in 2017 (26 percent) than in 2016 (29 percent), while more whites with college degrees voted in 2017 (41 percent) than in 2016 (38 percent).

Trump won college educated whites in 2016 by 4 percentage points, while Northam won them in 2017 by 3 points. Gillespie’s vote total among whites without college degrees was about equal with Trump’s massive support from this demographic.

The electorate being composed of more Democratic-leaning NoVa professionals and fewer Republican-leaning working-class whites likely explains Northam’s larger win margin than Hillary’s.

The implication behind the argument of Sabato and other analysts is that a Republican could have won if he had been free of the taint of Trumpism. There is no evidence that an anti-Trump Republican running on the Jeff Flake agenda would have done any better than Gillespie, who reluctantly adopted a Trumpian platform during the GOP primary.

For instance, the top concern among Virginia voters was health care, with the group who selected that as their primary issue going strongly for Northam. This is likely an indication these voters are worried about repealing Obamacare and privatization of government benefits. Never Trumpers who insist conservative orthodoxy is more electorally viable than Trumpism are still big on repealing Obamacare and privatizing social security, which means a candidate more to their taste would have lost these voters as well.

Additionally, it is likely that Never Trumpers desire to surrender the culture war and embrace open borders may have possibly dimmed turnout among the GOP base in Virginia.

Voters who rated immigration as their top issue went strongly for Gillespie. A marjority of Virginia voters in 2017 said they opposed taking down Confederate statues.

Both of those factors indicate strong support among the base for Trumpism, but the problem in Virginia is that they are overwhelmed by other groups.

Maybe if Trump had campaigned for Gillespie in counties that he won in 2016, white working-class turnout would have been higher and the race could have been closer.

Instead, Gillespie kept a distance from the president, while Jill Vogel — the Republican candidate for lieutenant governor who fully embraced Trump and his message — performed better than him.

It’s hard to say Tuesday proved Republican critics of Trump right, and it is certainly thick-headed to dismiss how demographics have transformed Virginia politics.

No matter who Republicans run or what the political climate is like, the party will have to reckon that it can only count on 67 percent of whites to turn out to vote in Old Dominion. Maybe they can do better among college-educated whites to get a closer race than the Tuesday’s drubbing, but that still wouldn’t have netted a win. No Republican has yet to figure out a message that can retain the base while winning over significant numbers of non-whites and a strong majority of college-educated whites.

Jeff Flake, the face of GOP opposition to Trump, has certainly not figured that out, as he has an abysmal approval rating in his home state of Arizona despite all the positive press he receives.

The current demographics of Virginia mean it will become a Democratic stronghold for years to come. That’s the message Republicans should take from this week.

That’s a hard thing for a lot of the party to accept, but it’s much better than thinking the GOP must abandon the interests of the one group that overwhelmingly supports it in order to fruitlessly pander to demographics who will never vote red.

Follow Scott on Twitter and buy his new book, “No Campus for White Men.”