I'm writing a simple Javascript library that makes use of some WebGL code. I'd like to include the shader sources inline in the .js file, because my alternatives are to include them as script tags in each page, or to have them as separate files which are loaded as AJAX. Neither of these options are particularly modular. However, due to the lack of multi-line strings in javascript, I don't have any good ideas for how to inline the WebGL code. Is there an approach I'm not thinking of?
Asked
Active
Viewed 1,906 times
4 Answers
4
JavaScript has had multiline strings in all browsers except IE since about 2009.
var shader = `
code
goes
here
`;

gman
- 100,619
- 31
- 269
- 393
3
Use a single string per line and then join them together, e.g.
var shader = [
"// line1 ",
"// line2 ",
].join('\n');
P.S. The general problem was discussed here before, see Creating multiline strings in JavaScript

Community
- 1
- 1

Stefan Haustein
- 18,427
- 3
- 36
- 51
-
+1, This is how I do all of my inlined shaders. Nicest way I've found yet. – Toji Jun 19 '12 at 20:26
1
I ended up hacking this: http://github.com/noteed/language-glsl/ into a code compactor,
by replacing all instances of vcat
with hsep
in Language.GLSL.Pretty. I get a one-line version of the shader code I have in a file, that I can then just paste into a string. I was hoping to find a similar solution already done when I posted this.

Edward
- 1,786
- 1
- 15
- 33
0
This is the way NetBeans handle the case:
var shader =
"firstLine\n\
secondLine\n\
thirdLine";
I found this way more efficient for editing than having to create an array item for each line.

Flavien Volken
- 19,196
- 12
- 100
- 133