How to include file name in uglify banner in grunt?

I use grunt to minimize some JS files. It would be nice to include the file name in the banner. I found several examples of how to include the package name in the banner, but I have not yet been able to get the file name there. So: what do I need to put in the grunt file (see below) instead of pkg.name in order to get the original file name (or dest file name) there?

Thanks in advance

module.exports = function(grunt) {

   // Project configuration.
   grunt.initConfig({
      pkg: grunt.file.readJSON('package.json'),
      uglify: {
         options: {
            banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
         },
         build: {
            files: [{
               expand: true,
               src: '**/*.js',
               dest: 'build',
               cwd: 'src',
               ext: '.min.js'
            }]
         }
      }
   });

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

   // Default task(s).
   grunt.registerTask('default', ['uglify']);
};
+4
source share
5 answers

I tried to do the same and could not find a link to the current file name that was set to the template. Here's a workaround that I finally understood; This is a custom task that dynamically creates a new target for each file:

grunt.registerMultiTask('minify', function () {
    this.files.forEach(function (file) {
        var path = file.src[0],
            target = path.match(/src\/(.*)\.js/)[1];

        // store some information about this file in config
        grunt.config('ugtargets.' + target, {
            path: path,
            filename: path.split('/').pop()
        });

        // create and run an uglify target for this file
        grunt.config('uglify.' + target + '.files', [{
            src: [path],
            dest: path.replace(/^src\/(.*)\.js$/, 'build/$1.min.js')
        }]);
        grunt.task.run('uglify:' + target);
    });
});

And my uglify setup:

uglify: {
    options: {
        banner: 'Filename: <% ugtargets[grunt.task.current.target].filename %>\n'
    }
}
  • . , ; . ; fs JSDoc .
  • Grunt, . ; , .
  • , uglify. src dest ; , . uglify .

grunt.task.current.target , . Presto!

+1

, <%= pkg.name %> , <% for ( var s in grunt) { %> \ngrunt.<%=s%><% } %> <% for ( var s in this) { %> \nthis.<%=s%><% } %>.

( ), , :

var bannerTemplate = '<%' +' var subtask = uglify[grunt.task.current.target];' +' var file = subtask?subtask.dest:\'\';' +' var filename = file.split(\'/\').pop();' +'%>' +'/*! <%= filename %>' +'\n * version: <%= pkg.version %>' +'\n * author: <%= pkg.author %>' +'\n */\n';

0

, uglify, .

    uglify: {
        options: {
            banner: 
            '<% var subtask = uglify[grunt.task.current.target]; %>' +
            '/* <%= subtask.name %> <%= pkg.version %> (<%= grunt.template.today("yyyy-mm-dd, HH:MM") %>)\n' +
            ' */\n'
        },
        main: {
            name: 'main.min.js',
            files: [{
                src: 'build/files/js/main.min.js',
                dest: 'build/files/js/main.min.js'
            }]
        },
        vendor: {
            name: 'vendor.min.js',
            files: [{
                src: 'build/files/js/vendor.min.js',
                dest: 'build/files/js/vendor.min.js'
            }]
        }
    }
0

:

banner: grunt.file.read('./folder/file.js'),

fooobar.com/questions/1536136/...

0

https://github.com/mattstyles/grunt-banner/issues/5#issuecomment-33445038

process: function (src, filepath) {} completed the task.

For me, I want to add "// # sourceUrl = xxx.min.js" at the bottom of each cleared .min.js so that I can debug these dynamically loaded .min.js. The following simple Gruntfile.js works for me:

module.exports = function (grunt) {
    var cwd = "/Branding/Layouts/JS/";
    var src = [
                "file1.js",
                "file2.js",
                "file3.js"
    ];

    var minified_src = [];
    for (i=0;  i< src.length; i++)
        minified_src.push(src[i].replace(/\.js$/g, ".min.js"));

    var config = grunt.initConfig({
        "uglify": {
            options: {
                sourceMap: true
            },
            target: {
                files: [
                    {
                        expand: true,
                        cwd: cwd,
                        src: src,
                        dest: cwd,
                        ext: ".min.js",
                        extDot: "last"
                    }
                ]
            }
        },
        concat: {
            options: {
                process: function (src, filepath) {
                    return src + "\n//# sourceURL=" + filepath.split("/").slice(-1);
                }
            },
            target: {
                files: [
                    {
                        expand: true,
                        cwd: cwd,
                        src: minified_src,
                        dest: cwd
                    }
                ]
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.registerTask('default', ['uglify', 'concat']);
};

Note: uglify cannot save a comment that is not inside the code block (e.g. // # sourceMap = xxx.js), I have to use concat to add a comment after completion.

Wow, this is my first stackoverflow post.

0
source

Source: https://habr.com/ru/post/1536134/


All Articles