]> source.dussan.org Git - jquery.git/commitdiff
Fix #13776: Add banner before generating source map. Close gh-1246.
authorRichard Gibson <richard.gibson@gmail.com>
Thu, 18 Apr 2013 17:19:22 +0000 (13:19 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Thu, 18 Apr 2013 17:48:51 +0000 (13:48 -0400)
(cherry picked from commit 9d16fe6283667396094d49559a37fc672c06252c)

Gruntfile.js
package.json

index 27786474e365e7c87196bc73233867cf9e4345dd..bf31ddf8d11f9471dba1472715ceca74157721b9 100644 (file)
@@ -41,7 +41,7 @@ module.exports = function( grunt ) {
                        srcFile: "src/sizzle/dist/sizzle.js"
                },
                build: {
-                       all:{
+                       all: {
                                dest: "dist/jquery.js",
                                src: [
                                        "src/intro.js",
@@ -105,22 +105,39 @@ module.exports = function( grunt ) {
                        tasks: "dev"
                },
 
+               "pre-uglify": {
+                       all: {
+                               files: {
+                                       "dist/jquery.pre-min.js": [ "dist/jquery.js" ]
+                               },
+                               options: {
+                                       banner: "/*! jQuery v<%= pkg.version %> | " +
+                                               "(c) 2005, 2013 jQuery Foundation, Inc. | " +
+                                               "jquery.org/license\n" +
+                                               "//@ sourceMappingURL=jquery.min.map\n" +
+                                               "*/"
+                               }
+                       }
+               },
                uglify: {
                        all: {
                                files: {
-                                       "dist/jquery.min.js": [ "dist/jquery.js" ]
+                                       "dist/jquery.min.js": [ "dist/jquery.pre-min.js" ]
                                },
                                options: {
-                                       banner: "/*! jQuery v<%= pkg.version %> | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license */",
+                                       // Keep our hard-coded banner
+                                       preserveComments: "some",
                                        sourceMap: "dist/jquery.min.map",
+                                       sourceMappingURL: "jquery.min.map",
+                                       report: "min",
+                                       beautify: {
+                                               ascii_only: true
+                                       },
                                        compress: {
                                                hoist_funs: false,
                                                join_vars: false,
                                                loops: false,
                                                unused: false
-                                       },
-                                       beautify: {
-                                               ascii_only: true
                                        }
                                }
                        }
@@ -221,7 +238,6 @@ module.exports = function( grunt ) {
                grunt.log.writeln( "File '" + name + "' created." );
        });
 
-
        // Special "alias" task to make custom build creation less grawlix-y
        grunt.registerTask( "custom", function() {
                var done = this.async(),
@@ -241,7 +257,7 @@ module.exports = function( grunt ) {
 
                grunt.util.spawn({
                        cmd: process.platform === "win32" ? "grunt.cmd" : "grunt",
-                       args: [ "build:*:*:" + modules, "uglify", "dist" ]
+                       args: [ "build:*:*:" + modules, "pre-uglify", "uglify", "dist" ]
                }, function( err, result ) {
                        if ( err ) {
                                grunt.verbose.error();
@@ -256,7 +272,6 @@ module.exports = function( grunt ) {
        });
 
        // Special concat/build task to handle various jQuery build requirements
-       //
        grunt.registerMultiTask(
                "build",
                "Concatenate source (include/exclude modules with +/- flags), embed date/version",
@@ -434,7 +449,7 @@ module.exports = function( grunt ) {
                nonascii = false;
 
                distpaths.forEach(function( filename ) {
-                       var i, c, map,
+                       var i, c,
                                text = fs.readFileSync( filename, "utf8" );
 
                        // Ensure files use only \n for line endings, not \r\n
@@ -462,19 +477,17 @@ module.exports = function( grunt ) {
                        if ( /\.map$/.test( filename ) ) {
                                text = text.replace( /"dist\//g, "\"" );
                                fs.writeFileSync( filename, text, "utf-8" );
+
+                       // Use our hard-coded sourceMap directive instead of the autogenerated one (#13274; #13776)
                        } else if ( /\.min\.js$/.test( filename ) ) {
-                               // Wrap sourceMap directive in multiline comments (#13274)
-                               text = text.replace( /\n?(\/\/@\s*sourceMappingURL=)(.*)/,
-                                       function( _, directive, path ) {
-                                               map = "\n" + directive + path.replace( /^dist\//, "" );
-                                               return "";
+                               i = 0;
+                               text = text.replace( /(?:\/\*|)\n?\/\/@\s*sourceMappingURL=.*(\n\*\/|)/g,
+                                       function( match ) {
+                                               if ( i++ ) {
+                                                       return "";
+                                               }
+                                               return match;
                                        });
-                               if ( map ) {
-                                       text = text.replace( /(^\/\*[\w\W]*?)\s*\*\/|$/,
-                                               function( _, comment ) {
-                                                       return ( comment || "\n/*" ) + map + "\n*/";
-                                               });
-                               }
                                fs.writeFileSync( filename, text, "utf-8" );
                        }
 
@@ -495,6 +508,24 @@ module.exports = function( grunt ) {
                return !nonascii;
        });
 
+       // Work around grunt-contrib-uglify sourceMap issues (jQuery #13776)
+       grunt.registerMultiTask( "pre-uglify", function() {
+               var banner = this.options().banner;
+
+               this.files.forEach(function( mapping ) {
+                       // Join src
+                       var input = mapping.src.map(function( file ) {
+                               var contents = grunt.file.read( file );
+
+                               // Strip banners
+                               return contents.replace( /^\/\*!(?:.|\n)*?\*\/\n?/gm, "" );
+                       }).join("\n");
+
+                       // Write temp file (with optional banner)
+                       grunt.file.write( mapping.dest, ( banner || "" ) + input );
+               });
+       });
+
        // Load grunt tasks from NPM packages
        grunt.loadNpmTasks("grunt-compare-size");
        grunt.loadNpmTasks("grunt-git-authors");
@@ -504,7 +535,7 @@ module.exports = function( grunt ) {
        grunt.loadNpmTasks("grunt-contrib-uglify");
 
        // Default grunt
-       grunt.registerTask( "default", [ "update_submodules", "selector", "build:*:*", "jshint", "uglify", "dist:*", "compare_size" ] );
+       grunt.registerTask( "default", [ "update_submodules", "selector", "build:*:*", "jshint", "pre-uglify", "uglify", "dist:*", "compare_size" ] );
 
        // Short list as a high frequency watch task
        grunt.registerTask( "dev", [ "selector", "build:*:*", "jshint" ] );
index b44de1e15e9e613ff3829240fc91434ce5aa2d54..27c041fe5579c6b712bb438ce2eb71979a700605 100644 (file)
@@ -28,7 +28,7 @@
                "grunt-update-submodules": "0.2.0",
                "grunt-contrib-watch": "0.3.1",
                "grunt-contrib-jshint": "0.3.0",
-               "grunt-contrib-uglify": "0.1.2",
+               "grunt-contrib-uglify": "0.2.0",
                "grunt": "0.4.1",
                "gzip-js": "0.3.1",
                "testswarm": "0.2.2"