1. 程式人生 > >在使用grunt進行壓縮js時候,如何處理檔案里名稱面有多個點號?

在使用grunt進行壓縮js時候,如何處理檔案里名稱面有多個點號?

請注意,這個可能是grunt的bug,不過可以自己直接用rename來自定義js檔案的名稱。

(ext不能出現,因為ext優先順序高於rename,然後傳進去的src是已經處理好用ext處理好的名稱,只會有一個點號的。)

例如:

我們要壓縮p.fortest.js:

用的gruntfile.js內容:


但是壓縮出來的結果與我們的想象有很多出入:


國外的答案:

rite
2
I'm uglifying files with one to one mapping like this:

    plugins: {
        files: [{
            expand: true,
            src: '*.js',
            cwd: 'Scripts/v1/Plugins',
            dest: 'Scripts/v1/Build/Plugins',
            ext: '.min.js'
        }]
    },
And this works fine, until i start naming my files with multiple dots in filename. So above script will uglify 
4 files:
plugins.a.js
plugins.b.js
plugins.c.js
plugins.d.js

into one file:

plugins.min.js

while I expect:

plugins.a.min.js
plugins.b.min.js
plugins.c.min.js
plugins.d.min.js

Is this expected behavior or bug? In any case how can I keep one to one mapping with my naming convention.

task gruntjs minification
share|improve this question
asked May 17 '13 at 10:31

Sergej Popov
95911227
add a comment | 
2 Answers

active oldest votes
up vote 4 down vote accepted
It is currently the default expected behavior.

This has been brought up a few times with grunt:

https://github.com/gruntjs/grunt-contrib-uglify/issues/54
extension is after last period only
From the first link, a change has been submitted to node globule that will let you select either first or last dot.

Other than that (or until that lands) you could use a rename function to get the behavior you need.

share|improve this answer
answered May 18 '13 at 4:48

dc5
6,4612524
add a comment | 
up vote 2 down vote
Did you manage to find a solution for this? I checked all the issues in their github and apparently it should be fixed, but I'm still getting the same behavior with the latest builds.

EDIT: A solution was to pass a rename function and manually create the filename

files: {
            src: 'src/hp-lp-<%= pkg.version %>.js',
            dest: 'src/',
            expand: true,
            flatten: true,
            rename: function (dest, src) {
                var folder = src.substring(0, src.lastIndexOf('/'));
                var filename = src.substring(src.lastIndexOf('/'), src.length);

                filename = filename.substring(0, filename.lastIndexOf('.'));

                return dest + folder + filename + '.min.js';
            }
        }


ok,我們修改一下gruntfile.js如下:

module.exports = function (grunt) {
    //var fs = require('fs');
    //var localPath = fs.realpathSync('.');


    // 專案配置
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            options: {
                banner: '/*! <%= pkg.file %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            /*
            build: {
                src: 'public/module/p.fortest.js',
                dest: 'public/module/p.fortest.min.js'
            } */
            minjs: { //最小化、混淆所有 js/ 目錄下的 JavaScript 檔案
                files: [{
                    expand: true,
                    cwd: 'public/module/',
                    src: ['*.js', '!*.min.js'],
                    dest: 'public/module/'
                    //,ext: '.min.js'
                    ,rename: function (dest, src) {
                        var folder = src.substring(0, src.lastIndexOf('/'));
                       var filename = src.substring(src.lastIndexOf('/'), src.length);
                      //  var filename=src;
                        filename = filename.substring(0, filename.lastIndexOf('.'));

                        return dest + folder + filename + '.min.js';
                        //return  filename + '.min.js';
                    }
                }]
            }
        }
    });
    // 載入提供"uglify"任務的外掛
    grunt.loadNpmTasks('grunt-contrib-uglify');
    // 預設任務
    grunt.registerTask('default', ['uglify']);

}
下面是執行結果:

符合要求。