why cant I run this simple defined task with grunt? :
copy: {
templates: {
files: [{
expand: true,
cwd: ['src/tpl'],
src: ['**/*.tpl'],
dest: 'dist/assets/tpl'
}]
}
}
but when i try to run that task i get this warning:
$ grunt copy:templates
Running "copy:templates" (copy) task
Verifying property copy.templates exists in config...ERROR
>> Unable to process task.
Warning: Required config property "copy.templates" missing. Use --force to continue.
Aborted due to warnings.
is a very simple task, uglify and other tasks i had made works perfect.
The cwd property of files should be an string not an Array.
Fixed task:
copy: {
templates: {
files: [{
expand: true,
cwd: 'src/tpl',
src: ['**/*.tpl'],
dest: 'dist/assets/tpl'
}]
}
}
Output:
C:\Foo>grunt copy
Running "copy:templates" (copy) task
Done, without errors.
Reference: http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
Related
I'm trying to concatenate and then babelify and uglify files with Grunt.
I'd like to read an external file list, from a file where the files are written one for each line, newline separated.
I'm trying to use the following GruntFile.js, but Grunt says (after I added the src=['<%= jsFiles.toString().split("\n") %>'] line):
Running "browserify:dist1" (browserify) task
Warning: An error occurred while processing a template (Invalid or unexpected token). Use --force to continue.
Where is the error?
This is the GruntFile.js
module.exports = function(grunt) {
grunt.initConfig({
jsFiles: grunt.file.read('scripts/s.list'),
env: {
prod: {
NODE_ENV: 'production'
}
},
browserify: {
dist1: {
options: {
transform: [
['babelify', {presets: ['es2015']}]
]
},
src: ['<%= jsFiles.toString().split("\n") %>'],
dest: '/WebContent/js/libs/s.bundle.js'
},
},
uglify: {
my_target1: {
files: {
'/WebContent/js/libs/s.bundleuglified.js': ['/WebContent/js/libs/s.bundle.js']
}
},
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-env');
grunt.registerTask('default', ['release']);
grunt.registerTask('release', ['env', 'browserify', 'uglify']);
};
Edit: I added a backslash to \n and the error has gone, but the babelify task gives me an empty file...
Edit2: I was able to read the file list using the following two lines at the beginning of the GruntFile.js
const jsFiles = grunt.file.read('scripts/s.list');
const jsFilesArray = jsFiles.toString().split("\n");
and then
src: jsFilesArray.slice(0, jsFilesArray.length-1),
because the last element was '' and it gave the error Warning: must provide pattern” as Beniamin H suggested.
Edit3: I found that the babelify task was reading the files in alphabetical order, so I had to first concat them, as explained here, and then babelify and uglify
You don't need to use any '<%= %>'. The file is read synchronously into jsFiles property and it can be used immediately. You may want to specify encoding for grunt.file.read to get a string: https://gruntjs.com/api/grunt.file#grunt.file.read
I have the following gruntfile.js. Using grunt-contrib-concat and grunt-contrib-uglify together.
This is an extention of a developement asked #Dynamic Mapping and Concat with Grunt Uglify
I use banner in the uglify task to reference a file created in the concat task.
module.exports = function(grunt){
// Configure task(s)
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// setup concats
concat: {
options: {
separator: ';\n',
},
dev: {
files: [{
src: [
'sports.js',
'salon.js',
'offRoad.js',
],
dest: 'src/js/concat/car-dev.js'
}],
},
},
// setup uglify task
// this will produce a car js file (which includes car-dev.js) for every car js file located within scr/js/cars/
uglify: {
// dev - use in development mode, global overrides section
dev: {
options: {
// include concat dev.js
banner: grunt.file.read('src/js/concat/car-dev.js'),
beautify: true,
mangle: false,
compress: false,
preserveComments: 'all'
},
files: [{
expand: true,
cwd: 'src/js/cars/',
src: '*.js',
dest: 'javascript/dev',
ext: '.dev.js',
extDot: 'first'
}],
},
},
});
// Load the plugins
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-sass');
// Register task(s)
grunt.registerTask('default',
'concat:dev'
'uglify:dev'
]);
};
My issue is that if src/js/concat/car-dev.js doesn't exist, I get the following error:
Error: Unable to read "src/js/concat/car-dev.js" file <Error code: ENOENT).
If I create an empty file #src/js/concat/car-dev.js the grunt command works fine.
However this is not always possible. I want uglify to reference the car-dev.js file even if it doesn't exist. What I'm looking for is someway to reference the grunt->concat->dev->files->dest file.
I've tried adding the following
banner: grunt.file.read(grunt.concat.dev.files.dest),
and
banner: grunt.concat.dev.files.dest
but neither work. Is this possible?
This is not a task dependency issue - the error gets thrown before any tasks are run because banner: src/js/concat/car-dev.js does not exist.
Any help - much appreciated.
Whenever I run grunt jade I get an error:
Warning: pattern.indexOf is not a function Use --force to continue.
Now here is my jade task:
jade: {
options: {
pretty: true
},
all: {
files: {
expand:true,
cwd: 'src/static/jade',
ext: "html",
src: ['src/static/jade/**/*.jade', '!src/static/jade/_includes'],
dest: 'build/'
}
}
}
So basically I am trying to take the jade files in src/static/jade (including subdirs, except _include) and put them in build, keeping the directory structure. I have tryed commenting the expand line, however it gives me:
Warning: Unable to read "src/static/jade" file (Error code: EISDIR). Use --force to continue.
Perhaps I am going about this the wrong way. How should I fix this?
Your initial issues is that files should be an array of objects, not just an object: files: [{...}].
But then you have other troubles with your file definition:
if you specify cwd, your src should not repeat it
your ext needs a starting .
your ! pattern needs to specify files instead of a dir
So you need:
files: [{
expand:true,
cwd: 'src/static/jade/',
ext: ".html",
src: ['**/*.jade', '!_includes/**/*.jade'],
dest: 'build/'
}]
My grunt-contrib-sass will stop on compilation error with a traceback when I run grunt sass, but not when sass is triggered via grunt-contrib-watch.
sass: {
development: {
options: {
style: 'expanded'
},
files: [
{
expand: true,
cwd: '<%= paths.development %>stylesheets/',
src: ['*.scss', '*.sass'],
dest: '<%= paths.development %>assets/',
ext: '.css.liquid'
},
]
},
watch: {
sass: {
files: [
'<%= paths.development %>stylesheets/*{.sass,.*scss}'
],
tasks: ['sass:development'] // this swallows errors
},
I can't seem to find any information on how to prevent this from happening in the grunt-contrib-sass or grunt-contrib-watch documentation. There is no error propagation option.
What am I missing?
I have not had this problem on 5+ other previous grunt based projects with similar watch->sass setups and am curious what the internet thinks.
Without any error message it cant be hard to determine how to solve your problem. What does grunt sass -v tell you?
Also I've had a similar problem in the past, tho I don't recall the actual error message. I solved the problem by using grunt-sass instead of grunt-contrib-sass.
I'm having trouble configuring Grunt to watch my project files, rebuild and update a page hosted in a connect server. If I run any of the build tasks and then 'watch' as part of a combined task, then 'watch' seems to get stuck in a loop, endlessly printing the message.
Running "watch" task
Waiting...
Warning: must provide pattern
If instead I just run $ grunt watch, it will happily watch my source files and compile/build as appropriate.
I think the relevant task configurations are these:
watch: {
html: {
files: [ '<%= site.partials %>', '<%= site.layouts %>', '<%= site.pages %>' ],
tasks: [ 'html' ]
},
sass: {
files: [ '<%= site.src %>sass/*.scss' ],
tasks: [ 'styles' ]
}
},
// development server
connect: {
options: {
port: 8080,
livereload: 35729,
hostname: 'localhost',
},
dev: {
options: {
directory: 'build',
}
}
},
and the task definitions:
grunt.registerTask( 'build', [ 'styles', 'html', ] );
grunt.registerTask( 'default', [ 'build','connect:dev', 'watch' ] );
The 'styles' and 'html' tasks run grunt-sass and assemble. As stated above, running any of these tasks, or even 'watch' on its own yields the expected results. This suggests my config object has site.partials, site.dest etc defined correctly. The problem only happens when I run any task and then 'watch', as in the default task.
I just encountered a similar problem when I had been editing my Gruntfile and left a field (that should have had a file pattern) blank.
Check your Gruntfile for an empty file field.
In my specific example:
wiredep: {
options: {
overrides: {
"jquery-ui": {
"main": [
"jquery-ui.js",
"themes/base/jquery-ui.css",
""
]
}
}
}
}
Note the empty string above. That generated an error very similar to yours. It seems that Grunt doesn't tell you where the error is, unfortunately. You'll just need to scan through your Gruntfile manually to find the error.
connect:dev is the problem. Remove that and it should work fine.