Compact X-ray free-electron laser based on an optical undulator