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

preprocessor-loader

v1.0.5

Published

Preprocessor loader for webpack. Allows for regular expressions, callbacks, macros, similiar to the C preprocessor

Downloads

508

Readme

To install npm install preprocessor-loader preprocess-loader was takened already =(.

The preprocess-loader is a loader I developed to support the Webpack Module Bundler project. The preprocess-loader provides the ability to preprocess source files through user defined regular expressions, macros, and callback routines. Additionally it has the ability out of the box to replace common tags like LINE and FILE with line number and filename. Furthermore, all user defined logic can be applied to line scope or source scope.

To simply use the LINE or FILE feature you can either specify it in the configuration file below or by simply adding values to the query string.

Below is an example of using the preprocess-loader and chaining the output to babel-loader.

     {
        test: /\.jsx?$/,
        loaders: ["babel","preprocessor?line&file&config="+path.join(__dirname,'./cfg/preprocess.json')],
        exclude: /node_modules/
      },

All parameters are optional in the query string. As previously mentioned you may optionally specify the line or file values in the configuration file.

A sample configuration file may be found below, notice it is simply json.

{
  "line"        : true,
  "file"        : true,
  "regexes"      : [
    {
      "fileName"        : "all",
      "scope"           : "line",
      "regex"           : "__FOO__",
      "flags"           : "g",
      "value"           : "FOO"
    }
  ],
  "macros"      : [
    {
      "fileName"        : "all",
      "scope"           : "line",
      "name"            : "MYMACRO(x,b)",
      "flags"           : "g",
      "inline"          : "function doSomething(x, b) {console.log(x+b);}"
    }
  ],
  "callbacks"   : [
    {
      "fileName": "all",
      "scope"   : "line",
      "callback": "(function fooA(line, fileName, lineNumber) { line = line.replace(new RegExp('TEST'),'HELLO','g'); return line; })"
    },
    {
      "fileName": "all",
      "scope"   : "source",
      "callback": "(function fooB(source, fileName) { console.log('FILENAME is:'+fileName); return source; })"
    }
  ]
}

##regexes are regular expressions

  1. You may specify a specific filename or "all" if you would like it to process on all files.
  2. scope must be "line" or "source".
  3. regex is the regular expression you want to use.
  4. flags are the flags provide to a typical string.replace function or used in a regex
  5. value is the value you would like to replace the regex with

##macros are like regular expressions (They operate in the same way) Except they allow for macros to be expanded inline in the code like the C preprocessor.

  1. You may specify a specific filename or "all" if you would like it to process on all files.
  2. scope must be "line" or "source".
  3. name is the regular expression you want to use to search and replace the macro tag.
  4. flags are the flags provide to a typical string.replace function or used in a regex
  5. inline is the value you would like to replace the macro with.

##callbacks are user defined functions

  1. You may specify a specific filename or "all" if you would like it to process on all files.
  2. scope must be "line" or "source".
  3. callback is the method you supply that will be called. For line scope three paramters are provided:
  4. line, which is the source code line
  5. fileName, which is the filename being processed
  6. lineNumber which is the current line number
  7. For source scope
  8. source, is the code source file
  9. fileName, which is the filename being processed

ALL CALLBACKS MUST RETURN THE LINE OR SOURCE WHEN PROCESSING IS COMPLETE