From 1ce300d4765b15bfd19be8e8d024c91de37976d6 Mon Sep 17 00:00:00 2001 From: Benjamin Solum Date: Thu, 6 Mar 2014 11:26:14 -0600 Subject: [PATCH 1/4] Added support for source maps Added source map support for this plugin, also added documentation. --- README.md | 6 ++++++ tasks/closure-compiler.js | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4de0cd8..68f1770 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ grunt.initConfig({ closurePath: '/src/to/closure-compiler', js: 'static/src/frontend.js', jsOutputFile: 'static/js/frontend.min.js', + sourceMap: true, + sourceMapUrl: true, maxBuffer: 500, options: { compilation_level: 'ADVANCED_OPTIMIZATIONS', @@ -42,6 +44,10 @@ grunt.initConfig({ If `jsOutputFile` property is set, the script will be minified and saved to the file specified. Otherwise it will be output to the command line. +If the `sourceMap` property is set to a truthy value, the closure compiler flags --create_source_map and --source_map_format are set. If you pass this property a string value, the source map will use that string as the map name. Otherwise, boolean true assumes the file name with a .map extension. The --source_map_format flag is set to V3. + +If the `sourceMapUrl` property is set to a truthy value, the sourceMappingURL comment is appended to the generated JS file. If you pass this property as a string value, the sourceMappingURL will be set to that value. Otherwise, boolean true assumes the file name with a .map extension. + `maxBuffer` property If the buffer returned by closure compiler is more than 200kb, you will get an error saying "maxBuffer exceeded". To prevent this, you can set the maxBuffer to the preffered size you want (in kb) diff --git a/tasks/closure-compiler.js b/tasks/closure-compiler.js index 17bc38e..bee5848 100644 --- a/tasks/closure-compiler.js +++ b/tasks/closure-compiler.js @@ -16,7 +16,8 @@ module.exports = function(grunt) { var closurePath = '', reportFile = '', data = this.data, - done = this.async(); + done = this.async(), + sourceMapName; // Check for closure path. if (data.closurePath) { @@ -49,6 +50,20 @@ module.exports = function(grunt) { // Build command line. command += ' --js "' + data.js.join('" --js "') + '"'; + // Build Source Map + if (data.sourceMap) { + if (typeof data.sourceMap == 'boolean' && data.jsOutputFile) data.sourceMap = data.jsOutputFile + '.map'; + + if (!grunt.file.isPathAbsolute(data.sourceMap)) { + data.sourceMap = path.resolve('./') + '/' + data.sourceMap; + } + + sourceMapName = path.basename(data.sourceMap); + + command += ' --create_source_map "' + data.sourceMap + '"'; + command += ' --source_map_format=V3'; + } + if (data.jsOutputFile) { if (!grunt.file.isPathAbsolute(data.jsOutputFile)) { data.jsOutputFile = path.resolve('./') + '/' + data.jsOutputFile; @@ -94,6 +109,15 @@ module.exports = function(grunt) { done(false); } + // Check to see if we should add the source map comment to end of file + if (data.sourceMap && data.sourceMapUrl) { + if (typeof data.sourceMapUrl == 'boolean') data.sourceMapUrl = sourceMapName; + + fs.appendFile(data.jsOutputFile, '//# sourceMappingURL=' + data.sourceMapUrl, function (err) { + grunt.log.writeln('Could not add sourceMappingURL!'); + }); + } + if (stdout) { grunt.log.writeln(stdout); } From 2580201463359962d2d8e129de4597533a8308d4 Mon Sep 17 00:00:00 2001 From: Benjamin Solum Date: Fri, 7 Mar 2014 08:18:02 -0600 Subject: [PATCH 2/4] Updated to use options obj Updated to use options object for Source Map generation. --- README.md | 6 ++---- tasks/closure-compiler.js | 21 +++------------------ 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 68f1770..5a11f00 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,12 @@ grunt.initConfig({ closurePath: '/src/to/closure-compiler', js: 'static/src/frontend.js', jsOutputFile: 'static/js/frontend.min.js', - sourceMap: true, sourceMapUrl: true, maxBuffer: 500, options: { compilation_level: 'ADVANCED_OPTIMIZATIONS', - language_in: 'ECMASCRIPT5_STRICT' + language_in: 'ECMASCRIPT5_STRICT', + create_source_map: 'src.js.map' } } } @@ -44,8 +44,6 @@ grunt.initConfig({ If `jsOutputFile` property is set, the script will be minified and saved to the file specified. Otherwise it will be output to the command line. -If the `sourceMap` property is set to a truthy value, the closure compiler flags --create_source_map and --source_map_format are set. If you pass this property a string value, the source map will use that string as the map name. Otherwise, boolean true assumes the file name with a .map extension. The --source_map_format flag is set to V3. - If the `sourceMapUrl` property is set to a truthy value, the sourceMappingURL comment is appended to the generated JS file. If you pass this property as a string value, the sourceMappingURL will be set to that value. Otherwise, boolean true assumes the file name with a .map extension. `maxBuffer` property diff --git a/tasks/closure-compiler.js b/tasks/closure-compiler.js index bee5848..c5b8799 100644 --- a/tasks/closure-compiler.js +++ b/tasks/closure-compiler.js @@ -16,8 +16,7 @@ module.exports = function(grunt) { var closurePath = '', reportFile = '', data = this.data, - done = this.async(), - sourceMapName; + done = this.async(); // Check for closure path. if (data.closurePath) { @@ -50,20 +49,6 @@ module.exports = function(grunt) { // Build command line. command += ' --js "' + data.js.join('" --js "') + '"'; - // Build Source Map - if (data.sourceMap) { - if (typeof data.sourceMap == 'boolean' && data.jsOutputFile) data.sourceMap = data.jsOutputFile + '.map'; - - if (!grunt.file.isPathAbsolute(data.sourceMap)) { - data.sourceMap = path.resolve('./') + '/' + data.sourceMap; - } - - sourceMapName = path.basename(data.sourceMap); - - command += ' --create_source_map "' + data.sourceMap + '"'; - command += ' --source_map_format=V3'; - } - if (data.jsOutputFile) { if (!grunt.file.isPathAbsolute(data.jsOutputFile)) { data.jsOutputFile = path.resolve('./') + '/' + data.jsOutputFile; @@ -110,8 +95,8 @@ module.exports = function(grunt) { } // Check to see if we should add the source map comment to end of file - if (data.sourceMap && data.sourceMapUrl) { - if (typeof data.sourceMapUrl == 'boolean') data.sourceMapUrl = sourceMapName; + if (data.sourceMapUrl && data.options.create_source_map) { + if (typeof data.sourceMapUrl == 'boolean') data.sourceMapUrl = path.basename(data.jsOutputFile) + '.map'; fs.appendFile(data.jsOutputFile, '//# sourceMappingURL=' + data.sourceMapUrl, function (err) { grunt.log.writeln('Could not add sourceMappingURL!'); From 7942407f1e13d4999e41b42498cfb21f08dc7d19 Mon Sep 17 00:00:00 2001 From: Benjamin Solum Date: Fri, 7 Mar 2014 08:32:41 -0600 Subject: [PATCH 3/4] Switched source of sourceMapURL --- tasks/closure-compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/closure-compiler.js b/tasks/closure-compiler.js index c5b8799..7a3bb56 100644 --- a/tasks/closure-compiler.js +++ b/tasks/closure-compiler.js @@ -96,7 +96,7 @@ module.exports = function(grunt) { // Check to see if we should add the source map comment to end of file if (data.sourceMapUrl && data.options.create_source_map) { - if (typeof data.sourceMapUrl == 'boolean') data.sourceMapUrl = path.basename(data.jsOutputFile) + '.map'; + if (typeof data.sourceMapUrl == 'boolean') data.sourceMapUrl = path.basename(data.options.create_source_map); fs.appendFile(data.jsOutputFile, '//# sourceMappingURL=' + data.sourceMapUrl, function (err) { grunt.log.writeln('Could not add sourceMappingURL!'); From fb790340022c9e20bd59b11f1fb53ded80903029 Mon Sep 17 00:00:00 2001 From: Benjamin Solum Date: Fri, 7 Mar 2014 08:34:40 -0600 Subject: [PATCH 4/4] Added brackets to if statement Cleaner code. --- tasks/closure-compiler.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tasks/closure-compiler.js b/tasks/closure-compiler.js index 7a3bb56..3af08a5 100644 --- a/tasks/closure-compiler.js +++ b/tasks/closure-compiler.js @@ -96,7 +96,9 @@ module.exports = function(grunt) { // Check to see if we should add the source map comment to end of file if (data.sourceMapUrl && data.options.create_source_map) { - if (typeof data.sourceMapUrl == 'boolean') data.sourceMapUrl = path.basename(data.options.create_source_map); + if (typeof data.sourceMapUrl == 'boolean') { + data.sourceMapUrl = path.basename(data.options.create_source_map); + } fs.appendFile(data.jsOutputFile, '//# sourceMappingURL=' + data.sourceMapUrl, function (err) { grunt.log.writeln('Could not add sourceMappingURL!');