Number 2 (comer pastel) is the one you want. Number 1, though not ungrammatical, is not natural. That's in short. But why?
These two sentences are basically equivalent, as you said:
- A ellos les gusta el pastel.
- A ellos les gusta comer pastel.
The subject (in this unusual construction) goes after the verb. In the first sentence the subject is el pastel. In sentence 2 the subject is comer pastel. In sentence 1, el pastel is a definite noun phrase.
The phrase comer pastel is also (sort of) a definite noun phrase. Inside it there's an infinitive and what in English you'd call a mass noun as its direct object. Pastel in this sense means "cake" in general, as if cake were a substance, although it can of course work as a countable noun (just as English "cake"). But comer pastel is both general and definite as it is, because Spanish infinitives are like that by default.
If you were to use a definite article (comer el pastel) it would sound as if you meant one particular cake that you've already talked about. This is not what you want, because it's not a general statement (it would be right if you said e. g. A ellos les gusta comer el pastel que les hago cada vez que vienen = "They like eating the cake I make for them every time they come").
Things are different if you just want to say les gusta el pastel. You can't say *les gusta pastel because Spanish doesn't work like that; mass nouns in that context do need a definite article.
Note that you can have X + infinitive constructions, where X definite and indefinite articles, demonstratives (ese, aquel), possessives (su, nuestros). At this point the infinitive is already what's called a deverbal noun and it works differently.