Skip to content

Instantly share code, notes, and snippets.

@megurock
Last active August 29, 2015 14:20
Show Gist options
  • Save megurock/faee37dfbad7d2f1e741 to your computer and use it in GitHub Desktop.
Save megurock/faee37dfbad7d2f1e741 to your computer and use it in GitHub Desktop.
dynamic compilation config for jade and sass
module.exports = function(grunt) {
var glob = require('glob');
// load plugins in 'devDependencies'
require('load-grunt-tasks')(grunt, {
scope: 'devDependencies'
});
// load a configuration file that specifies 'jade' amd 'sass' compilation settings.
var pages = grunt.file.readJSON('compilation_config.json');
/**
*
* @param taskName:String
* @param updateFilepath:String
* @param pageConfig: Object
* @param options: Object
*/
var createCompilationTask = function(taskName, updateFilepath, pageConfig, options) {
var needCompile,
taskConfig = {
files: {}
};
if (options !== undefined) {
taskConfig.options = options;
}
for (var pageDest in pageConfig) {
var page = pageConfig[pageDest],
srcList = page.src;
for (var i = 0, len = srcList.length; i < len; i++) {
var src = srcList[i],
files = glob.sync(src);
for (var j = 0, fileLen = files.length; j < fileLen; j++) {
if (updateFilepath === files[j]) {
needCompile = true;
taskConfig.files[pageDest] = page.compileFile;
break;
}
}
}
}
if (needCompile) {
grunt.config(taskName.split(':'), taskConfig);
return [taskName];
}
};
/**
*
*/
grunt.initConfig({
project: {
src: 'src',
release: 'release'
},
connect: {
dev: {
options: {
port: 8000,
livereload: true,
open: true,
base: '<%= project.src %>'
}
}
},
esteWatch: {
options: {
livereload: {
enabled: true,
port: 35729,
extensions: ['js', 'html', 'css']
},
dirs: [
'<%= project.src %>/userweb/',
'<%= project.src %>/userweb/js/**/',
'<%= project.src %>/userweb/scss/**/'
]
},
js: function(filepath) {
var taskConfig = {
src: filepath
};
grunt.config(['jshint', 'dev'], taskConfig);
return ['jshint:dev'];
},
scss: function(filepath) {
return createCompilationTask('sass:dev', filepath, pages.sass);
},
/*css: function(filepath) {
var taskConfig = {
src: filepath,
dest: filepath.replace('.tmp/', "")
};
grunt.config(['autoprefixer', 'dev'], taskConfig);
return ['autoprefixer:dev'];
},*/
"*": function(filepath) {
//
}
},
sass: {
options: {
compass: true,
style: 'compact',
sourcemap: 'none'
},
all: {
files: [{
expand: true,
cwd: '<%= project.src %>/userweb/scss/',
src: ['**/*.scss'],
dest: '<%= project.src %>/userweb/css/',
ext: '.css'
}]
}
},
autoprefixer: {
options: {
browsers: ['last 10 versions', 'ie >= 7']
},
all: {
files: [{
expand: true,
src: '<%= project.src %>/userweb/css/*.css',
dest: '<%= project.src %>/userweb/css/'
}]
}
},
jshint: {
all: {
src: [
'<%= project.src %>/userweb/**/*.js',
'!<%= project.src %>/userweb/**/*min.js'
]
}
},
clean: {
release: ['<%= project.release %>/']
},
copy: {
release: {
files: [{
expand: true,
cwd: '<%= project.src %>/',
dest: '<%= project.release %>/',
src: [
'**',
'!**/scss/**'
],
dot: false
}]
}
}
});
grunt.registerTask('default', ['connect', 'esteWatch']);
grunt.registerTask('release', ['clean:release', 'copy:release'])
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment