Grunt: "Uglification failed" - node.js

I'm trying to use a gruntfile to uglify my JS and CSS. The one problem is that uglify doesn't work, the terminal gives a very vague error...
Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify:
{
js:
{
files: { 'compressed/javascript.min.js': ['js/*.js'] }
},
css:
{
files: { 'compressed/css.min.css': ['css/*.css'] }
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// Default task(s).
grunt.registerTask('default', ['uglify']);
};
package.json
{
"name": "my-project-name",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-uglify": "~0.2.2",
"grunt-contrib-imagemin": "~0.3.0"
}
}
Terminal output:
MacBook-Pro-van-Valerie:grunt valerieeskens$ grunt
Running "uglify:js" (uglify) task
>> Uglifying source "js/javascript1.js,js/javascript2.js" failed.
Warning: Uglification failed. Use --force to continue.
Aborted due to warnings.
MacBook-Pro-van-Valerie:grunt valerieeskens$
Map structure: http://d.pr/i/CxVJ

Uglify is for JavaScript, not CSS. Use grunt-contrib-cssmin if you want to compress your CSS files. The problem here seems to be the JS itself though, and I've tried that pattern in my Uglify config and it works OK. Have you run JSHint on your JavaScript source files? It may be a problem with the input itself.

Uglify is only for JavaScript, so you should use another Grunt plugin to minify your CSS. However, even then your javascript needs to be valid. Otherwise the minification will fail. For example the following will not pass:
var name = "Steve;
or
if(condition1 &&) {
// ...
}

Related

How to write a grunt-shell task which works cross-platform?

I'm trying to write a grunt task which removes a directory and its contents.
Here is config file
{
"name": "lorem",
"homepage": "ipsum",
"version": "0.9.5",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-shell": "~0.5.0",
}
}
and grunt.js file
// Whole Gruntfile.js so far
module.exports = function(grunt) {
// 1. All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
shell: {
clean: {
command: 'rm dist -r',
options: { stdout: true, failOnError: true }
}
}
});
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['shell:clean']);
};
This probably work on linux, but what if the machine is windows or osx?
Is there any cross-platform solution to this?
Shell commands uses the system shell, which differs between OSes. If you want something cross-platform, just do it in JS. As for your use-case you could use grunt-contrib-clean, trash, or rimraf.

grunt throw "Recursive process.nextTick detected"

I'm running Lion 10.9.2 with nodejs v0.10.26
I want to setup an automated compilation on sass files and a live reload with grunt, nothing complicated but...
When running grunt watch I get the following error
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
util.js:35
var str = String(f).replace(formatRegExp, function(x) {
^
RangeError: Maximum call stack size exceeded
here is the Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
dist: {
files: {
'assets/css/styles.css': 'assets/sass/styles.scss'
}
}
},
watch: {
all: {
files: 'index.html', // Change this if you are not watching index.html
options: {
livereload: true // Set livereload to trigger a reload upon change
}
},
css: {
files: [ 'assets/sass/**/*.scss' ],
tasks: [ 'sass' ],
options: {
spawn: false
}
},
options: {
livereload: true // Set livereload to trigger a reload upon change
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.registerTask('watch', [ 'watch']);
grunt.registerTask('default', [ 'sass', 'watch' ]);
};
and here is the package.json
{
"name": "application",
"version": "0.0.1",
"private": true,
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-sass": "~0.7.3"
}
}
I finally figured out a similar problem I was having with SASS. I was using
grunt.registerTask('sass', [ 'sass']);
The trick was that Grunt doesn't seem to like the repetition in names. When I switch to
grunt.registerTask('styles', [ 'sass']);
Everything worked as it should.
Just had this problem. Resolved it by removing grunt.registerTask('watch', [ 'watch']);
I just fixed a similar error "Recursive process.nextTick detected" causing by command: grunt server
The solution? Use sudo grunt serve instead
you could try this one, it fixed the issue for me, working with Yeoman 1.3.3 and Ubuntu 14.04 Grunt watch error - Waiting...Fatal error: watch ENOSPC
I was getting error in even trying to install grunt. Running npm dedupe solved my problem as answered here: Grunt watch error - Waiting...Fatal error: watch ENOSPC
Alternative solution: check your watch for an empty file argument.
Here's an excerpt of my gruntfile
watch: {
all: {
options:{
livereload: true
},
files: ['src/scss/*.scss', 'src/foo.html',, 'src/bar.html'],
tasks: ['default']
}
}
In my case, I could recreate the original poster's error on demand with the empty argument above.

grunt bowercopy task not found

I can't figure out my configuration problem. When I try to run 'grunt bowercopy', I get this error message:
Warning: Task "bowercopy" not found. Use --force to continue.
If I run 'grunt jshint', jshint works fine.
Here is my package.json:
{
"name": "treblebull",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "~3.2.6",
"jade": "~0.31.2",
"underscore": "~1.5.2",
"pg": "~2.11.1"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-bowercopy": "~0.7.1",
"grunt-contrib-jshint": "~0.8.0",
"load-grunt-tasks": "~0.2.1"
}
}
and here is my gruntfile:
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
lib: {
src: ['lib/**/*.js']
},
test: {
src: ['test/**/*.js']
}
},
bowercopy: {
options: {
clean: true
//srcPrefix: 'bower_components'
},
libs: {
options: {
// destPrefix: 'public/js/lib'
},
files: {
'angular.js': 'angular/angular.js'
//'underscore.js': 'underscore/underscore.js',
//'underscore.string.js': 'underscore.string/underscore.string.js'
}
}
}
});
grunt.loadNpmTasks('grunt-bowercopy');
grunt.loadNpmTasks('grunt-contrib-jshint');
};
Run bower init to give yourself a bower.json file for the bowercopy task to read. Also if you already have installed everything via bower, set runBower to false in your options hash.
If you're ever having Grunt failures, it's worth running with the --v (verbose) flag to see exactly what it's failing on. Running this myself I saw it looking for a bower.json, and once I supplied one the task succeeded.
You're missing task registration, You need to register a task that you want to explicity run in grunt, so you need this
grunt.registerTask('bowercopy', ['bowercopy']);
Then you can run
grunt bowercopy
Since I can't comment on #dcodesmith's answer due to points, I have to leave an answer. I ran into the problem in that actually adding grunt.registerTask('bowercopy', ['bowercopy']);
called bowercopy's task, but it doesn't actually work. Removing it actually allowed bowercopy to copy files.

