grunt watch/sass not working correctly - linux

It is my first time trying to create a workflow with grunt I have manage to make almost all work but when I run grunt the watch task for sass does nothing just outputs that the file has changed and thats it no error code nothing
gruntfile.js:
'use strict';
module.exports = function(grunt) {
// All configuration goes here
grunt.initConfig({
//jekyll tasks
jekyll: {
//Builds jekyll to local development
dist: {
options: {
config: '_config_dev.yml',
dest: './_local',
}
},
//Builds jekyll for deploying
live: {
options: {
config: '_config.yml',
dest: './_live',
}
}
},
//jshint task
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: [
'./!Gruntfile.js',
'./assets/js/*.js',
'./assets/js/plugins/*.js',
'./!assets/js/scripts.min.js'
]
},
// Uglify
uglify: {
dist: {
files: {
'./assets/js/scripts.min.js': [
'./assets/js/plugins/*.js',
'./assets/js/_*.js'
]
}
}
},
//imagemin task
imagemin: {
dist: {
options: {
optimizationLevel: 7,
progressive: true
},
files: [{
expand: true,
cwd: './img/',
src: '{,*/}*.{png,jpg,jpeg}',
dest: './img/'
}]
}
},
//svgmin task
svgmin: {
dist: {
files: [{
expand: true,
cwd: './img/',
src: '{,*/}*.svg',
dest: './img/'
}]
}
},
//Sass tasks
sass: {
//Compiles sass to css for local test
dist: {
files: [{
expand: true,
cwd: 'assets/sass/',
src: ['**/*.scss'],
dest: 'assets/css/',
ext: '.css'
}]
},
//Compiles sass to css for live deployment
live: {
options: {
style: 'compressed'
},
files: [{
expand: true,
cwd: 'assets/sass',
src: ['**/*.scss'],
dest: 'assets/css',
ext: '.css'
}]
},
//Compiles sass for browser sync
style: {
options: {
style: 'compressed'
},
files: [{
expand: true,
cwd: 'assets/sass',
src: ['**/*.scss'],
dest: 'assets/css',
ext: '.css'
}]
},
},
//Watch tasks
watch: {
scss: {
files: 'assets/sass/*.scss',
task: ['clean', 'sass:dist'],
options: {
spaw: false,
},
},
style: {
files: 'assets/sass/*.scss',
task: ['sass:style'],
},
js: {
files: [
'<%= jshint.all %>'
],
tasks: ['jshint','uglify'],
},
jekyll: {
files: [
'_layouts/*.html',
'_includes/*.html',
'assets/css/main.css',
'*.html',
'*.md',
'_post/*.md'
],
tasks: ['jekyll:dist'],
},
livereload: {
options: {
livereload: true,
},
files: [
'_local/**'
],
},
},
//Clean task
clean: {
dist: [
'assets/css/main.css',
'assets/js/scripts.min.js'
]
},
//Server
connect: {
server : {
options: {
livereload: true,
base : './_local/',
port: 4000
}
}
}
});
// Load the plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-svgmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-jekyll');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-clean');
// Custom tasks
grunt.registerTask('dev', ['clean', 'sass:dist', 'uglify', 'imagemin','svgmin', 'jekyll:dist', 'connect:server', 'watch']);
grunt.registerTask('default', ['clean', 'sass:live', 'uglify','imagemin', 'svgmin', 'jekyll:live']);
grunt.registerTask('css', ['clean', 'sass:dist', 'uglify', 'imagemin','svgmin', 'jekyll:dist', 'connect:server', 'watch:style']);
};
My file structure is the next:
gruntfile.js
assets/
css/
sass/
And this is the output of the terminal when running grunt dev task as you see nothing changes
┌[~/git/Nutricorp] [dev *]← →[*+?]
└[ % ± ]>> grunt dev -v O_O
Initializing
Command-line options: --verbose
Reading "Gruntfile.js" Gruntfile...OK
Registering Gruntfile tasks.
Initializing config...OK
Registering "grunt-contrib-watch" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-watch/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-watch/package.json...OK
Loading "watch.js" tasks...OK
+ watch
Registering "grunt-contrib-imagemin" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-imagemin/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-imagemin/package.json...OK
Loading "imagemin.js" tasks...OK
+ imagemin
Registering "grunt-contrib-connect" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-connect/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-connect/package.json...OK
Loading "connect.js" tasks...OK
+ connect
Registering "grunt-svgmin" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-svgmin/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-svgmin/package.json...OK
Loading "svgmin.js" tasks...OK
+ svgmin
Registering "grunt-contrib-jshint" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-jshint/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-jshint/package.json...OK
Loading "jshint.js" tasks...OK
+ jshint
Registering "grunt-contrib-uglify" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-uglify/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-uglify/package.json...OK
Loading "uglify.js" tasks...OK
+ uglify
Registering "grunt-jekyll" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-jekyll/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-jekyll/package.json...OK
Loading "jekyll.js" tasks...OK
+ jekyll
Registering "grunt-contrib-sass" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-sass/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-sass/package.json...OK
Loading "sass.js" tasks...OK
+ sass
Registering "grunt-contrib-clean" local Npm module tasks.
Reading /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-clean/package.json...OK
Parsing /home/cruznick/git/Nutricorp/node_modules/grunt-contrib-clean/package.json...OK
Loading "clean.js" tasks...OK
+ clean
Loading "Gruntfile.js" tasks...OK
+ css, default, dev
Running tasks: dev
Running "dev" task
Running "clean" task
Running "clean:dist" (clean) task
Verifying property clean.dist exists in config...OK
Files: assets/css/main.css -> dist
Options: force=false, no-write=false
Options: force=false, no-write=false
Cleaning assets/css/main.css...OK
Running "sass:dist" (sass) task
Verifying property sass.dist exists in config...OK
Files: assets/sass/_config.scss -> assets/css/_config.css
Files: assets/sass/_grids.scss -> assets/css/_grids.css
Files: assets/sass/_main-page.scss -> assets/css/_main-page.css
Files: assets/sass/_menu.scss -> assets/css/_menu.css
Files: assets/sass/main.scss -> assets/css/main.css
Options: (none)
Writing assets/css/main.css...OK
File assets/css/main.css created.
Running "uglify" task
Running "uglify:dist" (uglify) task
Verifying property uglify.dist exists in config...OK
Files: [no src] -> ./assets/js/scripts.min.js
Options: banner="", footer="", compress={"warnings":false}, mangle={}, beautify=false, report="min"
>> Destination ./assets/js/scripts.min.js not written because src files were empty.
Running "imagemin" task
Running "imagemin:dist" (imagemin) task
Verifying property imagemin.dist exists in config...OK
Options: cache, optimizationLevel=7, progressive
Minified 0 images (saved 0 B)
Running "svgmin" task
Running "svgmin:dist" (svgmin) task
Verifying property svgmin.dist exists in config...OK
Options: (none)
Total saved: 0 B
Running "jekyll:dist" (jekyll) task
Verifying property jekyll.dist exists in config...OK
File: [no files]
Options: config="_config_dev.yml", dest="./_local"
`jekyll build --destination ./_local --config _config_dev.yml` was initiated.
Jekyll output:
Configuration file: _config_dev.yml
Source: /home/cruznick/git/Nutricorp
Destination: ./_local
Generating... done.
Running "connect:server" (connect) task
Verifying property connect.server exists in config...OK
File: [no files]
Options: protocol="http", port=4000, hostname="0.0.0.0", base="./_local/", directory=null, keepalive=false, debug=false, livereload, open=false, useAvailablePort=false, middleware=null
Started connect web server on http://0.0.0.0:4000
Running "watch" task
Waiting...Verifying property watch exists in config...OK
Verifying property watch.scss.files exists in config...OK
Verifying property watch.style.files exists in config...OK
Verifying property watch.js.files exists in config...OK
Verifying property watch.jekyll.files exists in config...OK
Live reload server started on port: 35729
Watching assets/sass/_config.scss for changes.
Watching assets/sass/_grids.scss for changes.
Watching assets/sass/_main-page.scss for changes.
Watching assets/sass/_menu.scss for changes.
Watching assets/sass/main.scss for changes.
Watching assets/sass/_config.scss for changes.
Watching assets/sass/_grids.scss for changes.
Watching assets/sass/_main-page.scss for changes.
Watching assets/sass/_menu.scss for changes.
Watching assets/sass/main.scss for changes.
Watching _layouts/main.html for changes.
Watching _includes/head.html for changes.
Watching _includes/menu.html for changes.
Watching assets/css/main.css for changes.
Watching index.html for changes.
Watching .git for changes.
Watching .sass-cache for changes.
Watching _includes for changes.
Watching _layouts for changes.
Watching _live for changes.
Watching _local for changes.
Watching _locales for changes.
Watching _plugins for changes.
Watching _posts for changes.
Watching assets for changes.
Watching img for changes.
Watching node_modules for changes.
Watching README.md for changes.
OK
>> File "assets/sass/_menu.scss" changed.
Live reloading assets/sass/_menu.scss...
Live reloading assets/sass/_menu.scss...
Completed in 0.002s at Thu Mar 27 2014 08:02:04 GMT-0600 (CST) - Waiting...
And I dont know why it doesnt work no error message any ideas thanks in advance
ps: also tried with grunt contrib compass and it didnt work i'm running archlinux with ruby 2.1.0p0 and sass and compass gems installed everything else works great
ps2: my shell is zsh if that is important

In your watch config you have task: instead of tasks:.
The key for specifying that property is plural.

Related

Grunt executes multiple times, creates infinite loop

We are working as on a project with a couple of other devs on GIT, using the same repo, it works fine with everyone else. However, when I run grunt it executes multiple times, seems to be running into an infinite loop, without me actually making any changes. It's only happening to me on a different computer
I am considering that maybe I need something else installed.
I deleted node, npm and reinstalled with homebrew which I'm also using for updates. Node v5.3.0, npm 3.3.12
Running Mavericks.
What am I missing?
Here's the loop:
Reloading watch config...
Running "watch" task
Waiting...
>> File "Gruntfile.js" changed.
>> File "js/all-js/bootstrap-hover-dropdown.min.js" changed.
>> File "js/all-js/site.js" changed.
Running "uglify:build" (uglify) task
>> 1 file created.
Done, without errors.
Completed in 3.740s at Tue Jan 05 2016 13:11:07 GMT-0500 (ECT) - Waiting...
[BS] File changed: js/site.min.js
Reloading watch config...
Running "watch" task
Waiting...
>> File "js/all-js/bootstrap.js" changed.
>> File "Gruntfile.js" changed.
Running "uglify:build" (uglify) task
>> 1 file created.
Done, without errors.
Completed in 1.869s at Tue Jan 05 2016 13:11:10 GMT-0500 (ECT) - Waiting...
[BS] File changed: js/site.min.js
>> File "js/all-js/bootstrap-hover-dropdown.min.js" changed.
Running "uglify:build" (uglify) task
>> 1 file created.
Done, without errors.
Completed in 1.885s at Tue Jan 05 2016 13:12:04 GMT-0500 (ECT) - Waiting...
[BS] File changed: js/site.min.js
Reloading watch config...
Running "watch" task
Waiting...
>> File "Gruntfile.js" changed.
Here's my Gruntfile.js:
module.exports = function(grunt) {
//Get all tasks from the package.json file
require("matchdep").filterDev("grunt-*").forEach(grunt.loadNpmTasks);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
/* Concurrent Task */
concurrent: {
watch: {
tasks: ['watch', 'compass:dist', 'browserSync'],
options: {
logConcurrentOutput: true
}
}
},
/* SASS task */
compass: {
dist: {
options: {
sassDir: ['SASS/'],
cssDir: ['css/'],
environment: 'development', /* development | production */
importPath: ['SASS/'],
outputStyle: 'compressed', /* expanded for development | compressed for production */
watch: true,
sourcemap: true
},
},
live: {
options: {
sassDir: ['SASS/'],
cssDir: ['css/'],
environment: 'production', /* development | production */
importPath: ['SASS/'],
outputStyle: 'compressed', /* expanded for development | compressed for production */
watch: false,
force: true,
},
},
},
/* Javascript Tasks */
uglify: {
// Uglify files
build: {
src: [
'js/all-js/bootstrap.js',
'js/all-js/site.js'
],
dest: 'js/site.min.js'
}
},
/* Run tasks when needed */
watch: {
js: {
files: ['js/all-js/*.js'],
tasks: ['uglify'],
options: { livereload: true }
},
gruntfile: {
files: ['Gruntfile.js'],
options: {reload: true}
}
},
/* Browser Reload with BrowserSync */
browserSync: {
bsFiles: {
src : [
'css/**/*.css',
'js/site.min.js',
'**/*.php'
]
},
}
});
// Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask('default', ['concurrent:watch']);
grunt.registerTask('live', ['compass:live']);
};
Here's my package.json file:
{
"name": "ourframework",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-compass": "*",
"grunt-browser-sync": "*",
"grunt-contrib-watch": "*",
"grunt-concurrent": "*",
"grunt-contrib-uglify": "*",
"matchdep": "*"
}
}
Look at the timestamps in the grunt output, eg:
Completed in 3.740s at Tue Jan 05 2016 13:11:10
Completed in 1.885s at Tue Jan 05 2016 13:12:04
Notice these compilations are a minute apart - you are running the grunt watch task. This will watch a set of specified files and re-run tasks if there are any changes.
There is no 'loop' - it will simply re-compile every time something changes, which is often desirable.
You can see your default task is set to concurrent:watch
grunt.registerTask('default', ['concurrent:watch']);
So if you just type grunt, it will run that task.

