Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module.exports = function(grunt) {

function localCssMiddleware() {
// Pour remplacer les CSS / JS de la page par les CSS / JS locales dans le cas où l'on a des fichiers en local
function localMiddleware() {

var fs = require('fs');
var url = require("url");
Expand All @@ -11,29 +12,46 @@ module.exports = function(grunt) {

var cfg = grunt.config.get('cfg');
var filename = null;

var parsed = url.parse(req.url);

var match = parsed.pathname.match(/style.*\.css(?:.map)?$/);

if (match) {
filename = cfg.css + '/' + match[0];
} else if(match = parsed.pathname.match(/[^\/]+.js(?:.map)?$/)){
filename = cfg.js + '/' + match[0];
} else if(match = parsed.pathname.match(/bower_components.*\.(scss|map)$/)) {
filename = __dirname + '/' + match[0];
} else if(match = parsed.pathname.match(/[^\/]+.scss$/)) {
filename = cfg.sass + '/' + match[0];
}
if (null != filename) {

if (null !== filename) {

//console.log(filename);
var contentType = 'text/css; charset=utf-8';
var exist = null;

if(/map$/.test(filename)) {
contentType = 'application/json; charset=utf-8';
} else if (/scss$/.test(filename)) {
contentType = 'text/scss; charset=utf-8';
} else if(/js$/.test(filename)){
contentType = 'application/javascript; charset=utf-8';
}
try {
var exist = fs.statSync(filename);
} catch (err) {
console.log("Le fichier "+filename+" n'existe pas en local et ne peut pas être synchronisé. La version présente sur le serveur sera utilisée.");
// Pour plus de détail sur l'erreur
// console.error(err);
}
if(null !== exist){
res.setHeader('Content-Type', contentType);
return res.end(fs.readFileSync(filename));
}
res.setHeader('Content-Type', contentType);
return res.end(fs.readFileSync(filename));
}
next();
}
};
}

grunt.initConfig({
Expand Down Expand Up @@ -78,16 +96,24 @@ module.exports = function(grunt) {
livereloadOnError: false,
dateFormat: function(time) {
grunt.log.write(grunt.template.today("[HH:MM:ss] "));
grunt.log.writeln('Execution en '+(''+time).green.bold+' ms');
grunt.log.writeln('Execution en '+time+'ms');
}
}
},
// JS -> https://github.com/gruntjs/grunt-contrib-watch : A CONFIGURER
scripts: {
files: ['<%= cfg.js %>/*.js'],
tasks: ['jshint'],
options: {
spawn: false,
},
},
},
postcss: {
options: {
map: true, // sourcemaps
processors: [
require('pixrem')(), // add fallbacks for rem units, cf https://github.com/robwierzbowski/node-pixrem pour les browers supportés
require('autoprefixer-core')(), // voir https://github.com/ai/browserslist pour definir les browers supportés dans le fichier 'browserslist'
require('autoprefixer-core')({browsers: 'last 2 versions'}), // voir https://github.com/ai/browserslist pour definir les browers supportés
require('cssnano')() // minify the result
]
},
Expand All @@ -108,16 +134,23 @@ module.exports = function(grunt) {
browserSync: {
dev: {
bsFiles: {
src : '<%= cfg.css %>/*.css'
src : ['<%= cfg.css %>/*.css', '<%= cfg.js %>/*.js']
},
options: {
watchTask: true,
proxy: {
target: "<%= cfg.site %>",
middleware: localCssMiddleware()
middleware: localMiddleware()
}
}
}
},
jshint: {
options: {
globals: {
jQuery: true
}
}
}
});

Expand All @@ -128,7 +161,7 @@ module.exports = function(grunt) {

// --sass=..., --css=... et --site=... sont prioritaires par rapport au fichier de configuration
var options = {};
['sass','css', 'site'].forEach(function(arg) {
['sass','css', 'site', 'js'].forEach(function(arg) {
if (grunt.option(arg)) {
options[arg] = grunt.option(arg);
}
Expand All @@ -142,17 +175,21 @@ module.exports = function(grunt) {
var cfg = grunt.config.get('cfg');
var sass = cfg.sass || grunt.fail.fatal("option --sass=<repertoire> manquant");
var css = cfg.css || grunt.fail.fatal("option --css=<repertoire> manquant");
var js = cfg.js || grunt.fail.fatal("option --js=<repertoire> manquant");
var site = cfg.site;

grunt.file.isDir(sass) || grunt.fail.fatal("repertoire '"+sass+"' inexistant");
grunt.file.isDir(css) || grunt.fail.warn("repertoire '"+css+"' inexistant");
grunt.file.isDir(js) || grunt.fail.warn("repertoire '"+js+"' inexistant");

grunt.log.write("Répertoire source : ").writeln(sass.cyan);
grunt.log.write("Répertoire destination : ").writeln(css.cyan);
grunt.log.write("Site web : ").writeln(site ? site.cyan : "non défini".yellow);
grunt.log.write("Répertoire source : ").writeln(sass['cyan']);
grunt.log.write("Répertoire destination : ").writeln(css['cyan']);
grunt.log.write("Répertoire JS : ").writeln(js['cyan']);
grunt.log.write("Site web : ").writeln(site ? site['cyan'] : "non défini"['yellow']);

grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-postcss');
grunt.loadNpmTasks('grunt-express-server');
grunt.loadNpmTasks('grunt-browser-sync');
Expand All @@ -161,4 +198,4 @@ module.exports = function(grunt) {
grunt.registerTask('serve', ['express', 'default']);
grunt.registerTask('bs', ['browserSync', 'default']);

};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"express": "^4.13.3",
"grunt": "^0.4.5",
"grunt-browser-sync": "^2.1.3",
"grunt-contrib-jshint": "^0.11.3",
"grunt-contrib-watch": "^0.6.1",
"grunt-express-server": "^0.5.1",
"grunt-postcss": "^0.5.5",
Expand Down