From: nick on
Has anyone here used preprocessing instructions in their js source,
for example to import other files, conditionally include bits of code
based on compile time options, etc?

If so, how did you do it?

I've discovered that using gcc with the -E option seems to work pretty
well. It feels hackish though, I don't totally trust it. Lines
beginning with # and : have to be removed from the output. The benefit
is that pretty much any C(++) preprocessor stuff can be used with
javascript... Is there a cleaner way to do this though?

# preprocess as chromium extension
cp src.js temp.c
echo '/** my chromium extension */' > out.js
gcc -DMY_VERSION=1.23 -DEXT_CHROMIUM=1 -E temp.c \
| sed s/^[#:].*// | jsmin >> out.js
rm temp.c
From: Stefan Weiss on
On 27/04/10 02:53, nick wrote:
> Has anyone here used preprocessing instructions in their js source,
> for example to import other files, conditionally include bits of code
> based on compile time options, etc?
>
> If so, how did you do it?
>
> I've discovered that using gcc with the -E option seems to work pretty
> well. It feels hackish though, I don't totally trust it. Lines
> beginning with # and : have to be removed from the output. The benefit
> is that pretty much any C(++) preprocessor stuff can be used with
> javascript... Is there a cleaner way to do this though?

I've used preprocessor instructions to build JS applications before, but
that turned out to be too inflexible after a while. The next step was a
custom Perl script, which basically replicated some of the CPP
functionality, but accepted directives in a slightly different format,
and allowed leading whitespace:

//#ifdef WITH_REGEX
//#include "regexp.js"
...
//#endif

The JS files remained syntactically valid and could be processed with
other tools, like JSLint. We've since moved on to a Java-based builder
tool which can be more easily integrated with Ant or Maven.

What you might find interesting is that you don't need to run gcc if you
only need the preprocessor (cpp):

$ cat input.js
print("line 1");
#ifdef ALL_LINES
print("line 2");
print("line 3");
#endif
print("line 4");

$ cpp -P input.js
print("line 1");
print("line 4");

$ cpp -P -DALL_LINES input.js
print("line 1");
print("line 2");
print("line 3");
print("line 4");

(some blank lines snipped)


--
stefan
From: nick on
On Apr 26, 9:14 pm, Stefan Weiss <krewech...(a)gmail.com> wrote:

> I've used preprocessor instructions to build JS applications before, but
>  that turned out to be too inflexible after a while. The next step was a
> custom Perl script, which basically replicated some of the CPP
> functionality, but accepted directives in a slightly different format,
> and allowed leading whitespace:

>   //#ifdef WITH_REGEX
>     //#include "regexp.js"
>     ...
>   //#endif

Hmm, it would be pretty easy to add that functionality with another
sed before preprocessing.

> What you might find interesting is that you don't need to run gcc if you
> only need the preprocessor (cpp)

Good to know, it looks like I can pipe the cpp file right through cpp.
Thanks :)
From: nick on

> pipe the cpp file

s/cpp/js/

From: nick on
On Apr 26, 9:38 pm, nick <nick...(a)fastmail.fm> wrote:

> Hmm, it would be pretty easy to add that functionality with another
> sed before preprocessing.

This works:

sed s/^\\s*\\/\\/#/#/