The three countries are currently re-negotiating NAFTA, which underpins Mexico's economy — the U.S. accounts for 80 percent of its exports.

The deal comes as Pena Nieto closes a trip to China on trade and investment, and is part of efforts to open new opportunities for Mexico, as President Donald Trump has threatened to kill the $1.2 trillion North American Free Trade Agreement, the foundation of trade for the U.S., Canada and Mexico.

"By partnering with Alibaba, we can expand Mexico's export options in China and in Asia more broadly, while enhancing Mexican [small- and medium-sized enterprises'] knowledge of e-commerce and cross-border trade," said Mexican President Enrique Pena Nieto of the memorandum of understanding inked with Alibaba.

Mexico's Ministry of Economy and Alibaba will partner to get more Mexican products onto the tech firm's popular e-commerce platforms. The move aims to help Mexico's small- and medium-sized enterprises expand internationally and in China — the world's largest consumer market with a growing middle class. Alibaba, in turn, will provide its technological expertise on logistics and payments, and it will share analytics to help Mexican firms market better to the Chinese.

"The idea behind Mexico's current diversification efforts, including going to China, is that, in the event President Donald Trump eventually backs out of NAFTA, we have to be ready to start implementation of plan B, and plan B is China," said Adrian Cisneros Aguilar, a lawyer and director-general of Chevaya, a firm that specializes in helping Mexican firms expand in China.

For a long time, Mexico didn't need to look beyond the U.S. when it came to trade. But on the campaign trail, Trump said he would slap tariffs on both Mexico and China, something that appears to have pushed the two nations closer together. China reiterated during Pena Nieto's trip that it's open to signing a free trade agreement directly with Mexico. Mexican officials have previously echoed similar sentiments.

More trade with Mexico is something consumers will likely welcome, as many in China have developed a taste for avocadoes, beer and tequila. Avocado exports from Mexico to China, for example, have shot up to more than 10,000 metric tons of the green fruit from 17 metric tons back in 2009, according to Mexican government data cited by ChinaAg, a market research firm.

While growing trade with China can offset some of Mexico's reliance on the U.S., there's still a long way to go for replacing the U.S. as the main trade partner — Mexican exports to China are around $7 billion a year, a far cry from the $300 billion in goods the country sends across the border to the U.S.

But experts say there are a number of challenges when it comes to a broader economic relationship between China and Mexico. One detail that complicates further trade ties between the two nations is that both are competing for more business with the U.S. The physical distance is also an issue, as China has no shortage of trading partners closer to home.

On top of that, while a free trade agreement could be a step in the right direction, many Mexican businesses are still inexperienced when it comes to dealing with China's tough business environment.

"Companies that have never mentioned China are approaching me to say they see, suddenly, it's an option," said Cisneros Aguilar.

But success in the world's second-largest usually requires time and effort — what works for Mexican companies in Spain won't translate in China, he explained.

Understanding the environment is very "important when it comes to a country like China, which is so culturally different from us," Cisneros Aguilar said.