Forum Feature requests

Add --define command-line flag

alecov
I would like to suggest adding a
--define=property=val
command-line option, which is useful for communicating build environment information to the document, for further processing with JavaScript.

For example, receive current version information directly from Git, or conditionally rendering material (much like an #ifdef). There are countless possibilities.

Command-line definitions could be accessed via the `Prince` object or perhaps some object embedded in `window`.

Example:

# command-line:
# --define=rev=$(git describe --always)

# html:
<footer><div id=rev>rev. </div></footer>

# js:
const rev = document.getElementById("rev");
const version = document.createElement("span");
version.textContent = Prince.defines.rev;
rev.appendChild(version);


Similar approaches:

- Give access to environment variables in JS (similar to Node.JS `process.env`, could be `Prince.env`), making the whole thing trivial;
- Accept a `key=value` file ("environment file") with the same semantics, maybe `--env-file`.

I am currently emulating this behavior by producing a JSON file in compile-time with required build time definitions and loading it via `XMLHttpRequest` with a local webserver (since XHR doesn't support local file loads), which is very cumbersome.

What do you think about this?

Thanks!
mikeday
How about just applying a script with the --script option?
alecov
Hi Mike,

It works, but it's kinda cumbersome because the use case for this usually involves using a build tool (such as `make`), which then needs to produce an actual valid JS script before processing with `prince`... whereas it could be simply passing `--define` or `--env` accordingly.

There seems to be little risk in Prince allowing access to local context (such as environment variables) during processing -- it would also immediately work cross-platform too :)

Thanks
mikeday
Right, it would still require constructing JSON, but at least would avoid the need to use XMLHttpRequest with a local webserver to load the file.