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

kanso-precompiler-base

v0.0.2

Published

A small utility library that takes the boiler plate out of creating precompilers for Kanso

Downloads

4

Readme

This is a small CommonJS module that can be used by packages that precompile files and add them to design documents, such as compiling javascript code, html templates or css stylesheets.

Usage

Install the npm package in your own precompiler package:

npm install kanso-precompiler-base

In your precompiler package code (usually in build/compile) require the precompiler to get access to its methods:

precompiler = require("kanso-precompiler-base")

Example

This example is taken from the Eco template precompiler. It uses a method called compileTemplate to actually compile the Eco templates into JavaScript.

module.exports =
  before: "modules"
  run: (root, path, settings, doc, callback) ->
    console.log "Running eco pre-compiler"
    return callback(null, doc) unless settings["eco"]?["templates"]?

    # Extract the template paths from the settings
    templatePaths = precompiler.normalizePaths(settings["eco"]["templates"], path)

    # Create a continuation that processes a template using the given doc and path
    processTemplate = async.apply(compileTemplate, doc, path)

    # Run processTemplate, asynchronously, on each of the files that match the given pattern, in the given paths 
    precompiler.processPaths(templatePaths, /.*\.j?eco$/i, processTemplate, callback)

Notice also that we pass the callback function that was passed to us into the processPaths function. This is so that Kanso knows when the precompilation is complete but also, more importantly, gives Kanso the doc object to which we have added.

Reference

normalizePaths: (paths, cwd)

Use this method to get a nice array of absolute paths from an entry in the settings object.

  • paths: either a string or an array containing one or more relative paths to the files to be precompiled.
  • cwd: a string containing the current working directory to which the given paths are relative.

processPaths: (paths, pattern, processItem, callback)

Use this function to process each of the files in each of the paths that match the given pattern

  • paths: A normalized array of paths to search for items to process
  • pattern: A regular expression that matches the files to be processed
  • processItem: The function to be executed to actually do the precompilation
  • callback: The function to be called once all the items have breen precompiled - of the form: callback(err, doc)

addModule: (doc, name, originalPath, content)

Use this function to add a CommonJS module to the design document, which can be required in JS code on the server or can be stitched up by the modules package into the modules.js file for use in the browser.

  • doc: The design document where the module will be added
  • name: The name of the module (without extension)
  • originalPath: The original full filename of the module before precompilation
  • content: The content of the precompiled module

addAttachment: (doc, name, originalPath, content)

Use this function to add an item to the design document as an attachment, which can be downloaded for use in the browser.

  • doc: The design document where the attachment will be added
  • name: The name of the attachment
  • originalPath: The original full filename of the attachment before precompilation
  • content: The content of the precompiled attachment