# compile-middleware

> Generic Express.js/Connect middleware handling runtime compilization tasks

Latest version **0.2.8** (published 2013-07-25) · Apache License v2.0 license · 0 weekly downloads

## Install

```sh
npm install compile-middleware
pnpm add compile-middleware
yarn add compile-middleware
bun add compile-middleware
```

## 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.2.8 |
| Published | 2013-07-25 |
| First published | 2013-03-05 |
| Weekly downloads | 0 |
| License | Apache License v2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Shinohane |
| Maintainers | shinohane |
| Keywords | middleware, express, connect, compile, less, jade, jade-browser |

## Links

- npm: https://www.npmjs.com/package/compile-middleware
- Repository: https://github.com/shinohane/compile-middleware
- npm.io page: https://npm.io/package/compile-middleware

## Dependencies (1)

- [gaze](https://npm.io/package/gaze.md) *

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.8 (latest) — 2013-07-25
- 0.2.7 — 2013-07-17
- 0.2.6 — 2013-05-07
- 0.2.4 — 2013-05-01
- 0.2.1 — 2013-04-29
- 0.1.2 — 2013-04-15
- 0.1.1 — 2013-03-07
- 0.0.1 — 2013-03-05

## README

Compile
=======

Generic Express.js/Connect middleware handling runtime compilization tasks.

Feature
-------

* Compile source file on requested

* Cache the compiled data for next request

* Watch for source file changes

  Once the source file is changed, cache is invalidated.

* Dependency Supplement interface

  Add extra dependency files to watch

* Support JSONP request

  Support request with `GET /api?callback=?`

Usage
-----

```javascript
var compile = require('compile-middleware');
var midware = {
  less: compile({
    filename  : function (req) {            // Source filename resolve
                  // Function obtaining filename 
                },
    src       : '/path/to/source',          // Path to source file
    render    : function (source_path, cb, depend) {
                  // Function render file 
                },
  }),
  jade: compile({
    filename  : /(?:\/runtime\/)(.*)\.js/i, // Capture group 1 will be used
    src_ext   : '.jade',                    // Optional, Default ''
    src       : '/path/to/source',
    render    : function (source_path, cb) {
                  // Function rendering file
                },
    headers   : {                           // Optional, HTTP Headers
                  'Cache-Control': 'public, max-age=86400',
                  'Content-Type': 'text/javascript' 
                }
  })
};

app.use(midware.less);
app.use(midware.jade);
```

When render function issue an `ENOENT` error. The middleware will invoke the
`next()` function for other middleware to execute. 

_Thus, put this middleware before `express.static` middleware is recommended 
practice._

Render Function Arguments: 

* **source_path**

  path to the file to be rendered, file path be made from `src`, 
  `filename` expression and `src_ext` parameter.

* **cb**

  callback function, invoke `cb(err)` on error, `cb(null, <data>)` on
  success

* **depend** (Optional)

  You can add extra dependency by calling `depend("/path/to/extra/depend")`
  or `depend(["list/of", "extra/depend", "files"])`

  *The change on depended file will also invalidate the compiled cache*

Either a function or Regular Expression is accepted as filename parameter.
When using RegEx, the first capture group will be used as the name of source
file. A suffix to filename can be defined by `src_ext`.

**WARNING** No not add a `g` flag to the RegExp, that will broke the 
filename extraction procedure. It's an
[V8 Issue](https://code.google.com/p/v8/issues/detail?id=778)

Related Works
-------------

* [Compile-Middleware for Jade-Runtime](http://github.com/shinohane/compile-mw-jade-runtime)

* [Compile-Middleware for LESS](http://github.com/shinohane/compile-mw-less)

License
-------

Copyright 2013 Shinohane&lt;imandry.c@gmail.com&gt;

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and 
limitations under the License.

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