Get text from velocity template action in JavaScript

On front I get text from velocity action and I try to put it in js variable:

<script>
const text = {
#foreach($resource in ${messages.getResourcesWithPrefix('path.to.keys')})
  '$!{resource.getKey()}': '$!{resource.getValue().replace("'", "")}',
#end
}
</script>

The problem arise when the velocity template is loaded and text is in more lines (devided with enter) for example:

console.log(text)
//const text = {
//text.push({'key1': 'value in one line'})
//text.push({'key2': 'value in first line
//value in second line.'})
...

Then js error is thrown:

Uncaught SyntaxError: Invalid or unexpected token (at doc.do)

Is it possible to avoid this error on front with JavaScript or it must be done in the back with Java?