# preprocess-loader

> preprocess loader module for webpack

Latest version **0.3.0** (published 2018-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install preprocess-loader
pnpm add preprocess-loader
yarn add preprocess-loader
bun add preprocess-loader
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2018-10-22 |
| First published | 2015-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 94.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Yu Jin |
| Maintainers | dearrrfish |

## Links

- npm: https://www.npmjs.com/package/preprocess-loader
- Repository: https://github.com/dearrrfish/preprocess-loader
- Issues: https://github.com/dearrrfish/preprocess-loader/issues
- npm.io page: https://npm.io/package/preprocess-loader

## Dependencies (2)

- [preprocess](https://npm.io/package/preprocess.md) ^3.0.2
- [loader-utils](https://npm.io/package/loader-utils.md) ^1.0.0

## Recent versions

- 0.3.0 (latest) — 2018-10-22
- 0.2.2 — 2017-05-26
- 0.2.1 — 2017-05-23
- 0.2.0 — 2015-11-04
- 0.1.0 — 2015-05-30

## README

## preprocess-loader for webpack

> [Webpack](https://webpack.github.io) loader uses [preprocess](https://github.com/jsoverson/preprocess) to preprocess HTML, Javascript, and other module files based on custom or environment configurations.
> 
> Inspired by [gulp-preprocess](https://github.com/jas/gulp-preprocess) and [coffee-loader](https://github.com/webpack/coffee-loader). 

## Usage

This loader is used within loader-chain before other loaders doing 'real' job.

``` 
var exports = require('coffee!preprocess?+DEBUG&NODE_ENV=production!./file.coffee')

```

[Webpack Doc: Using Loaders](http://webpack.github.io/docs/using-loaders.html)

#### loadUtils.parseQuery examples

``` 
null                   -> {}
?                      -> {}
?flag                  -> { flag: true }
?+flag                 -> { flag: true }
?-flag                 -> { flag: false }
?xyz=test              -> { xyz: "test" }
?xyz[]=a               -> { xyz: ["a"] }
?flag1&flag2           -> { flag1: true, flag2: true }
?+flag1,-flag2         -> { flag1: true, flag2: false }
?xyz[]=a,xyz[]=b       -> { xyz: ["a", "b"] }
?a%2C%26b=c%2C%26d     -> { "a,&b": "c,&d" }
?{json:5,data:{a:1}}   -> { json: 5, data: { a: 1 } }
```

#### webpack.config file

##### Webpack 2+ (Thanks @scholtzm)
``` javascript
const config = {
  module: {
    loaders: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'preprocess-loader',
            options: {
              foo: true,
              bar: 'baz'
              ppOptions: {
                type: 'js'
              }
            }
          }
        ]
      }
    ]
  }
};
```

##### Webpack 1
``` coffeescript
{
  module: {
    loaders: [{
        test: /\.coffee$/
        loader: 'coffee!preprocess?+DEBUG&NODE_ENV=production'
    }, {
        test: /\.cjsx$/
        loader: 'coffee!cjsx!preprocess?+DEBUG&NODE_ENV=production'
    }, {
        test: /\.test_pp_options$/
        loader: "coffee!cjsx!preprocess?{DEBUG:true,ppOptions:{type:'cjsx'}}"
    }, {
        test: /\.js$/
        loader: 'babel-loader!preprocess?+DEBUG'
    }]
  }
}
```

Loader supports `.cjsx` as an alias type of `.coffee`.

You can override default **preprocess options** by passing `ppOptions` object in query.  See [preprocess API](https://github.com/jsoverson/preprocess#options) for available options.

#### Example HTML

``` html
<head>
  <title>Your App

  <!-- @if NODE_ENV='production' -->
  <script src="some/production/lib/like/analytics.js"></script>
  <!-- @endif -->

</head>
<body>
  <!-- @ifdef DEBUG -->
  <h1>Debugging mode - <!-- @echo RELEASE_TAG --> </h1>
  <!-- @endif -->
  <p>
  <!-- @include welcome_message.txt -->
  </p>
</body>
```

#### Example Javascript

``` javascript
var configValue = '/* @echo FOO */' || 'default value';

// @ifdef DEBUG
someDebuggingCall()
// @endif
```

#### Example Coffeescript

``` coffeescript
configValue = '/* @echo FOO */' or 'default value'
# @ifdef DEBUG
somDebuggingCall()
# @endif
```

`@echo`  block won't be processed in `coffee/shell` type unless applying another preprocess loading after it is compiled to javascript.

``` coffeescript
loader: "preprocess?{ppOptions:{type:'js'}}!coffee!cjsx!preprocess?{DEBUG:true,ppOptions:{type:'cjsx'}}"
```

**More examples can be found in [README of preprocess](https://github.com/jsoverson/preprocess#directive-syntax).**



## Licences

MIT

---
_Source: https://npm.io/package/preprocess-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
