Giter Club home page Giter Club logo

preprocessor-loader's Introduction

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

preprocessor-loader's People

Contributors

mageweaver avatar tarunc avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.