Grunt: change file extension at build - gruntjs

I would like to write a Grunt task that, during build, will copy all .html files that I have and make an .asp version of it in /dist as well.
I have been trying to use grunt-contrib-copy to accomplish this, and here's what I have:
copy: {
//some other tasks that work...
//copy an .asp version of all .html files
asp: {
files: [{
expand: true,
dot: true,
cwd: '<%= config.app %>',
src: ['{,*/}*.html'],
dest: '<%= config.dist %>',
option: {
process: function (content, srcpath) {
return srcpath.replace(".asp");
}
}
}]
} //end asp task
},
I know that the process function is not actually correct... I've tried a few different regex to make it work to no avail. When I run the asp task, the Grunt CLI says that my it has copied 2 files, but they're nowhere to be found. Any help is appreciated.

You can do that using rename function.
For example:
copy: {
//some other tasks that work...
//copy an .asp version of all .html files
asp: {
files: [{
expand: true,
dot: true,
cwd: '<%= config.app %>',
src: ['{,*/}*.html'],
dest: '<%= config.dist %>',
rename: function(dest, src) {
return dest + src.replace(/\.html$/, ".asp");
}
}]
} //end asp task
},
This should works.

Related

assemble is not showing the text in json file

I am using assemble task in grunt to generate html page using handlebars like below:
assemble: {
options: {
flatten: true,
assets: '<%= config.tmp %>/assets',
layout: '<%= config.src %>/templates/layouts/default.hbs',
partials: '<%= config.src %>/templates/partials/*.hbs'
},
en: {
options: {
data: '<%= config.src %>/data/en/data.json'
},
files: [
{
expand: true,
cwd: '<%= config.src %>/templates/pages/',
src: '**/*.hbs',
dest: '<%= config.tmp %>/en_global/'
}
]
}
}
And I am referring the data by {{data.title}} in my partial files. However, in the generated html files, there is no text at all, it seems the data.json file in not loaded correctly. Does anyone know why?
You may want to try using {{data.data.title}} and see if that fixes it.

Yeoman, grunt build - keeping original file names?

I'm using the default gruntfile that is included when I scaffold out a new project.
copy: {
dist: {
files: [{
expand: true,
dot: false,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: ['myfolder/css/**/*',
]
},
}
}
I'm wanting to keep the original file names when I run the build as I keep getting b914da89.somefile.css.
What file do I need to modify to do this?
Look in Gruntfile.js to see where you are calling the rev task: grunt-rev. Removing that call should stop the modification of your file names/

Template var in gruntfile doesn't always work

In my gruntfile I noticed that I can't always get a template variable to work. Below is a very simple small grunt file to show the issue. The copy task will understand <%= yeoman.app %>, but the less task does not.
I suppose this has something to do with the files syntax, but I dont understand why. Can someone elaborate? And.. how can I get less to work?
'use strict';
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
var yeomanConfig = {
app: 'app',
dist: 'dist'
};
grunt.initConfig({
yeoman: yeomanConfig,
copy: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: '404.html'
}]
}
},
less: {
dist: {
files: {
'<%= yeoman.dist %>/styles/main.css': ['<%= yeoman.app %>/styles/main.less']
}
}
}
});
};
grunt copy: works
grunt less: Warning: An error occurred while processing a template (_ is not defined). Use --force to continue.
note, I'm using grunt 0.4.2 and node 0.10.13

How to omit a folder from the output file structure of a Grunt task

Im running a grunt task to build an Assemble site.
My task is configured as follows:
assemble: {
options: {
flatten: false,
assets: '<%= config.dist %>',
dist: '<%= config.dist %>',
layout: 'default.hbs',
layoutdir: 'templates/layouts/',
partials: 'templates/partials/*.hbs',
},
pages: {
files: { '<%= config.dist %>': ['pages/{,*/}*.hbs'] }
}
},
In my source files, I have a structure like this:
pages
cat1
cat2
This is outputting something like:
dist
pages
cat1
cat2
How can I setup the task so that it won't include the /pages folder but still generate the subfolders?
#jakerella is close but missing one piece, ext. It should be:
files: [
{
expand: true,
cwd: 'pages',
src: ['**/*.hbs'],
dest: '<%= config.dist %>/',
ext: '.html'
}
]
So you want it to be just dist->cat1 and dist->cat2? I think you'll need to tweak your pages target a bit, but I think you're looking for the expand and cwd options:
pages: {
files: {
expand: true,
cwd: 'pages/',
src: ['**/*.hbs'],
dest: '<%= config.dist %>',
ext: '.html'
}
}
(This was partially yanked from this answer.)
EDIT Added the ext option as well.

Grunt - Copy specific file without usemin

I would like to copy specific coffeescript file (which is not included in the <!-- build:js({.tmp, app}) --> section in my index.html) to for example dist/scripts directory.
I tried with (Gruntfile.js):
copy: {
dist: {
files: [
// ...
{
expand: true,
cwd: './tmp/scripts/polyfill',
dest: '<%= yeoman.dist %>/scripts/polyfill',
src: [
'*'
]
}]
}
},
If you just have one single file you want to copy over, you can use the simpler grunt syntax:
copy: {
dist: {
files: [{
'.tmp/scripts/polyfill/myfile.js': '<%= yeoman.dist %>/scripts/polyfill/myfile.js'
}]
}
}
Also the name of the temporary folder is .tmp, not just tmp.
This worked:
{
expand: true,
cwd: '.tmp/scripts/polyfill',
dest: '<%= yeoman.dist %>/scripts/polyfill',
src: [
'polyfill.js'
]
}
My approach was good, however it contained a typo: ./tmp - it should be .tmp.

Resources