Is 'El Capitan's' rootless breaking old grunt configs?

After migrating to El Capitan, it seems users are experiencing an issue with grunt installations, possibly related to the rootless changes of El Capitan. In particular, running the grunt --force command results in EPERM errors. The workflow is as follows:
Assuming npm has been installed, navigate to the grunt directory with package.json and gruntfile.js and invoke grunt:
grunt --force
Example Gruntfile.js file contents:
module.exports = function(grunt) {
// All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
// Configuration for concatenating files goes here.
dist: {
src: [
'../js/libs/owl.carousel.js',
'../js/libs/jquery.actual.js',
'../js/libs/chosen.jquery.js',
'../js/libs/jquery.parallax.js',
'../js/src/common.js'
],
dest: '../js/pro/global.js',
},
},
uglify: {
build: {
src: '../js/pro/global.js',
dest: '../js/pro/global.min.js',
},
},
imagemin: {
dynamic: {
files: [{
expand: true,
cwd: '../img/src/',
src: ['**/*.{png,jpg,gif}'],
dest: '../img/pro/'
}]
}
},
compass: {
dev: {
options: {
sassDir: '../sass',
cssDir: '../css',
fontsDir: '../fonts',
imagesDir: '../img/',
images: '../img/',
javascriptsDir: '../js/pro',
//environment: 'development',
outputStyle: 'compressed',
relativeAssets: true,
httpPath: '.',
}
},
},
watch: {
scripts: {
files: ['../js/**/**.js'],
tasks: ['concat', 'uglify'],
options: {
spawn: true,
},
},
images: {
files: ['../img/src/**.{png,jpg,gif}'],
tasks: ['imagemin'],
options: {
spawn: true,
}
},
compass: {
files: ['../**/*.{scss,sass}'],
tasks: ['compass:dev'],
}
},
svgstore: {
defaults: {
options: {
prefix : 'icon-',
},
files: {
'../img/svg-defs.svg': ['../img/svg/*.svg']
}
}
},
});
// Where we tell Grunt we plan to use this plug-in.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-svgstore');
// Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask('default', ['concat', 'uglify', /*'imagemin',*/ 'compass', 'svgstore', 'watch']);
};
Example package.json file contents:
{
"name": "Call Me Maybe",
"version": "0.2.0",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-compass": "^1.0.4",
"grunt-contrib-concat": "^0.5.1",
"grunt-contrib-imagemin": "^0.9.4",
"grunt-contrib-sass": "^0.9.2",
"grunt-contrib-uglify": "^0.9.2",
"grunt-contrib-watch": "^0.6.1",
"grunt-svgstore": "^0.5.0"
}
}
The resulting EPERM errors are as follows:
Running "concat:dist" (concat) task
Warning: Unable to write "../js/pro/global.js" file (Error code: EPERM). Used --force, continuing.
Running "uglify:build" (uglify) task
Warning: Unable to write "../js/pro/global.min.js" file (Error code: EPERM). Used --force, continuing.
Running "compass:dev" (compass) task
Warning: Command failed: /bin/sh: compass: command not found. Used --force, continuing.
Warning: You need to have Ruby and Compass installed and in your system PATH for this task to work. More info: https://github.com/gruntjs/grunt-contrib-compass Used --force, continuing.
Running "svgstore:defaults" (svgstore) task
Warning: Unable to write "../img/svg-defs.svg" file (Error code: EPERM). Used --force, continuing.
Running "watch" task
Waiting...
Interestingly, Ruby and Compass are also installed, so it does align with the theory of the rootless unable to write to folders issues, but how can the dependency cores be moved elsewhere (i.e. /usr/local/bin) so this isn't an issue?
During the El Capitan Betas, some users suggested enabling root via terminal - though this seemingly no longer works, as the error persists and /usr/bin folder still doesn't allow for permission changes.
For those running into the same issue, I had to eliminate the use of binaries installed to the path: /usr/bin, and reinstall after updating the path /usr/local/bin. Ruby tended to be the primary culprit. Because I was struggling with locating all my ruby installs, I ended up installing rbenv to manage my ruby versions.
The following terminal commands may be helpful in identifying your problematic paths:
which ruby
gem environment
gem uninstall [insert gem name here]
[google how to set your paths to /usr/local/bin... (will be in a hidden file)]
gem install [insert gem name here]
Installing non-system software on /usr/bin is a bad move, and is now prohibited in El Capitan, and with good reason.
I am not familiar with grunt, but if you can get to use /usr/local/bin instead then probably everything works.

