The launch of Tesla's first factory in China is likely to come at a difficult time for auto companies in the world's largest passenger-vehicle market.

The electric-car maker is building a factory in Shanghai that it expects to begin producing vehicles by the end of this year.

Having a factory in China will alleviate the shipping costs required to send vehicles there, which could allow Tesla to cut prices and attract more Chinese customers.

But Tesla is increasing its investment in China at a time when auto sales are declining for the first time in almost three decades — and as the Chinese government is cutting subsidies for electric vehicles.

Visit Business Insider's homepage for more stories.

The launch of Tesla's first factory in China is likely to come at a difficult time for auto companies in the world's largest passenger-vehicle market.

The electric-car maker is building a factory in Shanghai that it expects to begin producing vehicles by the end of this year. On Tuesday, CEO Elon Musk tweeted that the company was building a significant engineering team in China focused on software and firmware for both its factory and its vehicles.

Read more: Angry shareholders accused Elon Musk of using Tesla and SpaceX to bail out his cousins' solar company for $2.6 billion

Having a factory in China would alleviate the shipping costs required to send vehicles to the country, which could allow Tesla to cut prices and attract more Chinese customers. But Tesla is increasing its investment in China at a time when auto sales are declining for the first time in almost three decades — and as Chinese government is cutting subsidies for electric vehicles.

In the first half of this year, passenger-vehicle sales in China declined almost 10% compared with the same period last year. And in 2018, the country experienced its first annual auto-sales decrease since 1990.

In a note to investors from June, UBS cited trade issues, tepid economic growth, and a lack of government stimulus as possible reasons for the slowdown.

"China auto sales year-to-date have been much weaker than we anticipated at the beginning of the year," the investment bank said.

The decrease has not been limited to gas-powered vehicles. Following years of explosive growth, electric-vehicle sales declined year-over-year in July and August following subsidy cuts from the Chinese government. After introducing generous incentives for electric and plug-in hybrid vehicles in 2014, China's government plans to phase them out entirely by the end of next year.

See also: Apply here to attend IGNITION: Transportation, an event focused on the future of transportation, in San Francisco on October 22

That puts Tesla in a difficult position. Success in the Chinese market, which is the largest for both gas-powered and electric vehicles, will be essential as the company attempts to grow from a luxury automaker to a mass-market one. And the long-term cost savings made possible by operating factories closer to international customers could help the company, which has posted only four profitable quarters in its 16-year history, move closer to long-term profitability.

But for reasons outside its control, Tesla may end up opening its factory in an unusually challenging economic climate. (Though in the June note, UBS said it expected at least some recovery in the Chinese auto market next year.)

Tesla did not immediately respond to Business Insider's request for comment for this story.

Are you a current or former Tesla employee? Do you have an opinion about what it's like to work there? Contact this reporter at mmatousek@businessinsider.com.