Skip to content

Instantly share code, notes, and snippets.

@2no
Created October 3, 2012 03:27
Show Gist options
  • Save 2no/3824817 to your computer and use it in GitHub Desktop.
Save 2no/3824817 to your computer and use it in GitHub Desktop.
Grunt で js を minify して gzip 圧縮
// 個別で 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