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

grunt-prepr

v0.1.2

Published

Grunt preprocessor plugin

Downloads

1

Readme

grunt-prepr

Grunt task that provides a C/C++ like preprocessor (with some limitations, see examples) for JavaScript, CSS and other source code. Supported directives are:

#ifdef
#ifndef
#endif
#define
#undef

The task allows to perform both conditional preprocessing of the source code and to define macros.

Getting Started

Setup task grunt-prepr with grunt.js

Install this task next to your project's grunt.js gruntfile with:

npm install grunt-prepr

Then add the line bellow to your project's grunt.js gruntfile:

grunt.loadNpmTasks('grunt-prepr');

How to config

The standard Grunt conventions are followed when configuring task:

grunt.initConfig({
  prepr: {
    //Mask, output directory specified
    target1: {
      defined: ["PROD"],
      src: "in/*.js",
      dest: "."
    },
    //Mask, outputting in the same directory
    target2: {
      defined: ["DEBUG"],
      src: "in/*.js"
    },
    //File mask, JS and CSS, output directory specified
    target3: {
      defined: ["DEBUG"],
      src: "in/*",
      dest: "."
    },
    //Processing single file
    target4: {
      src: "in/valid_styles_with_variables.css",
      dest: "."
    },
    //Processing recursively all JS files
    target5: {
      defined: ["DEBUG"],
      src: "in/**/*.js",
      dest: "."
    },
    //Preserving blank lines when directives have been processed: using option keepLineBreaks
    target6: {
      src: "in/*.js",
      dest: ".",
      keepLineBreaks: true
    }
  }
});

Using as browserify transform

browserify:     {
    options:      {
        transform:  [ require('grunt-prepr').browserify(["DEBUG"]) ],
    }
}

Examples

For more details, refer to the examples in the repository and Jasmine specs.

Although the examples below deal only with JavaScript and CSS, the preprocessor can be used for any source files.

Conditionals

Input:

function add(x, y) {
//#ifdef DEBUG
	console.log("add(" +  x + ", " + y + ")");
//#endif
	return x + y;
}

Task configuration:

grunt.initConfig({
  prepr: {
    dev: {
      defined: ["DEBUG"],
      src: "src/*.js",
      dest: "build"
    },
    prod: {
      defined: ["PROD"],
      src: "src/*.js",
      dest: "dist"
    }
  }
});

Result of running grunt prepr:prod:

function add(x, y) {
	return x + y;
}

Result of running grunt prepr:dev:

function add(x, y) {
	console.log("add(" +  x + ", " + y + ")");
	return x + y;
}

So in the development version logging to console will be left intact while in the production version it will be removed.

Defining macros

With macros we can, for example, define colors in CSS.

Input:

/* #define $COLOR1 rgb(12, 12, 12)
   #define $COLOR2 rgb(23, 45, 67)
   #define $DEFAULT_BOX_WIDTH 300px*/
.container {
    width: $DEFAULT_BOX_WIDTH;
    position: relative;
}

.button {
    background-color: $COLOR1;
}

Output:

.container {
    width: 300px;
    position: relative;
}

.button {
    background-color: rgb(12, 12, 12);
}

Macros can also take parameters, please, refer to the Jasmine specs.

Avoid abusing macros

A word of caution about using macros. The same concerns as in C/C++ apply, the preprocessor is pretty unaware of the structure of the code (unlike Lisp macros). It treats code as strings and modifications then are pretty limited, the source code with preprocessor directives may become invalid if not handled by a preprocessor and moreover the resulting code may also be invalid if the macros were defined incorrectly.

I would say that #define should not be used with JavaScript in most of the cases because of these limitations. Just use the normal functions instead. For example,

instead of:

#define MAX(X, Y) (X > Y ? X : Y)

MAX(3, 4);

use pure JavaScript solution:

function max(x, y) {
    return x > y ? x : y;
}

max(3, 4);

License

MIT License (c) Anton Ivanov

Credits

The following plugins are used during the build:

The task was inspired by: