# insert-module-globals

insert implicit module globals
(`__filename`, `__dirname`, `process`, `global`, and `Buffer`)
into a module bundle stream generated by
[module-deps](https://github.com/substack/module-deps)

[![build status](https://secure.travis-ci.org/substack/insert-module-globals.png)](http://travis-ci.org/substack/insert-module-globals)

# example

``` js
var mdeps = require('module-deps');
var bpack = require('browser-pack');
var insert = require('insert-module-globals');

var files = [ __dirname + '/files/main.js' ];
mdeps(files)
    .pipe(insert(files))
    .pipe(bpack({ raw: true }))
    .pipe(process.stdout)
;
```

```
$ node example/insert.js | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}
```

or use the command-line scripts:

```
$ module-deps main.js | insert-module-globals main.js | browser-pack | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}
```

# methods

``` js
var insertGlobals = require('insert-module-globals')
```

## var insert = insertGlobals(files, opts)

Insert globals for an array of entry `files`. The `files` array is used to
rebase the `__filename` and `__dirname` names or you can pass in an
`opts.basedir` explicitly.

You can pass in a custom `opts.resolve(id, parent, cb)` function. By default
[browser-resolve](https://github.com/shtylman/node-browser-resolve) is used for
the `opts.resolve`.

When `opts.always` is truthy, wrap every file with all the global variables
without parsing. This is handy because parsing the scope can take a long time,
so you can prioritize fast builds over saving bytes in the final output.

Return a through stream `insert` that expects input from the format written by
[module-deps](https://github.com/substack/module-deps). The `insert` stream will
output data that can be fed into
[browser-pack](https://github.com/substack/browser-pack).

# usage

```
usage: insert-module-globals [files]
```

# install

With [npm](https://npmjs.org), to get the library do:

```
npm install insert-module-globals
```

and to get the bin script do:

```
npm install -g insert-module-globals
```

# insert custom globals.

`insert-module-globals` can also insert arbitary globals into files.
Pass in an object of functions as the `vars` option.

``` js
var vars = {
    process: function (row, basedir) {
        return {
            id: "path/to/custom_process.js",
            source: customProcessContent
        }
    },
    Buffer: function (row, basedir) {
        return {
            id: "path/to/custom_buffer.js,
            source: customProcessContent,
            //suffix is optional
            //it's used to extract the value from the module.
            //it becomes: require(...).Buffer in this case.
            suffix: '.Buffer'
        }
    },
    Math: function () {
        //if you return a string,
        //it's simply set as the value.
        return '{}'
        //^ any attempt to use Math[x] will throw!
    }
}

mdeps(files)
    .pipe(insert(files, {vars: vars}))
    .pipe(bpack({ raw: true }))
    .pipe(process.stdout)
```


# license

MIT
