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

smarter-preprocessor

v0.1.0

Published

A smarter preprocessor for Node, using AST-parsing.

Downloads

10

Readme

Smarter Preprocessor

A smarter preprocessor for Node.js.

  • License: MIT
  • Current status: alpha

Smarter Preprocessor allows you to preprocess a Javascript module, to build modified versions of the module, or even load a modified version of the module at run-time (module.preprocessorRequire()).

But instead of adding preprocessor commands that would turn your code into non-Javascript, or hide them behind comment marks, the great idea behind Smarter Preprocessor is that preprocessor commands ARE actual Javascript code, and that that your code would run fine without even preprocessing it, falling back to default behavior.

It is a strong recommandation to keep it operational without preprocessing.

Common use cases:
  • you want a lot of logs in development mode, but you don't even want that the production code had to filter them out with dozens of if statements.

  • your source code uses specific features of an engine, but you have specified an alternative code that is compatible with another.

  • you have some server code that works fine, you want to build a browser-compatible version

Be sure to check the recommendations.

smarter-preprocessor: the CLI program

Smarter Preprocessor can be invoked as a Command Line utility to build an alternative version of your code.

After installing it globally, using npm install -g smarter-preprocessor, we can run it from everywhere.

The syntax is simple:

smarter-preprocessor <source-file> [dest-file] [--switch1 [value1]] [--switch2 [value2]] [...].

If dest is not given, the standard output will be assumed. It's useful if we have to pipe that into another program.

All switches are identifiers we have used in our source-file. See below.

Any alpha-numric string can be used as a switch. Just try to be consistent with other projects.

Some examples:

  • smarter-preprocessor main.js main.debug.js --debug: build the main.debug.js file from main.js, using the debug parameter
  • smarter-preprocessor main.js main.trace.js --loglevel trace: build the main.trace.js file from main.js, setting the loglevel parameter to 'trace'

Require a module and pre-process it on-the-fly

Smarter Preprocessor can require a module while pre-processing it on-the-fly.

module.preprocessorRequire( modulePath , switches , [ options ] )

  • modulePath: string the module file path to load
  • switches: object an object containing the preprocessor switches
  • options: object optional, contains some options, available options are:
    • multi: if the module is required multiple times with different switches objects, multiple instances of the module will be spawned. Without this options, subsequent require will use the first instance even if the switches object has different options. Some node.js module execute code at require-time, that's why the default behaviour is to share only one instance, just like a normal require() does.
var spp = require( 'smarter-preprocessor' ) ;	// Load the smarter preprocessor module

var myModule = module.preprocessorRequire( 'my-module' , { config1: true , config2: 4 } ) ;

The .preprocessorRequire() method is added to the module prototype itself, that way, ISO behavior with vanilla require() is guaranted. Also this method is accessible from files that do not require 'smarter-preprocessor' directly.

Recommendations / Good practices

  • Your source code should be working without any preprocessing. That's what make Preprocessor.js a bad thing, Javascript must run unprocessed, out of the box.
  • In fact, your source code should be your standard / production version
  • Use runtime module.preprocessorRequire() only for development, debugging, or any kind of fail-safe or emergency mode, that's not a good practice to use it for production running in standard mode.