# raptor-optimizer-require

> RaptorJS Optimizer plugin to support Node.js style module require in the browser

Latest version **1.0.4-beta** (published 2014-09-18) · Apache License v2.0 license · 0 weekly downloads

## Install

```sh
npm install raptor-optimizer-require
pnpm add raptor-optimizer-require
yarn add raptor-optimizer-require
bun add raptor-optimizer-require
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4-beta |
| Published | 2014-09-18 |
| First published | 2014-03-04 |
| Weekly downloads | 0 |
| License | Apache License v2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 14 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Patrick Steele-Idem |
| Maintainers | pnidem, philidem |
| Keywords | raptor-optimizer, raptor-optimizer-plugin, require, commonjs, modules |

## Links

- npm: https://www.npmjs.com/package/raptor-optimizer-require
- Repository: https://github.com/raptorjs3/raptor-optimizer-require
- Issues: https://github.com/raptorjs3/raptor-optimizer-require/issues
- npm.io page: https://npm.io/package/raptor-optimizer-require

## Dependencies (14)

- [events](https://npm.io/package/events.md) ^1.0.2
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.0
- [esprima](https://npm.io/package/esprima.md) ^1.0.4
- [process](https://npm.io/package/process.md) ^0.6.0
- [through](https://npm.io/package/through.md) ^2.3.4
- [escodegen](https://npm.io/package/escodegen.md) ^1.3.0
- [estraverse](https://npm.io/package/estraverse.md) ^1.5.0
- [raptor-util](https://npm.io/package/raptor-util.md) ^1.0.0-beta
- [raptor-loader](https://npm.io/package/raptor-loader.md) ^1.0.0-beta
- [raptor-logging](https://npm.io/package/raptor-logging.md) ^1.0.0-beta
- [raptor-modules](https://npm.io/package/raptor-modules.md) ^1.0.0-beta
- [path-browserify](https://npm.io/package/path-browserify.md) 0.0.0
- [raptor-polyfill](https://npm.io/package/raptor-polyfill.md) ^1.0.0-beta
- [raptor-promises](https://npm.io/package/raptor-promises.md) ^1.0.0-beta

## Recent versions

- 1.0.4-beta (latest) — 2014-09-18
- 1.0.3-beta — 2014-09-18
- 1.0.2-beta — 2014-09-12
- 1.0.1-beta — 2014-09-04
- 1.0.0-beta — 2014-09-04
- 0.1.43-beta — 2014-09-03
- 0.1.42-beta — 2014-09-03
- 0.1.41-beta — 2014-09-03
- 0.1.40-beta — 2014-08-29
- 0.1.39-beta — 2014-08-21
- 0.1.38-beta — 2014-08-18
- 0.1.37-beta — 2014-08-11
- 0.1.36-beta — 2014-08-11
- 0.1.35-beta — 2014-08-08
- 0.1.34-beta — 2014-08-04
- … 34 more at https://npm.io/package/raptor-optimizer-require/versions

## README

raptor-optimizer-require
========================

Plugin for the [RaptorJS Optimizer](https://github.com/raptorjs3/raptor-optimizer) that adds support for transporting Node.js-style modules to the browser.

# Installation

This plugin is included as part of the `raptor-optimizer` module so it is not necessary to use `npm install` to add the module to your project. However, if you want to use a specific version of the `raptor-optimizer-require` plugin then you can install it using the following command:

```
npm install raptor-optimizer-require --save
```

# Usage

This plugin is enabled by default, but if you want to provide your own configuration then you can do that using code similar to the following:

```javascript
require('raptor-optimizer').configure({
    plugins: {
        'raptor-optimizer-require': {
            transforms: [ // Browserify compatible transforms
                'deamdify'
            ] // See https://github.com/substack/node-browserify/wiki/list-of-transforms
        }
    }
})
```

The `raptor-optimizer-require` plugin introduces two new dependency types that you can use to target Node.js modules for the browser. There usage is shown in the following `optimizer.json` file:

```json
{
    "dependencies": [
        "require: jquery",
        "require-run: ./main"
    ]
}
```


These new dependency types are described in more detail below.

# Dependency Types

## require

The `require` dependency type will wrap a Node.js module for delivery to the browser and allow it to be required from another module. For example:

__Input modules:__

_foo.js:_
```javascript
exports.add = function(a, b) {
    return a + b;
}
```

_bar.js:_
```javascript
var foo = require('./foo'); 

exports.sayHello = function() {
    console.log('Hello World! 2+2=' + foo.add(2, 2));
};
```

__Output Bundles:__

After running the following command:

```bash
raptor-optimizer require:./foo require:./bar --name test
```

The output written to `static/test.js` will be the following:

```javascript
$rmod.def("/foo", function(require, exports, module, __filename, __dirname) { exports.add = function(a, b) {
    return a + b;
} });
$rmod.def("/bar", function(require, exports, module, __filename, __dirname) { var foo = require('./foo'); 

exports.sayHello = function() {
    console.log('Hello World! 2+2=' + foo.add(2, 2));
}; });
```

__NOTE:__ `$rmod` is a global introduced by the [client-side Node.js module loader](https://github.com/raptorjs3/raptor-modules/blob/master/client/lib/raptor-modules-client.js). It should never be used directly!. The code that declares `$rmod` is not shown in the output above for brevity.

## require-run

In the previous examples, neither the `foo.js` or `bar.js` module will actually run. The `require-run` dependency type should be used to make a module self-executing. This is the equivalent of the entry point for your application when loaded in the browser.

Continuing with the previous example:

__Input modules:__

_foo.js_
(see above)

_bar.js_
(see above)

_main.js:_
```javascript
require('./bar').sayHello();
```

__Output Bundles:__

After running the following command:

```bash
raptor-optimizer require-run:./main --name test
```

Alternatively:
```bash
raptor-optimizer --main main.js --name test
```

The output written to `static/test.js` will be the following:

```javascript
$rmod.def("/foo", function(require, exports, module, __filename, __dirname) { exports.add = function(a, b) {
    return a + b;
} });

$rmod.def("/bar", function(require, exports, module, __filename, __dirname) { var foo = require('./foo'); 

exports.sayHello = function() {
    console.log('Hello World! 2+2=' + foo.add(2, 2));
}; });

$rmod.run("/main", function(require, exports, module, __filename, __dirname) { require('./bar').sayHello(); });
```

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