diff --git a/grunt/Gruntfile.js b/grunt/Gruntfile.js index ffeadc1e..cf95b583 100644 --- a/grunt/Gruntfile.js +++ b/grunt/Gruntfile.js @@ -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"]); diff --git a/grunt/fragments/after.frag b/grunt/fragments/after.frag index 130eeace..bee212f3 100644 --- a/grunt/fragments/after.frag +++ b/grunt/fragments/after.frag @@ -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)); \ No newline at end of file +} (this)); diff --git a/grunt/fragments/after.frag.min b/grunt/fragments/after.frag.min deleted file mode 100644 index 14046ea4..00000000 --- a/grunt/fragments/after.frag.min +++ /dev/null @@ -1 +0,0 @@ -void function(e,t){if(e.ToneDefinedDefine){e.define=t;e.ToneDefinedDefine=t}}(this,undefined) \ No newline at end of file diff --git a/grunt/fragments/after.preset.frag b/grunt/fragments/after.preset.frag new file mode 100644 index 00000000..b4cc3eaf --- /dev/null +++ b/grunt/fragments/after.preset.frag @@ -0,0 +1 @@ +} (this)); diff --git a/grunt/fragments/before.frag b/grunt/fragments/before.frag index 129c5111..14286a8e 100644 --- a/grunt/fragments/before.frag +++ b/grunt/fragments/before.frag @@ -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); + } + \ No newline at end of file diff --git a/grunt/fragments/before.frag.min b/grunt/fragments/before.frag.min deleted file mode 100644 index 1239feb1..00000000 --- a/grunt/fragments/before.frag.min +++ /dev/null @@ -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) \ No newline at end of file diff --git a/grunt/fragments/before.preset.frag b/grunt/fragments/before.preset.frag index 93ccaa4f..925e3b19 100644 --- a/grunt/fragments/before.preset.frag +++ b/grunt/fragments/before.preset.frag @@ -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); + } \ No newline at end of file diff --git a/grunt/package.json b/grunt/package.json index ec80f124..b0bd8985 100644 --- a/grunt/package.json +++ b/grunt/package.json @@ -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"