Grunt: How do I run seperate processes for CSS (sass, concat, minify) and JS (concat, minify)

I'm looking at the grunt watch documentation but I can see how to run a separate process for my javascript files. Below is what I have for CSS:
GruntFile.js
module.exports = function(grunt) {
grunt.initConfig({
// running `grunt sass` will compile once
sass: {
dist: {
options: {
style: 'expanded'
},
files: {
'./public/css/sass_styles.css': './src/sass/sass_styles.scss' // 'destination': 'source'
}
}
},
// bring in additonal files that are not part of the sass styles set
concat: {
dist: {
src: [
'public/css/datepicker.css',
'public/css/jquery.tagsinput.css',
'public/css/sass_styles.css',
'application/themes/japantravel/style.css'
],
dest: 'public/css/all.css',
},
},
// running `grunt cssmin` will minify code to *.min.css file(s)
cssmin: {
minify: {
expand: true,
cwd: "public/css/",
src: ["all.css", "!*.min.css"],
dest: "public/css/",
ext: ".min.css"
}
},
// running `grunt watch` will watch for changes
watch: {
files: ["./src/sass/*.scss", "./src/sass/partials/*.scss"],
tasks: ["sass", "concat", "cssmin"]
}
});
// load tasks
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks("grunt-contrib-cssmin");
grunt.loadNpmTasks("grunt-contrib-watch");
};
As you can see I have tasks for CSS ["sass", "concat", "cssmin"], but I want to do separate tasks for separate files (js) - concat and minify - and listen for changes (watch). Can someone point me in the correct direction, I'm not really sure what I should be searching for. Is this something that watch can handle, or is there another plugin? I'm a little new to grunt so still trying to figure out how to use it. Thanks
You can use 'grunt-concurrent' for that, you can define multiple tasks with it. In combination with watch sets you will have the proper solution. https://github.com/sindresorhus/grunt-concurrent
# to install:
npm install grunt-concurrent --save-dev
And this will be your adjusted function then.
Remember, you still have to set some uglify and jshint properties! But I believe that's not the issue here.
module.exports = function(grunt) {
grunt.initConfig({
/* .. */
// running `grunt watch` will watch for changes
watch: {
// Use 'sets' like this, just make up a name for it:
watchCss: {
files: ["./src/sass/*.scss", "./src/sass/partials/*.scss"], // Directory to look for changes
tasks: ["concurrent:taskCss"] // Tasks you want to run when CSS changes
},
watchJs: {
files: ["./src/js/**/*.js"], // Directory to look for changes
tasks: ["concurrent:taskJs"] // Tasks you want to run when JS changes
}
},
concurrent: {
taskCss: ["sass", "concat", "cssmin"], // define the CSS tasks here
taskJs: ["jshint", "concat", "uglify"] // define the JS tasks here
},
});
// load tasks
grunt.loadNpmTasks("grunt-contrib-sass");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-cssmin");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks('grunt-contrib-jshint'); // Added
grunt.loadNpmTasks('grunt-contrib-uglify'); // Added
grunt.loadNpmTasks("grunt-concurrent"); // Added
// register tasks (note: you can execute sets from concurrent)
grunt.registerTask('default', ["concurrent:taskCss", "concurrent:taskJs"]);
grunt.registerTask('css', ["concurrent:taskCss"]);
grunt.registerTask('js', ["concurrent:taskJs"]);
};
To watch for changes:
grunt watch
# if a css file is changed, only the css tasks are performed
You can also execute a task from the prompt directly, for example:
grunt js
# This will only execute the registered task 'js'
# In this case that task points to 'concurrent:taskJs' wich will run jshint, concat and uglify
To install uglify and jshint:
https://github.com/gruntjs/grunt-contrib-uglify
https://github.com/gruntjs/grunt-contrib-jshint
npm install grunt-contrib-uglify --save-dev
npm install grunt-contrib-jshint --save-dev

