Jscex pre-compiler and Its DocPad plug-in

Source: Internet
Author: User
Demand itself will be the best motivation. In addition to busy building the Jscex main site last weekend, I also reorganized Jscex pre-compiler & mdash; or AOT compiler. Jscex comes with a JIT compiler that can be used with eval to avoid additional compilation processes during development. This can also be said to be Jscex bright... SyntaxHighli.

Demand itself will be the best motivation. In addition to busy building the Jscex main site last weekend, I reorganized the Jscex pre-compiler-or AOT compiler. Jscex comes with a JIT compiler that can be used with eval to avoid additional compilation processes during development. This is also one of the highlights of Jscex. However, for the online environment, we generally recommend that you pre-compile the Jscex method, that is, replace the Jscex method definition with the target code. The main advantage of doing so is to reduce the script size during deployment (getting rid of the dependency on the compiler, all code is less than 4 kb ), or make it easy to locate errors in case of exceptions (mainly for Node. js production environment ). In addition, to facilitate document writing, I also developed a plug-in for DocPad to pre-compile Jscex scripts.

Unlike other Jscex packages, the Jscex pre-compiler is not a Jscex module, but an independent tool. Therefore, its first use method is as a command:

> Npm install jscexc
...

> Node node_modules/jscexc
Usage: node./node_modules/jscexc -- input -- Output

Options:
-- Input,-I The input file [required]
-- Output,-o The output file [required]

Missing required arguments: input, output, but it is best to construct this package as a command that can be directly executed, but I still don't know how to make it support Windows and * nix at the same time as mocha, so I haven't implemented it yet. After solving this problem, you can use the jscexc command as follows:

> [Sudo] npm-g install jscexc
...

> Jscexc -- input test. js -- output test. aot. then, the test. js content is converted to test. aot. js. The Jscex method definition is directly replaced with the target code.

This achieves the Effect of Pre-compilation, but the Jscex pre-compiler can also be used as a module. For example, when I compile the Home Page example, I use the plug-in mechanism provided by DocPad to directly pre-compile the *. jscex file and convert it to the final JavaScript code:

# Export Plugin
Module. exports = (BasePlugin)->
# Define Plugin
Class JscexcPlugin extends BasePlugin
Name: "jscexc"

# Render some content
Render: (opts, next)->
# Prepare
{InExtension, outExtension, templateData, file} = opts

# Check extensions
If inExtension in ["jscex"] and outExtension in ["js"]
# Requires www.2cto.com
Jscexc = require ("jscexc ")

# Render
Opts. content = jscexc. compile (opts. content)

# Done, return back to DocPad
Return next ()


Author: zhaojie
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.