Created
October 3, 2012 03:27
-
-
Save 2no/3824817 to your computer and use it in GitHub Desktop.
Grunt で js を minify して gzip 圧縮
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 個別で js を minify して gzip したかったので、こんな感じになった | |
// ファイル名などにスペースが入ってた時どうするのか | |
module.exports = function(grunt) { | |
'use strict'; | |
var src = 'shared/js/*.js', | |
files = grunt.file.expandFiles(src), | |
suffix = '.min', | |
minFiles = {}, | |
gzFiles = {}; | |
files.forEach(function(file) { | |
var parts, path, | |
re = new RegExp('.+' + quote(suffix) + '\\..+$'); | |
if (re.test(file)) { | |
return; | |
} | |
parts = file.match(/(.+)(\..+)$/); | |
path = parts[1] + suffix + parts[2]; | |
minFiles[file] = { | |
src: [file], | |
dest: path | |
}; | |
gzFiles[path + '.gz'] = path; | |
}); | |
grunt.initConfig({ | |
watch: { | |
scripts: { | |
files: [src], | |
tasks: 'default' | |
} | |
}, | |
min: minFiles, | |
compress: { | |
gzip: { | |
options: { | |
mode: 'gzip' | |
}, | |
files: gzFiles | |
} | |
} | |
}); | |
grunt.loadNpmTasks('grunt-contrib'); | |
grunt.registerTask('default', 'min compress'); | |
function quote(str) | |
{ | |
return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment