• This repository has been archived on 14/May/2018
  • Stars
    star
    163
  • Rank 231,141 (Top 5 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An AMD (RequireJS) optimizer that's stream-friendly. Made for gulp.

This project is no longer actively maintained. Mostly, because we use Webpack in our projects now. I am happy to review incoming PRs, though. If you would like to become maintainer, please contact me. – @normanrz

amd-optimize Build Status

An AMD (RequireJS) optimizer that's stream-friendly. Made for gulp. (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 support

Example

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 optimizer, but made for a streaming environment like gulp. 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

$ npm install amd-optimize

API

amdOptimize(moduleName, [options])

moduleName

Type: String

options.paths

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

options.map

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

options.shim

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.

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:

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.

// 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.

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)

Same as amdOptimize(), but won't accept an input stream. Instead it will rely on loading the files by itself.

Algorithms

Resolving paths

Finding files

  1. Check the input stream.
  2. Look for files with the default loader and baseUrl.
  3. Look for files with the custom loader and its transform streams.
  4. Give up.

Recommended modules

  • gulp-concat: Concat the output files. Because that's the whole point of module optimization, right?
var concat = require("gulp-concat");

gulp.src("src/scripts/**/*.js")
  .pipe(amdOptimize("index"))
  .pipe(concat("index.js"))
  .pipe(gulp.dest("dist"));
var uglify = require("gulp-uglify");

gulp.src("src/scripts/**/*.js")
  .pipe(amdOptimize("index"))
  .pipe(concat("index.js"))
  .pipe(uglify())
  .pipe(gulp.dest("dist"));
var coffee = require("gulp-coffee");

gulp.src("src/scripts/**/*.coffee")
  .pipe(coffee())
  .pipe(amdOptimize("index"))
  .pipe(concat("index.js"))
  .pipe(gulp.dest("dist"));
  • gulp-if: Conditionally pipe files through a transform stream. Useful for CoffeeScript precompilation.
var gif = require("gulp-if");

gulp.src("src/scripts/**/*.{coffee,js}")
  .pipe(gif(function (file) { return path.extname(file) == ".coffee"; }, coffee()))
  .pipe(amdOptimize("index"))
  .pipe(concat("index.js"))
  .pipe(gulp.dest("dist"));
var eventStream = require("event-stream");
var order = require("gulp-order");

eventStream.merge(
  gulp.src("bower_components/almond/almond.js"),
  gulp.src(amdOptimize("index"))
    .pipe(concat("index.js"))
)
  .pipe(order(["**/almond.js", "**/index.js"]))
  .pipe(concat("index.js"))
  .pipe(gulp.dest("dist"));

Current limitations

Tests

  1. Install npm dev dependencies npm install
  2. Install gulp globally npm install -g gulp
  3. Run gulp test

License

MIT © scalable minds 2014

More Repositories

1

chatroom

React-based Chatroom Component for Rasa Stack
JavaScript
307
star
2

webknossos

Visualize, share and annotate your large 3D images online
TypeScript
124
star
3

gulp-gm

Image manipulation with gm
JavaScript
25
star
4

localcast

Stream your local media to Chromecast
CoffeeScript
24
star
5

invoice-compiler

Generate invoices from YAML files using puppeteer.
HTML
22
star
6

webknossos-libs

Python API and CLI tools for working with WEBKNOSSOS datasets, annotations and server interactions. Includes converter to OME-Zarr.
Python
22
star
7

play-mongev

An evolution plugin for playframework 2 applications using MongoDB
Scala
19
star
8

zarrita

Python
18
star
9

fossildb

Versioned Key-Value Store with RocksDB backend and gRPC API
Scala
13
star
10

time-tracker

Time Tracking for Github issues.
CoffeeScript
13
star
11

sbt-play-raml

Scala
13
star
12

webknossos-wrap

Efficient 3D volume storage format for webKnossos and analyses
Rust
8
star
13

pixijsutil

Pixi.js Scaling Utilities
JavaScript
4
star
14

salt-event-hub

A REST server for firing salt events
Python
4
star
15

cluster_tools

Task distribution with Slurm and multi-processing for Python
Python
3
star
16

toggl-export

Exports toggl.com time entries to scalableminds time tracker.
JavaScript
2
star
17

nojekyll

Static site generator for node.js. Inspired by Jekyll.
CoffeeScript
2
star
18

require-sugar

Syntactic sugar for defining AMD modules
JavaScript
1
star
19

boot

Templates for creating new projects
JavaScript
1
star
20

hubot

CoffeeScript
1
star
21

dockerfiles

Docker images used within scalableminds 🐳
Dockerfile
1
star
22

stroke-lesion-segmentation

Supplementary materials for the publication "Deep learning-based automated lesion segmentation on mouse stroke magnetic resonance images"
Jupyter Notebook
1
star
23

nml-spec

Formal specification of NML files
1
star
24

coffeelint-min-colon-spacing

Validate a minimum spacing left and right of a colon assignment
CoffeeScript
1
star
25

maintenance

Simple maintenance website
HTML
1
star
26

fivepad

A synching notepad with exactly 5 panels.
Java
1
star
27

metal-common

A bunch of custom metalsmith plugins for static site generators
JavaScript
1
star
28

util-fox

Helper to deal with nested Box, Option and Future types
Scala
1
star
29

fluent-canvas

Chainable syntax for HTML5 Canvas. For readable barebone Canvas drawing.
CoffeeScript
1
star