Arguments to path.resolve must be strings when running Grunt

My Grunt file:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
ts: {
dev: {
src: ["src/background/*.ts"],
out: ["build/background.js"],
}
}
});
grunt.loadNpmTasks("grunt-ts");
grunt.registerTask("default", ["ts:dev"]);
};
(I am using grunt-ts.)
System info
Windows 8.1
NodeJS v0.10.24
grunt-cli v0.1.11
grunt v0.4.2
I've already searched the Internet and found many resources about this error, but they all say that one should upgrade NodeJS and/or Grunt. I've already tried that. I had even completely re-installed Grunt, however, the error remained.
The complete error message:
P:\my-folder>grunt ts
Running "ts:dev" (ts) task
Warning: Arguments to path.resolve must be strings Use --force to continue
Aborted due to warnings.
package.json
{
"name": "regex-search",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-uglify": "~0.2.2",
"grunt-ts": "~1.5.1"
}
}
After comparing my Gruntfile with the officially provided sample file, I found my really silly mistake:
ts: {
dev: {
src: ["src/background/*.ts"],
out: ["build/background.js"],
}
}
out must not be an array!
The correct version:
ts: {
dev: {
src: ["src/background/*.ts"],
out: "build/background.js",
}
}
So in my particular case, a node module's main attribute in package.json was an array and not a string, example in history.js' package.json:
{
"main": [ './history.js', './history.adapter.ender.js' ]
}
The way I found this out was going to where the error originated in my node_modules and then did console.log(pkg.main) right above it.
Original stacktrace:
Fatal error: Arguments to path.resolve must be strings
TypeError: Arguments to path.resolve must be strings
at Object.posix.resolve (path.js:422:13)
at /Users/ebower/work/renvy/node_modules/browserify/node_modules/resolve/lib/async.js:153:38
at fs.js:336:14
at /Users/ebower/work/renvy/node_modules/grunt-browserify/node_modules/watchify/node_modules/chokidar/node_modules/readdirp/node_modules/graceful-fs/graceful-fs.js:104:5
at /Users/ebower/work/renvy/node_modules/grunt-mocha/node_modules/mocha/node_modules/glob/node_modules/graceful-fs/graceful-fs.js:104:5
at FSReqWrap.oncomplete (fs.js:99:15)

Grunt warning: No source files found

I am trying to use Grunt for the first time. I think that I'm properly following the directions to install and use Grunt with a plugin (grunt-text-replace). (See, for instance, Grunt's page and the plugin's.) But I can't successfully run anything -- instead, I keep getting the same error. I've been checking my code against the instructions from both Grunt and the plugin, but I can't see anything I did wrong.
Here is my package.json file:
{
"name": "brink-prototype",
"version": "0.0.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt-text-replace": "~0.3.2"
}
}
And here is my Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
replace: {
src: ['components/bootstrap/less/navbar.less'],
dest: 'build/',
replacements: [{
from: /\.box-shadow.*$/g,
to: ''
}]
}
});
grunt.loadNpmTasks('grunt-text-replace');
grunt.registerTask('default', ['replace']);
};
When I run "grunt" in the command line, I get the following error:
Running "replace:src" (replace) task
Warning: No source files found Use --force to continue.
Aborted due to warnings.
I also tried this process with another plugin (grunt-regex-replace) and had exactly the same error message.
Where have I gone wrong?
UPDATE:
Here are the relevant parts of the file structure:
project/
Gruntfile.js
package.json
components/
bootstrap/
less/
navbar.less
node_modules/
grunt/
grunt-text-replace/
I have been trying to run the command from the project/ directory, where the Gruntfile.js is.
Maybe the path in my src should be relative to something else? I don't know.
The grunt-text-replace plugin requires you to specify a subtask.
replace: {
aSubtaskName: {
src: ['components/bootstrap/less/navbar.less'],
dest: 'build/',
replacements: [{
from: /\.box-shadow.*$/g,
to: ''
}]
}
}

Resources