2.0.0 • Published 3 years ago

compilers v2.0.0

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

compilers

codecov.io

This package is not supported and is only used for a personal project.

Given a request for a preprocessor/transpiler/templating engine/postprocessor, spits back a function of the form function(err, compiled).

The specifications are highly extensible. Initial specs support wide range of functions (from Typescript to ES2015 to minification and cleaning). Pull requests and tests welcome! See instructions on writing a specification below.

Installation

$ npm install compilers

Examples

var compileHandlebars = compilers("handlebars");

compileHandlebars("<div>Hello, {{ name }}!</div>", { name : "Mickey" }, function(err, compiled) {
  console.log(compiled);
  // <div>Hello, Mickey!</div>
});

// Don't "npm install" missing modules (defaults to installing)
var compileHandlebars = compilers("handlebars", { fetch : false });
// => Throws error if handlebars is missing

// "npm install" to different directory
var compileHandlebars = compilers("handlebars", { dir : process.cwd() });

API

compilersfunction
.defaultCompilerForExtension(ext)String

compilers ⇒ function

Loads a processor (templating, transpiler, minification), and standardizes callback to be fn(err, compiled). Defaults to npm install packages if they are missing. To disable, set context.fetch to false;

Most packages are referenced by their npm name, common exceptions include: javascript, html, css, es2015, and react.

Note that the callback is node style (err, compiled).

Returns: function - Processor of format fn(str, context, next)

ParamTypeDescription
nameStringName of module (npm install name).
contextObjectOptions include {fetch} and {dir} (install directory).

Example

compilers("typescript")
  // => fn(str, context, next) for typescript

compilers.defaultCompilerForExtension(ext) ⇒ String

Returns the default compiler for an extension. Leading "." is removed.

Returns: String - Name of the default compiler.

ParamTypeDescription
extStringThe file extension.

Example

compilers.defaultCompilerForExtension("ts");
  // => "typescript"

Specifications

Specifications make use of JavaScript's eval. While using eval is typically considered to be bad form, when calling another function the performance does not appear suffer in V8. See http://jsperf.com/eval-function-call.

A specification consists of the following:

ParamTypeDescription
nameStringName of compiler (by convention we use npm package name)
extStringTypical file extension (e.g. handlebars uses hbs)
typeStringType of output file (html, css, js)
modulesArrayArray of required modules
syntaxStringThe function to be evaluated
optionsObjectOptional options to be accessed via options
suffixStringOptional suffix to call on results (sync or async)

Example specification

{
  "name"    : "handlebars",
  "ext"     : "hbs",
  "type"    : "html",
  "modules" : ["consolidate", "handlebars"],
  "syntax"  : "handlebars.render(str, context, next)"
}

Looking up a specification by extension

Specifications is processed as an array. The first specification with ext is assumed to be the default for extension ext. This is why css, html and js are located at the top of the specification.

Writing a specification

The syntax processor does the following: 1. Prepends $0 to the string if string doesn't contain $0.
2. Replaces all $x with modules[x]. This allows incorporate of modules array.
3. Creates function that evaluates command. Note that str, context and next have special meanings.

Using Handlebars as an example:

// Input
"handlebars.render(str, context, next)"

// Interim
modules[0].handlebars.render(str, context, next)

// Equivalent function
function(str, context, next) { consolidate.handlebars.render(str, context, next); }

License

MIT

2.0.0

3 years ago

1.2.0

7 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.0

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago