<commit_before># amd-optimize [![Build Status](https://drone.io/github.com/scalableminds/amd-optimize/status.png)](https://drone.io/github.com/scalableminds/amd-optimize/latest)

> An AMD ([RequireJS](http://requirejs.org/)) optimizer that's stream-friendly. Made for [gulp](http://gulpjs.com/). (WIP)

# Features

* Trace all dependencies of an AMD module
* Stream-friendly: Pipe in files and get an ordered stream of files out. No need for writing on disk in between.
* Support for precompilation of source files (ie. CoffeeScript)
* Wraps non-AMD dependencies
* Supply a custom loader for on-demand loading
* Leaves concatenation and minification to your preferred choice of modules
* [gulp-sourcemaps](https://www.npmjs.org/package/gulp-sourcemaps) support

# Example

```js
var gulp = require("gulp");
var amdOptimize = require("amd-optimize");
var concat = require('gulp-concat');

gulp.task("scripts:index", function () {

  return gulp.src("src/scripts/**/*.js")
    // Traces all modules and outputs them in the correct order.
    .pipe(amdOptimize("main"))
    .pipe(concat("index.js"))
    .pipe(gulp.dest("dist/scripts"));

});
```

# Motivation
This aims to be an alternative to the powerful [r.js](https://github.com/jrburke/r.js) optimizer, but made for a streaming environment like [gulp](http://gulpjs.com/). This implementation doesn't operate on the file system directly. So, there's no need for complicated setups when dealing with precompiled files. Also, this module only focuses on tracing modules and does not intend replace a full-fletched build system. Therefore, there might be tons of use cases where r.js is a better fit.


# Installation

```bash
$ npm install amd-optimize
```


## API

### amdOptimize(moduleName, [options])

#### moduleName
Type: `String`

#### options.paths

```js
paths : {
  "backbone" : "../bower_components/backbone/backbone",
  "jquery" : "../bower_components/jquery/jquery"
}
```

#### options.map

```js
map : {
  // Replace underscore with lodash for the backbone module
  "backbone" : {
    "underscore" : "lodash"
  }
}
```

#### options.shim

```js
shim : {
  // Shimmed export. Specify the variable name that is being exported.
  "three" : {
     exports : "THREE"
  },

  // Shimmed dependecies and export
  "three.color" : {
     deps : ["three"],
     exports : "THREE.ColorConverter"
  },

  // Shimmed dependencies
  "bootstrap" : ["jquery"]
}
```

#### options.configFile
Type: `Stream` or `String`

Supply a filepath (can be a glob) or a gulp stream to your config file that lists all your paths, shims and maps.

```js
amdOptimize.src("index", {
  configFile : "src/scripts/require_config.js"
});

amdOptimize.src("index", {
  configFile : gulp.src("src/scripts/require_config.coffee").pipe(coffee())
});
```

#### options.findNestedDependencies
Type: `Boolean`  
Default: `false`


If `true` it will trace `require()` dependencies inside of top-level `require()` or `define()` calls. Usually, these nested dependencies are considered dynamic or runtime calls, so it's disabled by default.

Would trace both `router` and `controllers/home`:

```js
define("router", [], function () {
  return {
    "/home" : function () {
      require(["controllers/home"]);
    },
    ...
  }
})
```

#### options.baseUrl
#### options.exclude
#### options.include

#### options.wrapShim

Type: `Boolean`  
Default: `false`

If `true` all files that you have declared a shim for and don't have a proper `define()` call will be wrapped in a `define()` call.



```js
// Original
var test = "Test";

// Output
define("test", [], function () {
  var test = "Test";
  return test;
});

// Shim config
shim : {
  test : {
    exports : "test"
  }
}
```

#### options.preserveFiles

Type: `Boolean`  
Default: `false`

If `true` all files that you combine will not be altered from the source, should be used for inline sourcemaps to resemble the original source file, good for debugging. A good code minifier or uglify will remove comments and strip new lines anyway.


#### options.loader
WIP. Subject to change.

```js
amdOptimize.src(
  "index",
  loader : amdOptimize.loader(
    // Used for turning a moduleName into a filepath glob.
    function (moduleName) { return "src/scripts/" + moduleName + ".coffee" },
    // Returns a transform stream.
    function () { return coffee(); }
  )
)
```

### amdOptimize.src(moduleName, options)<commit_msg>Shouldn't have touched the readme!<commit_after># amd-optimize [![Build Status](https://drone.io/github.com/scalableminds/amd-optimize/status.png)](https://drone.io/github.com/scalableminds/amd-optimize/latest)

> An AMD ([RequireJS](http://requirejs.org/)) optimizer that's stream-friendly. Made for [gulp](http://gulpjs.com/). (WIP)

# Features

* Trace all dependencies of an AMD module
* Stream-friendly: Pipe in files and get an ordered stream of files out. No need for writing on disk in between.
* Support for precompilation of source files (ie. CoffeeScript)
* Wraps non-AMD dependencies
* Supply a custom loader for on-demand loading
* Leaves concatenation and minification to your preferred choice of modules
* [gulp-sourcemaps](https://www.npmjs.org/package/gulp-sourcemaps) support

# Example

```js
var gulp = require("gulp");
var amdOptimize = require("amd-optimize");
var concat = require('gulp-concat');

gulp.task("scripts:index", function () {

  return gulp.src("src/scripts/**/*.js")
    // Traces all modules and outputs them in the correct order.
    .pipe(amdOptimize("main"))
    .pipe(concat("index.js"))
    .pipe(gulp.dest("dist/scripts"));

});
```

# Motivation
This aims to be an alternative to the powerful [r.js](https://github.com/jrburke/r.js) optimizer, but made for a streaming environment like [gulp](http://gulpjs.com/). This implementation doesn't operate on the file system directly. So, there's no need for complicated setups when dealing with precompiled files. Also, this module only focuses on tracing modules and does not intend replace a full-fletched build system. Therefore, there might be tons of use cases where r.js is a better fit.


# Installation

```bash
$ npm install amd-optimize
```


## API

### amdOptimize(moduleName, [options])

#### moduleName
Type: `String`

#### options.paths

```js
paths : {
  "backbone" : "../bower_components/backbone/backbone",
  "jquery" : "../bower_components/jquery/jquery"
}
```

#### options.map

```js
map : {
  // Replace underscore with lodash for the backbone module
  "backbone" : {
    "underscore" : "lodash"
  }
}
```

#### options.shim

```js
shim : {
  // Shimmed export. Specify the variable name that is being exported.
  "three" : {
     exports : "THREE"
  },

  // Shimmed dependecies and export
  "three.color" : {
     deps : ["three"],
     exports : "THREE.ColorConverter"
  },

  // Shimmed dependencies
  "bootstrap" : ["jquery"]
}
```

#### options.configFile
Type: `Stream` or `String`

Supply a filepath (can be a glob) or a gulp stream to your config file that lists all your paths, shims and maps.

```js
amdOptimize.src("index", {
  configFile : "src/scripts/require_config.js"
});

amdOptimize.src("index", {
  configFile : gulp.src("src/scripts/require_config.coffee").pipe(coffee())
});
```

#### options.findNestedDependencies
Type: `Boolean`  
Default: `false`


If `true` it will trace `require()` dependencies inside of top-level `require()` or `define()` calls. Usually, these nested dependencies are considered dynamic or runtime calls, so it's disabled by default.

Would trace both `router` and `controllers/home`:

```js
define("router", [], function () {
  return {
    "/home" : function () {
      require(["controllers/home"]);
    },
    ...
  }
})
```

#### options.baseUrl
#### options.exclude
#### options.include

#### options.wrapShim

Type: `Boolean`  
Default: `false`

If `true` all files that you have declared a shim for and don't have a proper `define()` call will be wrapped in a `define()` call.



```js
// Original
var test = "Test";

// Output
define("test", [], function () {
  var test = "Test";
  return test;
});

// Shim config
shim : {
  test : {
    exports : "test"
  }
}
```

#### options.preserveFiles

Type: `Boolean`  
Default: `false`

If `true` all files that you combine will not be altered from the source, should be used for outputted files to match the original source file, good for debugging and inline sourcemaps. A good code minifier or uglify will remove comments and strip new lines anyway.


#### options.loader
WIP. Subject to change.

```js
amdOptimize.src(
  "index",
  loader : amdOptimize.loader(
    // Used for turning a moduleName into a filepath glob.
    function (moduleName) { return "src/scripts/" + moduleName + ".coffee" },
    // Returns a transform stream.
    function () { return coffee(); }
  )
)
```

### amdOptimize.src(moduleName, options)
{'fraction_non_alphanumeric': 0.14457013574660635, 'fraction_numerical': 0.0, 'mean_word_length': 3.8845303867403316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1102587', 'n_tokens_mistral': 2861, 'n_tokens_neox': 2709, 'n_words': 904}