Skip to content

Documentation of how pug runtime and compileClient work #3093

Open
@PythonLinks

Description

@PythonLinks

Pug Version: your version number here
I am reading the current version of this repository.

Additional Comments

First thank you for all of this great free software.
I am editing PUG with the ace editor, and generating the Javascript in the browser.
That gives the user instant feedback on whether their edits are legal or not.
This is for an application for those who do not know how to use the
Unix Shell, Git, emacs, and Node.js and want to
develop completely in the browser.

Next I want to run a function to generate the HTML.
While the instructions as to what to do are quite clear,
what is going on under the hood is not at all clear.

First comment. It took me a while to even find this page. The main PUG github page does not provide clear instructions for those who want to generate stuff in the browser. In fact it is discouraged. Fine to discourage, but please leave some breadcrumbs so that those of us who wish to follow, can follow. Specifically I could not find an architectural document to tell me
what to look for. The API docs are brilliant, but I need more than that.

Second Comment. It is not at all clear what is going on here. One guy said if you do not understand an api, ask the developers what constraints they were operating under for this design. I would have expected

//First I could generate the javascript

javascriptSrc = pug.compile ( pugSrc, options)

//Then I could eval it.

evan (javascriptSrc)

But that is not the case. There is all the complexity described on this page on how to do it.
What is going on here? What is the motivation for this api design?
Rather than just a how-to, could you give an explanation, some reasoning.
An architecture.

So what is pug_escape(e)?
I would have expected the function to be called pug_render(). Why the word
escape? What is being escaped?

Take this line for example.

assert(runtime.attr('foo', 'bar', true, true) === ' foo="bar"');

I know what assert is.
I know what runtime is.
What is attr?
What are the 4 arguments?
Why should this hold true?
What is going on here?

// By default compileClient automatically embeds the needed runtime functions,
// rendering this module useless.

Which module is useless? Is the following command useless? If it is useless, how to
I compile it for the browser, without the runtime included?

And finally:

When testing code compiled for the browser in Node.js, it is necessary to make the runtime available. To do >so, one can use require('pug-runtime/wrap'):

I think you mean "When generating Javascript code to be run in the browser, it is necessary to make the runtime available..."

Rather than just complaining, let me try to contribute. I will describe what the wrap function does.

The Pug CompileClient Function

When I ran Pug Compile, it generated 3 functions.
pug_escape, pug_rethrow and template.
The template function is the one that generates your html.

The wrap function

Pug generates javascript, but that javascript cannot run on its own. It needs
access to the PUG runtime. The wrap function 'binds' the runtime to the Pug generted javascript.
The wrap function takes two arguments. The first argument is the generated javascript.
The second argument is the optional name of the function to be created. If there is
no second argument, the new function is called 'template'.
You can see the code here:
https://github.com/pugjs/pug/blob/master/packages/pug-runtime/wrap.js

So how do I summarize? The documentation for the PUG api is excellent.
I could not find any documentation of the architecture of PUG. How does it work under the hood. If I am doing mainstream stuff, I just need the api docs. If I am trying to do something
innovative, I need a bit more information than is provide.

Hope that helps.
Thanks for all of the great free software.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions