Skip to content

Commit

Permalink
Optimize RequireJS modules using Almond or AMD Clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Rao committed May 30, 2015
1 parent 0a6f6e0 commit 7329a53
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 22 deletions.
1 change: 0 additions & 1 deletion app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function($, HLJS, Drawer) {
},

init: function(options) {

this.fetch();
this.attach();
this.setup();
Expand Down
22 changes: 2 additions & 20 deletions app/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
(function(window, document, undefined) {
"use strict";

require.config({
paths: {
// Libraries
"jquery": "../../libraries/jquery/jquery",
"hljs": "../../libraries/highlightjs/highlight.pack",
// /Libraries

// Application
"app": "app",
// /Application
},

shim: {
}
});

require(["require-config"], function() {
require(["jquery", "app"],
function( $, App) {
var app = App.create({
Expand All @@ -26,4 +8,4 @@

console.log("App, %s, with jQuery v%s says, '%s'", app.options.name, $.fn.jquery, app.greet());
});
})(window, document);
});
15 changes: 15 additions & 0 deletions app/scripts/require-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require.config({
paths: {
// Libraries
"jquery": "../../libraries/jquery/jquery",
"hljs": "../../libraries/highlightjs/highlight.pack",
// /Libraries

// Application
"app": "app",
// /Application
},

shim: {
}
});
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"jquery": "https://github.com/jquery/jquery.git#1.10.2",
"requirejs": "https://github.com/jrburke/requirejs-bower.git#2.1.17",
"almond": "https://github.com/jrburke/almond.git#0.3.1",
"webfontloader": "https://github.com/typekit/webfontloader.git#1.5.18",
"highlightjs": "https://github.com/components/highlightjs.git#8.5.0"
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"express": "4.12.4"
},
"devDependencies": {
"amdclean": "2.7.0",
"bower": "1.4.1",
"browser-sync": "2.7.1",
"del": "1.1.1",
Expand Down
48 changes: 48 additions & 0 deletions tasks/task-script-optimize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var gulp = require("gulp");
var CFG = require("./utils/config.js");
var $ = require("gulp-load-plugins")();
var path = require("path");
var pkg = require(path.join("..", CFG.FILE.config.pkg));
var notify = require("./utils/notify-script-test-unit");
var rjs = require("requirejs");

/**
* script:optimize
* @see www.npmjs.org/package/requirejs
*/
gulp.task("script:optimize", [/*"script:test"*/], function (callback) {
console.log("__dirname", __dirname);
rjs.optimize({
baseUrl: path.join(CFG.DIR.src, CFG.DIR.script),
mainConfigFile: path.join(CFG.DIR.src, CFG.DIR.script, CFG.FILE.config.scriptMain),
name: "../../libraries/almond/almond.js",
optimize: "none",
uglify2: {
preserveComments: "some",
compress: {
drop_console: true,
dead_code: true,
drop_debugger: true,
join_vars: true,
warnings: false,
pure_funcs: [/* "console.log" */]
}
},
optimizeCss: "none",
findNestedDependencies: true,
include: [
"app"
],
exclude: [
"hljs",
"jquery"
],
out: path.join(CFG.DIR.dist, CFG.DIR.script, CFG.FILE.config.scriptMain),
wrap: {
startFile: "tasks/utils/umd-frag-prelude.js",
endFile: "tasks/utils/umd-frag-coda.js"
}
}, function(buildResponse) {
callback();
}, callback);
});
5 changes: 4 additions & 1 deletion tasks/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
asset: "assets",
util: "utils",
git: ".git",
almond: "almond",

report: "reports",
coverage: "coverage",
Expand All @@ -34,8 +35,10 @@ module.exports = {
scriptLint: ".jshintrc",
compass: ".compassrc",
bower: "bower.json",
scriptMain: "require-config.js",
testMain: "main.js",
karma: ".karmarc"
karma: ".karmarc",
almond: "almond.js"
},
coverageReporter: {
lcov: "lcov.info",
Expand Down
2 changes: 2 additions & 0 deletions tasks/utils/umd-frag-coda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
return require("voxel");
}));
9 changes: 9 additions & 0 deletions tasks/utils/umd-frag-prelude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function(root, factory) {
if (typeof define === "function") {
define(factory);
} else if (typeof exports === "object") {
module.exports = factory();
} else {
root.voxel = factory();
}
}(this, function() {

0 comments on commit 7329a53

Please sign in to comment.