How to use require.js and devcode grunt task together in build? (yeoman config)

Hierarchy:
App
.tmp // temp files
app // source files
dist // dist files
So if I put the devcode:build before requirejs:
Files from "app/scripts" are processed and saved into ".tmp/scripts"
Requirejs will be pointed to load the ".tmp/scripts"
Then fails because bower_components are not found at "bower_components"; Of course, because bower_components are located in "app/bower_components"
If I inverse the order:
Requirejs removes the comments and devcode doesn't work
I will remove require.js optimizer and then my build is not ok. Should I pass another uglify over it.
Any better solution? (don't make the pc to copy bower_components all over again, or I might change the position up to the root?)
Thanks
Well I don't need the devcode grunt task becasue requirejs already comes with uglify2 which allows you to have the dist.options.uglify2.compress.global_defs
If you put DEBUG = false then this code will be removed in production mode.
dist: {
options: {
baseUrl : '<%= yeoman.app %>/scripts/',
name : 'main',
mainConfigFile : '<%= yeoman.app %>/scripts/main.js',
out : '.tmp/concat/scripts/main.js',
optimize : 'uglify2',
uglify2: {
//Example of a specialized config. If you are fine
//with the default options, no need to specify
//any of these properties.
output: {
beautify: false
},
compress: {
global_defs: {
DEBUG: false
}
},
warnings : true,
mangle : true
},
}
},

Ember.js - Handlebars not recognized if required via Grunt (Node.js)

is anybody else using Grunt as build tool for the Ember web application and is experiencing the same behaviour as I do? At this time, I'm using the framework in version RC3 and I can use my build tool without hassle and import all necessary libraries, uglify and compress them and everything works like a charm.
Anyhow, at least since Ember RC5 I'm not able to use Grunt for building my application anymore as Ember would not recognize Handlebars anymore!. It's always complaining that Ember Handlebars requires Handlebars version 1.0.0-rc.4. Include a SCRIPT tag in the HTML HEAD linking to the Handlebars file before you link to Ember. and right afterwards it says Cannot read property 'COMPILER_REVISION' of undefined which leads me to the assumption that Ember is not recognizing the included Handlebars library.
I haven't changed anything in my app.js (the order of the libraries/frameworks is untouched) except the references to the js files (using Ember RC5/6 instead of RC3 and Handlebars RC4 instead of RC3). But it seems that something breaks the initialization of Ember.Handlebars since then...
Do I get something wrong here? Is there a solution out there so that I can continue using Grunt as build tool?
EDIT
Here's my Gruntfile.js:
/*jshint camelcase: false */
/*global module:false */
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
meta: {
dev: {
buildPath: '.'
},
prod: {
buildPath: '.'
}
},
/*
Task for uglifyng the application javascript file in production environment
*/
uglify: {
options: {
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */'
},
prod: {
files: [
{
src: '<%= meta.prod.buildPath %>/js/application.js',
dest: '<%= meta.prod.buildPath %>/js/application.min.js'
}
]
}
},
/*
Task for creating css files out of the scss files
*/
compass: {
prod: {
options: {
environment: 'production',
noLineComments: true,
outputStyle: 'expanded',
cssDir: '<%= meta.prod.buildPath %>/css',
fontsDir: '<%= meta.prod.buildPath %>/fonts',
imagesDir: '<%= meta.prod.buildPath %>/images',
javascriptsDir: '<%= meta.prod.buildPath %>/js'
}
},
dev: {
options: {
environment: 'development',
noLineComments: false,
outputStyle: 'expanded',
cssDir: '<%= meta.dev.buildPath %>/css',
fontsDir: '<%= meta.dev.buildPath %>/fonts',
imagesDir: '<%= meta.dev.buildPath %>/images',
javascriptsDir: '<%= meta.dev.buildPath %>/js'
}
}
},
/*
Task to minify all css files in production mode.
All css files will end with '.min.css' instead of
just '.css'.
*/
cssmin: {
minify: {
expand: true,
cwd: '<%= meta.prod.buildPath %>/css/',
src: ['*.css', '!*.min.css'],
dest: '<%= meta.prod.buildPath %>/css/',
ext: '.min.css'
}
},
/*
Clean up the production build path
*/
clean: {
cssd: ['<%= meta.prod.buildPath %>/css/**/*']
},
/*
A simple ordered concatenation strategy.
This will start at app/app.js and begin
adding dependencies in the correct order
writing their string contents into 'application.js'
Additionally it will wrap them in evals
with # sourceURL statements so errors, log
statements and debugging will reference
the source files by line number.
This option is set to false for production.
*/
neuter: {
prod: {
options: {
includeSourceURL: false
},
files: [
{
src: 'app/app.js',
dest: '<%= meta.prod.buildPath %>/js/application.js'
}
]
},
dev: {
options: {
includeSourceURL: true
},
files: [
{
src: 'app/app.js',
dest: '<%= meta.dev.buildPath %>/js/application.js'
}
]
}
},
/*
Watch files for changes.
Changes in dependencies/ember.js or application javascript
will trigger the neuter task.
Changes to any templates will trigger the ember_templates
task (which writes a new compiled file into dependencies/)
and then neuter all the files again.
*/
watch: {
application_code: {
files: ['js/dependencies/ember.js', 'app/**/*.js'],
tasks: ['neuter:dev']
},
compass: {
files: [
'styles/**/*.scss'
],
tasks: ['compass:dev']
}
},
/*
Runs all .html files found in the test/ directory through PhantomJS.
Prints the report in your terminal.
*/
qunit: {
all: ['test/**/*.html']
},
/*
Reads the projects .jshintrc file and applies coding
standards. Doesn't lint the dependencies or test
support files.
*/
jshint: {
all: ['Gruntfile.js', 'app/**/*.js', 'test/**/*.js', '!js/dependencies/*.*', '!test/support/*.*'],
options: {
jshintrc: '.jshintrc'
}
},
/*
Generate the YUI Doc documentation.
*/
yuidoc: {
name: '<%= pkg.name %>',
description: '<%= pkg.description %>',
version: '<%= pkg.version %>',
options: {
paths: '<%= meta.dev.buildPath %>/app/',
outdir: '<%= meta.dev.buildPath %>/yuidocs/'
}
},
/*
Find all the <whatever>_test.js files in the test folder.
These will get loaded via script tags when the task is run.
This gets run as part of the larger 'test' task registered
below.
*/
build_test_runner_file: {
all: ['test/**/*_test.js']
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-neuter');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-yuidoc');
/*
A task to build the test runner html file that get place in
/test so it will be picked up by the qunit task. Will
place a single <script> tag into the body for every file passed to
its coniguration above in the grunt.initConfig above.
*/
grunt.registerMultiTask('build_test_runner_file', 'Creates a test runner file.', function () {
var tmpl = grunt.file.read('test/support/runner.html.tmpl');
var renderingContext = {
data: {
files: this.filesSrc.map(function (fileSrc) {
return fileSrc.replace('test/', '');
})
}
};
grunt.file.write('test/runner.html', grunt.template.process(tmpl, renderingContext));
});
/*
A task to run the application's unit tests via the command line.
It will
- convert all the handlebars templates into compile functions
- combine these files + application files in order
- lint the result
- build an html file with a script tag for each test file
- headlessy load this page and print the test runner results
*/
grunt.registerTask('test', ['neuter', 'jshint', 'build_test_runner_file', 'qunit']);
/*
Configures all tasks which will be executed with production setup
*/
grunt.registerTask('prod_tasks', ['clean', 'compass:prod', 'cssmin', 'neuter:prod', 'uglify:prod']);
/*
Setup for the production build. Sets the production build path.
*/
grunt.registerTask('prod', 'Production Build', function () {
grunt.task.run('prod_tasks');
});
/*
Configures all tasks which will be executed with development setup
*/
grunt.registerTask('dev_tasks', ['compass:dev', 'neuter:dev', 'watch']);
/*
Setup for the development build. Sets the development build path.
*/
grunt.registerTask('dev', 'Development Build', function () {
grunt.task.run('dev_tasks');
});
// Default task
grunt.registerTask('default', 'dev');
/*
Configures all tasks which will be executed with doc setup
*/
grunt.registerTask('doc_tasks', ['yuidoc']);
/*
Setup for the YUI doc generation.
*/
grunt.registerTask('doc', 'Generate YuiDoc Documentation for the App', function () {
grunt.task.run('doc_tasks');
});
};
EDIT 2
I took the ember-1.0.0-rc.6.js and handlebars-1.0.0-rc.4.js files from the starter kit at the Ember.js website and tried to run the Grunt tasks on it. Here's what Chrome is telling me:
EDIT 3
Just in case if anybody cares, here's the link to the raised issue over at the Ember.js Github page: https://github.com/emberjs/ember.js/issues/2894
EDIT 4
Finally, the issue was identified to be a Handlebars inconsistency when dealing with global exports, like #Tao reported in his answer. Here's the link to the Issue on GitHub if you want to follow: https://github.com/wycats/handlebars.js/issues/539
It looks like this issue is being addressed in the next version of Handlebars: https://github.com/wycats/handlebars.js/issues/539 Stay tuned.
You have a mismatch in the version used to compile the Handlebars templates and the version included via the script tag.
If you are using grunt-contrib-handlebars, it uses the npm module handlebars to compile the the templates. The handlebars module/project is independent of Ember and has its own revisions that may or not be compatible with Ember.
To maintain compatibility with Handlebars Ember needs specific versions of handlebars which it is warning you about.
The tricky part here is you need to ensure that grunt-contrib-handlebars is forced to use that specific version of handlebars.
Solution 1: Use shrinkwrap to change grunt-contrib-handlebars's handlebars dependency version.
Solution 2: This is what I am currently using. I have switched to Emblem. The emblem grunt task asks for your handlebars file explicitly so you don't have to drop down to node sub dependency management. And your build includes the same file into your script tags, thus avoiding duplication/mismatch for future revisions.
Edit: After Gruntfile edit
Looking at the Gruntfile I don't see anything amiss. Looks like a standard build process, js -> neuter -> (if prod) -> uglify etc.
I think you need to try refreshing both emberjs and handlebars js files. Try using the files from the starter kit itself, those definitely work together.
And verify this for your index.html by looking at the unminified source in Chrome/Inspector. Handlebars has the revision numbers below the banner something like Handlebars.VERSION and Handlebars.COMPILER_REVISION. Match those with what you see in the ember.js file, somewhere below #submodule ember-handlebars-compiler in the Ember.assert.

Resources