Skip to content

Commit

Permalink
Add Grunt minify task, adapt some source code to support minify
Browse files Browse the repository at this point in the history
  • Loading branch information
llaske committed Oct 18, 2016
1 parent 89b1ba4 commit 7905031
Show file tree
Hide file tree
Showing 33 changed files with 333 additions and 373 deletions.
54 changes: 54 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
uglify: {
options: {
banner: '/*! Sugarizer <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
dynamic_mappings: {
expand: true,
src: [
'lib/*.js',
'js/*.js',
'activities/Abecedarium.Activity/**/*.js',
'activities/Calculate.Activity/**/*.js',
'activities/ChatPrototype.Activity/**/*.js',
'activities/Clock.Activity/**/*.js',
'activities/Cordova.Activity/**/*.js',
'activities/Etoys.Activity/**/*.js',
'activities/FoodChain.Activity/**/*.js',
'activities/Gears.Activity/js/*.js', // TODO: lib/gearsketch dont work
'activities/GetThingsDone.Activity/**/*.js',
'activities/Gridpaint.Activity/**/*.js',
'activities/LabyrinthJS.Activity/**/*.js',
'activities/LastOneLoses.Activity/**/*.js',
'activities/Markdown.Activity/**/*.js',
'activities/MazeWeb.Activity/**/*.js',
'activities/MediaViewer.Activity/**/*.js',
'activities/Memorize.Activity/**/*.js',
'activities/Moon.Activity/**/*.js',
'activities/Paint.Activity/**/*.js',
'activities/PhysicsJS.Activity/**/*.js',
'activities/Record.Activity/lib/*.js', // TODO: js/recordrtc.js don't work
'activities/SharedNotes.Activity/**/*.js',
'activities/Speak.Activity/**/*.js',
'activities/Stopwatch.Activity/**/*.js',
'activities/TamTamMicro.Activity/**/*.js',
'activities/TankOp.Activity/**/*.js',
'activities/TurtleBlocksJS.Activity/**/*.js',
'activities/VideoViewer.Activity/**/*.js',
'activities/WelcomeWeb.Activity/**/*.js'
],
dest: 'build/'
}
}
});

// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');

