洋鬼子 originated in the late Qing as a slur for Westerners, peaking around the Opium Wars and the Boxer Rebellion (1899-1901), when missionaries and merchants from across the 'great ocean' (
洋) were vilified as 'devils'.
鬼子 alone became a slur for Japanese (
日本鬼子) during the Second Sino-Japanese War.