new build regexp

removes all defines and replaces them with a function. this enables the
tonejs build to be used with requirejs
This commit is contained in:
Yotam Mann 2014-10-28 14:41:54 -04:00
parent a193820a64
commit e108e59066
8 changed files with 84 additions and 88 deletions

View file

@ -23,14 +23,6 @@ module.exports = function(grunt) {
optimize : "none"
}
},
min: {
options: {
baseUrl: "../",
name : "main",
out: "./Tone.min.js.tmp",
optimize : "uglify2"
}
}
},
copy : {
npm : {
@ -79,24 +71,12 @@ module.exports = function(grunt) {
"../build/Tone.js" : ["./Tone.js.tmp"],
}
},
removeRequireStringMin: {
options: {
process: function(src) {
var withoutRequire = src.substr(0, src.indexOf("require([") - 1);
return withoutRequire;
},
},
files: {
"../build/Tone.min.js" : ["./Tone.min.js.tmp"],
}
}
},
clean: {
options: {
force: true,
},
dist: ["../main.js","./Tone.js.tmp"],
min: ["../main.js","./Tone.min.js.tmp"],
},
wrap: {
dist: {
@ -110,24 +90,17 @@ module.exports = function(grunt) {
src: ["../build/Tone.Preset.js"],
dest: "../build/Tone.Preset.js",
options: {
wrapper: [grunt.file.read("./fragments/before.preset.frag"), grunt.file.read("./fragments/after.frag")]
wrapper: [grunt.file.read("./fragments/before.preset.frag"), grunt.file.read("./fragments/after.preset.frag")]
}
},
min: {
src: ["../build/Tone.min.js"],
dest: "../build/Tone.min.js",
options: {
wrapper: [grunt.file.read("./fragments/before.frag.min"), grunt.file.read("./fragments/after.frag.min")]
}
}
},
indent: {
dist: {
src: ["./Tone.js.tmp"],
dest: "./Tone.js.tmp",
src: ["../build/Tone.js"],
dest: "../build/Tone.js",
options: {
style: "tab",
size: 4,
size: 1,
change: 1
}
},
@ -140,6 +113,54 @@ module.exports = function(grunt) {
change: 1
}
}
},
uglify : {
dist : {
options : {
preserveComments : "some"
},
files: {
"../build/Tone.min.js": ["../build/Tone.js"]
}
}
},
replace: {
dist: {
options: {
patterns: [
{
match: /define\('([^']*)'\w*,\w*\[([^\]]*)\]\w*,\w*/g,
replacement: "toneModule("
},
{
match: /define\('Tone\/core\/Tone',\[\],/gi,
replacement: "mainModule("
},
{
match: /\n"use strict";\n/g,
replacement: ""
}
]
},
files: [{
src: ["../build/Tone.js"],
dest: "../build/Tone.js"
}]
},
presets: {
options: {
patterns: [
{
match: /define\(\w*\[([^\]]*)\]\w*,\w*/g,
replacement: "tonePreset("
}
]
},
files: [{
src: ["../build/Tone.Preset.js"],
dest: "../build/Tone.Preset.js"
}]
}
}
});
@ -151,13 +172,15 @@ module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-replace");
grunt.loadNpmTasks("grunt-contrib-uglify");
// Default task(s).
grunt.registerTask("docs", ["jsdoc:src"]);
grunt.registerTask("npm", ["copy:npm"]);
grunt.registerTask("presets", ["concat:presets", "wrap:presets"]);
grunt.registerTask("min", ["concat:dist", "requirejs:min", "concat:removeRequireStringMin", "clean:min", "wrap:min"]);
grunt.registerTask("build", ["concat:dist","requirejs:compile","concat:removeRequireString", "clean:dist", "wrap:dist"]);
grunt.registerTask("presets", ["concat:presets", "indent:presets", "replace:presets", "wrap:presets"]);
grunt.registerTask("min", ["uglify:dist"]);
grunt.registerTask("build", ["concat:dist","requirejs:compile","concat:removeRequireString", "clean:dist", "indent:dist", "replace:dist", "wrap:dist"]);
grunt.registerTask("buildall", ["build", "min", "presets"]);
grunt.registerTask("dist", ["buildall", "docs", "npm"]);

View file

@ -1,9 +1,10 @@
(function(root, undef) {
//leave no trace
//undefine 'define'
if (root.ToneDefinedDefine){
root.define = undef;
root.ToneDefinedDefine = undef;
//requirejs compatibility
if ( typeof define === "function" && define.amd ) {
define( "Tone", [], function() {
return Tone;
});
} else {
root.Tone = Tone;
}
}(this, undefined));
} (this));

View file

@ -1 +0,0 @@
void function(e,t){if(e.ToneDefinedDefine){e.define=t;e.ToneDefinedDefine=t}}(this,undefined)

View file

@ -0,0 +1 @@
} (this));

View file

@ -1,29 +1,12 @@
(function (root) {
// Tone.js can run with or without requirejs
//
// this anonymous function checks to see if the 'define'
// method exists, if it does not (and there is not already
// a function called Tone) it will create a function called
// 'define'. 'define' will invoke the 'core' module and attach
// its return value to the root. for all other modules
// Tone will be passed in as the argument.
if (typeof define !== "function" &&
typeof root.Tone !== "function") {
//define 'define' to invoke the callbacks with Tone
root.ToneDefinedDefine = true;
root.define = function(){
//the last argument is the callback
var lastArg = arguments[arguments.length - 1];
//the first argument is the dependencies or name
var firstArg = arguments[0];
if (firstArg === "Tone/core/Tone"){
//create the root object
root.Tone = lastArg();
} else if (typeof lastArg === "function"){
//if it's not the root, pass in the root
//as the parameter
lastArg(root.Tone);
}
};
"use strict";
var Tone;
//constructs the main Tone object
function mainModule(func){
Tone = func();
}
} (this));
//invokes each of the modules with the main Tone object as the argument
function toneModule(func){
func(Tone);
}

View file

@ -1 +0,0 @@
(function(e){if(typeof define!=="function"&&typeof e.Tone!=="function"){e.ToneDefinedDefine=true;e.define=function(){var t=arguments[arguments.length-1];var n=arguments[0];if(n==="Tone/core/Tone"){e.Tone=t()}else if(typeof t==="function"){t(e.Tone)}}}})(this)

View file

@ -1,19 +1,7 @@
(function (root) {
//AMD shim for the presets build
if (typeof define !== "function" &&
typeof root.Tone === "function") {
//define 'define' to invoke the callbacks with Tone
root.ToneDefinedDefine = true;
root.define = function(){
//the last argument is the callback
var lastArg = arguments[arguments.length - 1];
//the first argument is the dependencies or name
if (typeof lastArg === "function"){
//if it's not the root, pass in the root
//as the parameter
lastArg(root.Tone);
}
};
}
} (this));
"use strict";
function tonePreset(func){
func(root.Tone);
}

View file

@ -16,7 +16,9 @@
"devDependencies": {
"grunt-contrib-copy": "~0.5.0",
"grunt-wrap": "~0.3.0",
"grunt-indent": "~0.1.4"
"grunt-indent": "~0.1.4",
"grunt-replace": "~0.8.0",
"grunt-contrib-uglify": "~0.6.0"
},
"author": "",
"license": "ISC"