// Default task(s).
grunt.registerTask('default', ['uglify']);

};
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ The activity will look for media content on the server referenced in [activities

To remove resources for **Etoys**, remove directory [activities/Etoys.activities/resources](activities/Etoys.activities/resources) and replace the value `resources/etoys.image` in [activities/Etoys.activities/index.html](activities/Etoys.activities/index.html) by the remote location of the resources, for example `http://server.sugarizer.org/activities/Etoys.activity/resources/etoys.image`.

# Optimize performance

If you want to optimize JavaScript performance, you could generate an optimized version of Sugarizer with [Grunt](http://gruntjs.com). This optimized version will minimize and reduce size of all JavaScript files.

The [Gruntfile.js](Grunfile.js) contains tasks settings to build an optimized version of Sugarizer. To do that, ensure first that grunt is installed:

npm install -g grunt-cli

Then just launch:

grunt

At the end of the process, the `build` directory will contain the optimized version of each file in a same directory that the initial one, so you could just copy files:

cp -r build/* .

# Localization

Sugarizer use [webL10n](https://github.com/fabi1cazenave/webL10n) localization system by Fabien Cazenave.
Expand Down
22 changes: 11 additions & 11 deletions activities/Abecedarium.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@


define(function (require) {
Abcd.activity = require("sugar-web/activity/activity");
define(["sugar-web/activity/activity"], function (activity) {
Abcd.activity = activity;
app = null;

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
// Initialize the activity
Abcd.activity.setup();

// Create sound component
Abcd.sound = new Abcd.Audio();
Abcd.sound.renderInto(document.getElementById("header"));

// Create and display first screen
app = new Abcd.App().renderInto(document.getElementById("body"));

// Load context
Abcd.loadContext(function() {
app.restartLastGame();
});
});

// Stop sound at end of game to sanitize media environment, specifically on Android
document.getElementById("stop-button").addEventListener('click', function (event) {
// Stop sound at end of game to sanitize media environment, specifically on Android
document.getElementById("stop-button").addEventListener('click', function (event) {
Abcd.sound.pause();
});
});
});
});

});
13 changes: 6 additions & 7 deletions activities/ActivityTemplate/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
define(function (require) {
var activity = require("sugar-web/activity/activity");
define(["sugar-web/activity/activity"], function (activity) {

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {

// Initialize the activity.
activity.setup();
// Initialize the activity.
activity.setup();

});
});

});
12 changes: 3 additions & 9 deletions activities/Calculate.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
//Please look at calculateapp.js to see the related functions

/* Start of the app, we require everything that is needed */
define(function(require) {
var activity = require("sugar-web/activity/activity");
require("activity/calculate-activity");
require("activity/calculate-app");
require("math");
require("parser");
require("nanomodal");
CalculateApp.libs.palette = require("sugar-web/graphics/palette");
define(["sugar-web/activity/activity","mustache","sugar-web/graphics/palette","activity/calculate-activity","activity/calculate-app","math","parser","nanomodal"], function (activity, mustache, calcpalette) {
CalculateApp.libs.palette = calcpalette;

//function-plot depends on d3.
require(["d3"], function(d) {
Expand All @@ -19,7 +13,7 @@ define(function(require) {
});

CalculateApp.libs.activity = activity;
CalculateApp.libs.mustache = require("mustache");
CalculateApp.libs.mustache = mustache;

require(['domReady!', 'activity/trigo-palette', 'activity/algebra-palette', 'webL10n', 'sugar-web/datastore'], function(doc, trigoPaletteLib, algebraPaletteLib, webL10n, datastore) {
CalculateApp.libs.webL10n = webL10n;
Expand Down
30 changes: 14 additions & 16 deletions activities/ChatPrototype.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
define(function (require) {
define(["sugar-web/activity/activity","sugar-web/graphics/palette","sugar-web/graphics/presencepalette"], function (activity,palette,presencepalette) {
var activity = require("sugar-web/activity/activity");
var palette = require("sugar-web/graphics/palette");
var presencepalette = require("sugar-web/graphics/presencepalette");


// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {

Expand All @@ -14,7 +12,7 @@ define(function (require) {
var messagesList = document.getElementById('messages');
var socketStatus = document.getElementById('status');
var messageContent = document.getElementById('content');

var userSettings = null;

// Connect to network
Expand All @@ -23,17 +21,17 @@ define(function (require) {
presenceObject = activity.getPresenceObject(function (error, presence) {
// Unable to join
if (error) {
socketStatus.innerHTML = 'Error';
socketStatus.innerHTML = 'Error';
socketStatus.className = 'error';
return;
}

// Store settings
userSettings = presence.getUserInfo();
socketStatus.innerHTML = 'Connected';
socketStatus.className = 'open';
messageField.readOnly = false;

// Not found, create a new shared activity
if (!window.top.sugar.environment.sharedId) {
presence.createSharedActivity('org.sugarlabs.ChatPrototype', function (groupId) {
Expand All @@ -46,19 +44,19 @@ define(function (require) {
socketStatus.innerHTML = 'Disconnected from WebSocket.';
socketStatus.className = 'closed';
});

// Display connection changed
presence.onSharedActivityUserChanged(function (msg) {
var userName = msg.user.name.replace('<','&lt;').replace('>','&gt;');
messagesList.innerHTML += '<li class="received" style = "color:blue">' + userName + (msg.move>0?' join':' leave') + ' the chat</li>';
});

// Handle messages received
presence.onDataReceived(function (msg) {
var text = msg.content;
var author = msg.user.name.replace('<','&lt;').replace('>','&gt;');
var colour = msg.user.colorvalue;

var authorElem = '<span style = "color:' + colour.stroke + '">' + author + '</span>';

myElem = document.createElement('li');
Expand All @@ -77,22 +75,22 @@ define(function (require) {
var networkButton = document.getElementById("network-button");
presencepalette = new presencepalette.PresencePalette(networkButton, undefined);
presencepalette.addEventListener('shared', shareActivity);

// Launched with a shared id, activity is already shared
if (window.top.sugar.environment.sharedId) {
shareActivity();
presencepalette.setShared(true);
}

// Handle message text update
messageField.onkeydown = function (e) {
if (e.keyCode === 13) {
var message = messageField.value;

// Send the message through the WebSocket.
var toSend = {user: userSettings, content: message};
presenceObject.sendMessage(presenceObject.getSharedInfo().id, toSend);

// Clear out the message field
messageField.placeholder = "Write your message here...";
messageField.value = "";
Expand All @@ -103,4 +101,4 @@ define(function (require) {

});

});
});
10 changes: 3 additions & 7 deletions activities/Clock.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
define(function (require) {
var activity = require("sugar-web/activity/activity");
var radioButtonsGroup = require("sugar-web/graphics/radiobuttonsgroup");
var mustache = require("mustache");
var moment = require("moment");
define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","mustache","moment"], function (activity,radioButtonsGroup,mustache,moment) {

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
Expand Down Expand Up @@ -85,7 +81,7 @@ define(function (require) {
this.clockCanvasElem.style.display='none';
this.clockCanvasElem.offsetHeight;
this.clockCanvasElem.style.display='block';
}
}
}
requestAnimationFrame(this.tick.bind(this));
}
Expand Down Expand Up @@ -140,7 +136,7 @@ define(function (require) {
this.clockContainerElem.style.height = this.size + "px";

this.clockCanvasElem.style.width = (this.size+4) + "px";

this.margin = this.size * 0.02;
this.radius = (this.size - (2 * this.margin)) / 2;

Expand Down
3 changes: 1 addition & 2 deletions activities/Cordova.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
define(function (require) {
var activity = require("sugar-web/activity/activity");
define(["sugar-web/activity/activity"], function (activity) {

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
Expand Down
19 changes: 8 additions & 11 deletions activities/FoodChain.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@


define(function (require) {
l10n = require("webL10n");
var activity = require("sugar-web/activity/activity");
var radioButtonsGroup = require("sugar-web/graphics/radiobuttonsgroup");
var datastore = require("sugar-web/datastore");
define(["sugar-web/activity/activity","webL10n","sugar-web/graphics/radiobuttonsgroup","sugar-web/datastore"], function (activity, _l10n, radioButtonsGroup, datastore) {
l10n = _l10n;
var app = null;
var isFirefoxOS = (navigator.userAgent.indexOf('Mozilla/5.0 (Mobile') != -1);

Expand All @@ -13,7 +10,7 @@ define(function (require) {
// Initialize the activity
FoodChain.activity = activity;
FoodChain.activity.setup();

// Initialize buttons
var languageRadio = new radioButtonsGroup.RadioButtonsGroup([
document.getElementById("en-button"),
Expand All @@ -27,7 +24,7 @@ define(function (require) {
l10n.language.code = "fr";
FoodChain.setLocale();
};

// Wait for locale load
var localized_received = function() {
// Init activity
Expand All @@ -37,16 +34,16 @@ define(function (require) {
FoodChain.sound.renderInto(document.getElementById("header"));

// Create and display first screen
FoodChain.context.home = app = new FoodChain.App().renderInto(document.getElementById("body"));
FoodChain.context.home = app = new FoodChain.App().renderInto(document.getElementById("body"));
FoodChain.setLocale();

// Load context
FoodChain.loadContext(function() {
app.playGame({
name: FoodChain.context.game.replace("FoodChain.", ""),
level: FoodChain.context.level
});
});
});
} else {
// Just change locale
FoodChain.setLocale();
Expand All @@ -60,7 +57,7 @@ define(function (require) {
// Stop sound at end of game to sanitize media environment, specifically on Android
document.getElementById("stop-button").addEventListener('click', function (event) {
FoodChain.sound.pause();
});
});
});

});
5 changes: 1 addition & 4 deletions activities/Gears.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
define(function (require) {
var activity = require("sugar-web/activity/activity");
var radioButtonsGroup = require("sugar-web/graphics/radiobuttonsgroup");
require("gearsketch_main");
define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","gearsketch_main"], function (activity,radioButtonsGroup) {

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
Expand Down
8 changes: 1 addition & 7 deletions activities/GetThingsDone.activity/js/activity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
define(function (require) {
var activity = require("sugar-web/activity/activity");
var datastore = require("sugar-web/datastore");

var model = require("activity/model");
var view = require("activity/view");
var controller = require("activity/controller");
define(["sugar-web/activity/activity","sugar-web/datastore","activity/model","activity/view","activity/controller"], function (activity,datastore,model,view,controller) {

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
Expand Down
7 changes: 3 additions & 4 deletions activities/Gridpaint.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
// If you want to add features please make a fork with a different name.
// Thanks in advance

define(function (require) {
activity = require("sugar-web/activity/activity");
var datastore = require("sugar-web/datastore");
define(["sugar-web/activity/activity","sugar-web/datastore"], function (_activity, datastore) {
activity = _activity;

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
Expand All @@ -39,7 +38,7 @@ define(function (require) {

// Init activity and launch it
loadGallery(null);
appInit();
appInit();

// Load gallery from datastore
datastoreObject.loadAsText(function (error, metadata, data) {
Expand Down
Loading

0 comments on commit 7905031

Please sign in to comment.