npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

preschool

v0.2.0

Published

Universal preprocessor loader for templating and transpiling

Downloads

7

Readme

preschool

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 preschool

Examples

var compileHandlebars = preschool("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 = preschool("handlebars", { fetch : false });
// => Throws error if handlebars is missing

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

API

preschoolfunction
.defaultEngineForExtension(ext)String

preschool ⇒ 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 options.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, options, next)

| Param | Type | Description | | --- | --- | --- | | name | String | Name of module (npm install name). | | options | Object | Options include {fetch} and {dir} (install directory). |

Example

preschool("typescript")
  // => fn(str, options, next) for typescript

preschool.defaultEngineForExtension(ext) ⇒ String

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

Returns: String - Name of the default engine.

| Param | Type | Description | | --- | --- | --- | | ext | String | The file extension. |

Example

preschool.defaultEngineForExtension("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:

| Param | Type | Description | | --- | --- | --- | | name | String | Name of engine (by convention we use npm package name) | | ext | String | Typical file extension (e.g. handlebars uses hbs) | | type | String | Type of output file (html, css, js) | | modules | Array | Array of required modules | | syntax | String | The function to be evaluated |

Example specification

{
  "name"    : "handlebars",
  "ext"     : "hbs",
  "type"    : "html",
  "modules" : ["consolidate", "handlebars"],
  "syntax"  : "handlebars.render(str, options, 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, options and next have special meanings.

Using Handlebars as an example:

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

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

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

License

MIT