请注意,这个可能是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']);
}
符合要求。