So I'm new to Grunt and have been trying to implement a workflow for it and Jekyll. So I have a watch task running but it seems to fail to interrupt the current serve task before trying to start Jekyll again because I get an error about port binding, specifically jekyll 2.0.3 | Error: Address already in use - bind(2)
It could be me doing something stupid or not understanding something but does anyone have any ideas? Here's my gruntfile:
module.exports = function( grunt ) {
// load time-grunt and all grunt plugins found in the package.json
require( 'time-grunt' )( grunt );
require( 'load-grunt-tasks' )( grunt );
grunt.initConfig({
csslint : {
test : {
options : {
import : 2
},
src : [ 'css/main.css' ]
}
},
cssmin : {
dist : {
src : 'css/main.css',
dest : 'css/main.min.css'
}
},
shell : {
jekyllBuild : {
command : 'glynn'
},
jekyllServe : {
command : 'jekyll serve'
}
},
watch : {
files : [ '_layouts/*.html',
'_posts/*.md',
'css/main.css',
'_config.yml',
'index.html',
'404.html' ],
tasks : [ 'cssmin',
'shell:jekyllServe' ],
options : {
spawn : true,
interrupt : true,
atBegin : true,
debounceDelay: 250
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-shell');
// register custom grunt tasks
grunt.registerTask( 'lintcheck', [ 'cssmin','csslint', 'shell:jekyllServe' ] )
grunt.registerTask( 'qbuild', [ 'cssmin', 'shell:jekyllServe' ] )
grunt.registerTask( 'deploy', [ 'cssmin', 'shell:jekyllBuild' ] )
};
Any help would be much appreciated.
You got an error because you tried to run jekyll serve more than once (while server was already running). As you can read in the docs, jekyll serve runs a development server at localhost:4000 (http://jekyllrb.com/docs/usage/) so you can fire it only one on a particular port at the same time. After that you need only rebuild jekyll into ./_site when some files changed. You can do that with jekyll build.
I modified your Gruntfile:
module.exports = function( grunt ) {
// load time-grunt and all grunt plugins found in the package.json
require( 'time-grunt' )( grunt );
require( 'load-grunt-tasks' )( grunt );
grunt.initConfig({
csslint : {
test : {
options : {
import : 2
},
src : [ 'css/main.css' ]
}
},
cssmin : {
dist : {
src : 'css/main.css',
dest : 'css/main.min.css'
}
},
shell : {
jekyllBuildLocal : {
command : 'jekyll build'
},
jekyllBuild : {
command : 'glynn'
},
jekyllServe : {
command : 'jekyll serve'
}
},
watch : {
files : [ '_layouts/*.html',
'_posts/*.md',
'css/main.css',
'_config.yml',
'index.html',
'404.html' ],
tasks : [ 'cssmin',
'shell:jekyllBuildLocal' ],
options : {
spawn : true,
interrupt : true,
atBegin : true,
debounceDelay: 250
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-shell');
// register custom grunt tasks
grunt.registerTask( 'lintcheck', [ 'cssmin','csslint', 'shell:jekyllBuildLocal' ] )
grunt.registerTask( 'qbuild', [ 'cssmin', 'shell:jekyllBuildLocal' ] )
grunt.registerTask( 'deploy', [ 'cssmin', 'shell:jekyllBuild' ] )
};
And what I recommend you for development is to run:
grunt shell:jekyllServe
grunt watch (I guess in other terminal window)
This should work properly
I'm developing a site with a similar workflow but with Gulp instead of Grunt. I swapped the default jekyll server with browserSync, which also auto-reloads pages when changes are made (useful in development). This is an example Gulp build file; a Grunt-equivalent can be made.
Related
so I am trying to get grunt working ( ive installed the relevant elements ) I'm not sure what settings i have wrong as, this is what comes up in the terminal after 'grunt watch'
$ grunt watch
Running "watch" task
Waiting...
it won't do anything until i hit save. then it will say a change has taken place.
i dont think this is how it's supposed to work.
my gruntfile is:
// Use "grunt --help" to list the available tasks
module.exports = function(grunt) {
grunt.initConfig({
sass: {
// this is the "dev" Sass config used with "grunt watch" command
dev: {
options: {
style: 'expanded',
},
files: {
// the first path is the output and the second is the input
'style.css': 'sass/style.scss',
'style_products.css': 'sass/style_products.scss',
'style-mega-menu.css': 'sass/style-mega-menu.scss'
}
},
// this is the "production" Sass config used with the "grunt default" command
dist: {
options: {
style: 'compressed',
},
files: {
'style.css': 'sass/style.scss',
'style_products.css': 'sass/style_products.scss',
'style-mega-menu.css': 'sass/style-mega-menu.scss'
}
}
},
// configure the "grunt watch" task
watch: {
sass: {
files: ['sass/*.scss', 'sass/**/*.scss',],
tasks: ['sass:dev']
}
}
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
// "grunt" is the same as running "grunt sass:dist".
grunt.registerTask('default', ['sass:dist']);
grunt.registerTask('dev', ['sass:dev']);
grunt.registerTask('default', ['watch',]);
};
can anyone pls help?
I am create gruntfile and install all needed dependencies.
In tutorial all worked fine but not for me.
If i try to run this gruntfile i see this error message:
Error: Cannot find module 'pixrem'
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
Where is mistake? If you can please correct the mistakes in the answer.
Gruntfile is here:
module.exports = function(grunt) {
grunt.initConfig({
pkg : grunt.file.readJSON('package.json'),
watch : {
images : {
files : ['images/src/**/*.{png,jpg,gif}'],
tasks : ['newer:imagemin']
}, // watch images added to src
deleting : {
files : ['images/src/*.{png,jpg,gif}'],
tasks : ['delete_sync']
}, // end of delete sync
scripts : {
files : ['js/libs/*.js', 'js/custom/*.js'],
tasks : ['concat', 'uglify'],
options : {
spawn : false,
}
}, //end of watch scripts
css : {
files : ['sass/**/*.scss'],
tasks : ['sass', 'postcss', 'penthouse'],
options : {
spawn : false,
}
}, //end of sass watch
grunt : {
files : ['gruntfile.js']
}
}, //end of watch
/* ====================================================================================================================================================
* ====================================================================================================================================================
Tasks
====================================================================================================================================================
====================================================================================================================================================
*/
delete_sync : {
dist : {
cwd : 'images/dist',
src : ['**'],
syncWith : 'images/src'
}
}, // end of delete sync
imagemin : {
dynamic : {
files : [{
expand : true, // Enable dynamic expansion
cwd : 'images/src/', // source images (not compressed)
src : ['**/*.{png,jpg,gif}'], // Actual patterns to match
dest : 'images/dist/' // Destination of compressed files
}]
}
}, //end imagemin
concat : {
dist : {
src : ['js/libs/*.js', 'js/custom/*.js'],
dest : 'js/build/production.js'
}
}, //end concat
uglify : {
dist : {
src : 'js/build/production.js',
dest : 'js/build/production.min.js'
}
}, //end uglify
sass : {
dist : {
options : {
style : 'nested', //no need for config.rb
compass : 'true'
},
files : {
'css/main.css' : 'sass/main.scss'
}
}
}, //end of sass
postcss : {
options : {
map : true,
processors : [require('pixrem')(), // add fallbacks for rem units
require('autoprefixer-core')({
browsers : 'last 2 version, IE 9'
}), // add vendor prefixes. for more: https://github.com/ai/browserslist
require('cssnano')() // minify the result
]
},
dist : {
src : 'css/main.css'
}
},
penthouse : {
extract : {
outfile : 'css/critical.css.php',
css : 'css/main.css',
url : 'http://localhost/grunt-boilerplate',
width : 1200,
height : 500
},
}, //end of penthouse
browserSync : {
dev : {
bsFiles : {
src : ['css/*.css', 'images/*.*', 'js/build/production.min.js', '*.php', 'includes/*.php', '!.sass-cache']
},
options : {
proxy : "localhost/grunt-boilerplate",
watchTask : true
}
}
},
ftpush : {
build : {
auth : {
host : 'ftp.yourwebsite.com',
port : 21,
authKey : 'key1' //ftp login is in the .ftppass file
},
src : './', //root
dest : '/www', //destination folder
exclusions : ['.sass-cache', '.git', 'images/src', 'node_modules', '.gitignore', '.ftppass', 'gruntfile.js', 'README.md', 'package.json'], //remember adding '.ftppass' to the exclusions in .gitignore if you are publishing the repo to github
// keep : ['blog','cv','projects'], // SUPER IMPORTANT! check what resources should STAY on the server, for example your wordpress installation or other subfolders you use for other projects. else they'll get wiped out
simple : false,
useList : false
}
}
});
// load npm tasks
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browser-sync');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-postcss');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-newer');
grunt.loadNpmTasks('grunt-delete-sync');
grunt.loadNpmTasks('grunt-penthouse');
grunt.loadNpmTasks('grunt-ftpush');
// define default task
grunt.registerTask('default', ["browserSync", "watch"]);
};
strong text
try npm i -S pixrem then retry your grunt command.
I can only find examples about using grunt-newer with jshint but not requirejs during searching. So I use this config in my grunt config file:
watch: {
options: {
nospawn: true
},
js: {
files: [
'public/**/*.js',
'!public/asset/**/*.js'
],
tasks: ['newer:requirejs']
},
},
requirejs: getRequirejsOptions(),
...
the requirejs options is like this:
module.exports = function () {
var options = {};
files.forEach(function (file) {
options[file] = {
options: {
baseUrl: path.join(__dirname, '../public'),
include: file,
create: true,
out: path.join(__dirname, '../public/asset/', file + '.js'),
optimize: process.env.NODE_ENV == 'local' ? 'none' : 'uglify',
preserveLicenseComments: !false,
packages: [
...
]
}
}
});
return options;
}
it did fire the grunt watch to run requirejs on only changed files:
$ grunt watch
Running "watch" task Waiting...
>> File "public/stock/js/main.js" changed.
Running "newer:shell:requirejs" (newer) task No newer files to process.
Running "watch" task Completed in 1.834s at Fri Jan 06 2017 12:08:05 GMT+0800 (CST) - Waiting...
but after refreshing browser, I found the source file not changed.
I have a little big problem with BrowserSync version for Grunt.
PROBLEM:
If I edit .php files - BrowserSync work good but if I edited .less files, nothing happened but in bash I can still see:
[BS] File changed: d:\localhost\htdocs\www\wordpress\wp-content\themes\MYPROJECTNAME\css\main.css
Can somebody please help me to setup correctly:
GRUNT + LESS + BrowserSync running on XAMPP (because of WordPress)?
My gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
// Watch task config
watch: {
less: {
files: ['less/**/*.less'],
tasks: ['less']
}
},
// less task config
less: {
dev: {
files: {
"/localhost/htdocs/www/wordpress/wp-content/themes/MYPROJECTNAME/css/main.css" // destination
:
"less/main.less" // source file
}
}
},
// Using the BrowserSync Proxy for your existing website url.
browserSync : {
dev : {
bsFiles : {
src : [
'/localhost/htdocs/www/wordpress/wp-content/themes/MYPROJECTNAME/**/*.php',
'/localhost/htdocs/www/wordpress/wp-content/themes/MYPROJECTNAME/images/*.*',
'/localhost/htdocs/www/wordpress/wp-content/themes/MYPROJECTNAME/_dev/less/**/*.less',
'/localhost/htdocs/www/wordpress/wp-content/themes/MYPROJECTNAME/**/*.css',
]
},
options : {
watchTask: true,
debugInfoě: true,
logConnections: true,
notify: true,
proxy: "localhost/www/wordpress",
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browser-sync');
// register a default task.
grunt.registerTask('default', ['less', 'browserSync', 'watch']);
};
Thanks all! :-)
Here is my setting
var root = 'localhost/htdocs/www/wordpress/wp-content/themes/MYPROJECTNAME/';
browserSync : {
dev : {
options : {
files: [root+'style.css',
root+'js/**/*.js',
root+'**/*.php'],
watchTask: true,
debugInfoe: true,
logConnections: true,
notify: true,
proxy: "localhost/www/wordpress",
}
}
}
});
I need to see a stack trace when a test fails, but something is hiding it when Grunt runs the Mocha test suite. When I run the tests myself (mocha --recursive) I do get a stack trace:
site/server/server.js:10
server.use( express.static( path.join( __dirname( '../client' ))));
^
TypeError: string is not a function
at ...
But with this Gruntfile:
'use strict';
var should = require( 'should' );
module.exports = function( grunt ) {
grunt.initConfig({
cafemocha: {
test: {
src: 'server/test/**/test-*.js',
options: {/*
ui: 'bdd',
growl: true,
coverage: true,
reporter: 'spec'
*/}
}
},
watch: {
files: [
'server/**/*.js',
'Gruntfile.js',
'package.json'
],
tasks: [ 'test' ]
},
complexity: {
generic: {
src: [
'server/**/*.js',
'Gruntfile.js'
],
options: {
cyclomatic: 2,
halstead: 9,
maintainability: 80
}
}
}
});
grunt.loadNpmTasks( 'grunt-notify' );
grunt.loadNpmTasks( 'grunt-contrib-watch' );
grunt.loadNpmTasks( 'grunt-cafe-mocha' );
grunt.loadNpmTasks( 'grunt-complexity' );
grunt.registerTask( 'default', [ 'cafemocha' ]);
grunt.registerTask( 'test', [ 'cafemocha', 'complexity' ]);
};
All I get is a summary of an error:
$ grunt test
Running "cafemocha:test" (cafemocha) task
Warning: string is not a function Use --force to continue.
Aborted due to warnings.
Stack traces are hidden by default in grunt. Run grunt with grunt --stack to show them.