Some answers brought up how the word was coined, so I went to the Oxford English Dictionary. As it turns out, the entry for Chinaman has not been updated yet, and Google Books was if anything more informative.
Of the words for “inhabitant of China”, Chinese has been in use since the 17th century (with the plural Chineses). Chinese is an Italian word, and Italian missionaries were the most prominent Europeans to have had early contact with China.
Chinaman shows up in the 18th century. The first meaning attested in the OED is “someone who sells china, i.e. porcelain”; the earliest instance I can see is from 1746: The gentleman’s magazine. The word used with reference to Chinese people first shows up in Google Books in a history of the English East India Company, from 1759: An Universal History, from the Earliest Account of Time.
(Oh, Lee Ballentine? “Chinaman’s chance” shows up in OED too. “Colloquial, now derogatory”.)
Sng Kok Joon Leonard suspects that Chinaman is simply a calque into English of the Chinese Zhongguo ren. It could be, either as mocking of Chinese grammar (as some posters suspect), or as simply a more English-like name than the Romance Chinese. (Cf. Frenchman, Dutchman; *Chin-ese-man combines two suffixes, so it wouldn’t work.) My own suspicion is that once chinaman was coined referring to porcelain-salesmen, the transfer to inhabitants of China would have been irresistible anyway.
At any rate, yes, the early usages of Chinaman were written by English colonialists and orientalists, in the lead up to or during the 100 years of humiliation. But I don’t buy it that they were meant to be derogatory. The next instance is in a 1779 Malabar–English dictionary: A Dictionary of the English and Malabar Languages; I don’t see why you would bother to be derogatory in that context.
The 1872 instance, which is the first the OED cites, is if anything trying to show an empathetic picture of China: The Foreigner in Far Cathay. As the modern blurb puts it, “The author is determined to give a picture of the country and its inhabitants that is realistic and free of the tired clichés often found in contemporary Western accounts of the country. […] Concerned that the West should show China the respect it deserves, he attempts especially to capture the essence of the Chinese character.”
The book mostly uses Chinese, but occasionally uses Chinaman. The instance OED quotes is:
It has been observed that drunkenness is not a Chinese failing: on the contrary, I am happy to be able to bear witness that John Chinaman is a most temperate creature.
Sounds condescending? Are you sure? Creature does not mean beast, after all. And why is he calling his exemplar “John Chinaman”? Because he’s invoking John Bull. He’s giving Mr Average Chinese the same name he’d give Mr Average Briton. That may be patronising, but it is not vituperative.
Like others said: Chinaman was not born a racist name. Chinaman became a racist name, because it picked up those connotations in the West, particularly with Australian and American panic over Chinese migration. And Chinaman was susceptible to picking up those connotations, because it was a newer, less common, and likely more colloquial word than Chinese.
It picked up the racism of its speakers; like any word might have. It’s irredeemable now.
It makes me think of the closest equivalent word in Greek. The Modern Greek word for Turkish woman is turkala. It sounds derogatory to me. But it sounds derogatory because Greeks have traditionally hated Turks, and it’s grammatically odd (the only instance of that suffix in the language). And there is no good word to replace it, unlike Chinaman and Chinese (turkissa is outnumbered by turkala 100:1; tourkida remains a hypothetical form). People on forums online wonder what form they should use instead of turkala. But as they get more friendly and familiar with Turks (including Turkish women), I suspect, the derogatory tone of turkala will likely just go away.