grunt rename function not working - gruntjs

I am using save2png to provide fallback pngs to svg4everybody. The svgs are created and I can change input and output directory. But svg4everybody requires that the pngs are saved with a filename like: "spritesheetname.svg.id.jpg"
At first, I tried some simple things, but I can't even figure out how to change the filename in the first place.
Here is my code:
module.exports = function(grunt){
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
svg2png: {
all: {
files: [
// rasterize all SVG files in "img" and its subdirectories to "img/png"
{ cwd: '../img/icons/',
src: ['**/*.svg'],
dest: '../symbol/svg/',
rename: function(dest, src) {
var filename = src.substring(src.lastIndexOf('/'), src.length);
return dest + 'prefix' + filename + '.png';
}
}
]
}
}
});
grunt.registerTask('default', []);
grunt.loadNpmTasks('grunt-svg2png');
};
The pngs are created, but the filename is always the same as the original svg.
I stumbled upon this question, Grunt rename not working, but it was no real help. Flatten and expand just changed the directory structure, but had no influence on the filename.

Related

How to update css theme file in WordPress

I am confused on where to edit WordPress themes. I am new to WordPress and have a custom theme which main style.css file just imports the style for this theme like this:
#import url('assets/stylesheets/app.css');
I read that it is recommended to make a new child theme, but I don't see the need for that in my case, since I would like to almost completely change the css of the theme, so there is no need to keep the original theme files. Since, I tried to modify the file 'assets/stylesheets/app.css' I couldn't see any changes in the browser. Can I edit the styles there, or I need to do it in the WP admin dashboard somewhere?
I would like to build my scripts with gulp, which I set up like this:
var gulp = require('gulp');
var sass = require('gulp-sass');
var include = require('gulp-include');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var sourcemaps = require('gulp-sourcemaps');
var prefix = require('gulp-autoprefixer');
var connect = require('gulp-connect');
var browserify = require('gulp-browserify');
var livereload = require('gulp-livereload');
var browsersync = require('browser-sync');
var config = {
srcDir: './assets',
styles: {
src: '/scss/app.scss',
dest: '/stylesheets',
includePaths: [
'node_modules/foundation-sites/scss'
],
prefix: ["last 2 versions", "> 1%", "ie 9"]
},
scripts: {
src: '/js/app.js',
dest: '/js'
},
img: {
src: '/images/**/*',
dest: '/images'
}
};
var srcDir = './src',
destDir = './build';
gulp.task('styles', function() {
return gulp.src(config.srcDir + config.styles.src)
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: config.styles.includePaths,
sourceMap: true,
outFile: config.srcDir + config.styles.dest + '/app.css',
outputStyle: 'compressed'
}))
.pipe(prefix(config.styles.prefix))
.pipe(sourcemaps.write())
.on('error', sass.logError)
.pipe(gulp.dest(config.srcDir + config.styles.dest))
.pipe(browsersync.reload({ stream: true }));
});
gulp.task('scripts', function() {
gulp.src(config.srcDir + config.scripts.src)
.pipe(browserify({
insertGlobals : true,
debug : !gulp.env.production
}))
.pipe(gulp.dest(config.srcDir + config.scripts.dest))
});
gulp.task('include', function() {
return gulp.src(config.srcDir + config.img.src)
.pipe(gulp.dest(config.srcDir + config.img.dest));
});
gulp.task('watch', function () {
// Watch .scss files
gulp.watch(config.srcDir + config.styles.src, ['styles']);
// Watch .js files
gulp.watch(config.srcDir + config.scripts.src, ['scripts']);
});
gulp.task('default', ['styles', 'scripts', 'watch']);
So, not sure how can I do it utilizing gulp. Where can I change the theme without creating the child theme?
Where does the import of "app.css" happen - at the beginning or at the end of the "style.css" file? If it's at the beginning, the changed rules in "app.css" might be overwritten by the following "style.css" rules.

gulp-recess doesn't change properties order

I try to convert my less files into css files using gulp-less and then use gulp-recess to change properties order in css files. Task less works properly but task recess doesn't work.
This is my gulpfile.js file
'use strict';
var gulp = require('gulp'),
less = require('gulp-less'),
path = require('path'),
recess = require('gulp-recess');
// less
gulp.task('less', function () {
return gulp.src(['less/style.less', 'less/fonts.less'])
.pipe(less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.pipe(gulp.dest('css'));
});
// recess
gulp.task('recess', ['less'], function () {
return gulp.src(['css/style.css', 'css/fonts.css'])
.pipe(recess())
.pipe(recess.reporter())
.pipe(gulp.dest('css'));
});
// watch
gulp.task('watch', function () {
gulp.watch('less/*.less', ['less']),
gulp.watch('css/*.css', ['recess']);
});
// default
gulp.task('default', ['less', 'recess', 'watch']);
Errors in Node.js console
What's wrong? How to fix it?
gulp-recess won't reorder your css properties, its purpose is to 'lint' your css/less files - checks for syntax errors so you can fix there warnings/errors yourself in the source files.

Encrypting files with grunt-encrypt

I'm trying to encrypt my .keychain files on my Mac and saving them on in a backup place. When i run grunt enc it just creates 2 very small files in the directory of the source files. So encrypting and moving the files doesn't go well...
package.js is just grunt with 1 devDependency grunt-encrypt.
Gruntfile.js is shown below.
'use strict';
module.exports = function(grunt) {
var npmDependencies = require('./package.json').devDependencies;
grunt.initConfig({
encrypt: {
encryptKeys: {
options: {
key: 'SuperSecretKey',
dest: '/Users/myusername/www/admin/filebackup/encrypted',
},
files: {
'keychains': [
'/Library/Keychains/system.keychain',
'/Users/myusername/Library/Keychains/login.keychain'
]
}
}
},
});
grunt.loadNpmTasks('grunt-encrypt');
grunt.registerTask('enc', [
'encrypt'
]);
};
I've been using Grunt for a week, so could very well be i'm missing something obvious. Anybody used grunt-encrypt before?
Thanks!
Looks as if your SuperSecretKey is not defined yet.
I've solved it like this:
add
module.exports = function (grunt) {
// ad here
var superSecretKey = 'secretpassword';
to your Gruntfile.js
Or put a keyfile (mine is called livereload.key) in the project's root and alter your Gruntfile like this:
//key : superSecretKey,
key: grunt.file.read('livereload.key'),

How to avoid unnecessary uglifying in GruntJS?

I have a grunt file with the following definition:
uglify: {
build: {
src: 'www/temp/application.js', // a concatenation of files via grunt-contrib-concat
dest: 'www/temp/application.min.js'
}
},
what I would really like to do is to recompute the final application.min.js only in case that application.js file was changed. More precisely, I want to add the condition:
# pseudocode
if (getFileContents(application.js) == getFileContents(previously.uglified.application.js)) {
// do nothing
} else {
// run uglifying on application.js
}
Reason:
I deploy my project by git and uglifying is relatively slow (3+ seconds) and moreover, it is unnecessary since I don't change JS files often.
There are several possible solutions:
You can create your own grunt task that will check files for last modify time using for example fs.stat then run uglify task through grunt.task.run with prepared options as argument.
Or you can build files object dynamically passing it through filter function:
var fs = require('fs');
module.exports = function (grunt) {
function filterChanged(files) {
var mtime = '',
stats;
for (var dest in files) {
stats = fs.statSync(files[dest]);
try {
mtime = fs.readFileSync(files[dest] + '.mtime', 'utf8');
}
catch (ex) {
fs.writeFileSync(files[dest] + '.mtime', stats.mtime, 'utf8');
return files;
}
if (stats.mtime == mtime || !mtime) {
delete files[dest];
}
else {
fs.writeFileSync(files[dest] + '.mtime', stats.mtime, 'utf8');
}
}
return files;
}
grunt.initConfig({
uglify: {
build: {
files: filterChanged({
'www/temp/application.min.js': 'www/temp/application.js'
})
}
}
});
};
This causes invoke of filterChanged function every time uglify task runs.

run concat task dynamically with destination file dynamically created

I'm running a grunt concat task on one of my projects and it looks something like this:
/**
* Concatenate | Dependencies Scripts
*/
concat: {
dependencies: {
files: {
"./Ditcoop/js/plugins.min.js": ["./Ditcoop/js/vendor/**/*.min.js", "!./Ditcoop/js/vendor/modernizr/*.js", "!./Ditcoop/js/vendor/jquery/*.js"],
"./Global/js/plugins.min.js": ["./Global/js/vendor/**/*.min.js", "!./Global/js/vendor/modernizr/*.js", "!./Global/js/vendor/jquery/*.js"],
"./Webshop/js/plugins.min.js": ["./Webshop/js/vendor/**/*.min.js", "!./Webshop/js/vendor/modernizr/*.js", "!./Webshop/js/vendor/jquery/*.js"]
}
}
}
My question would be if I could somehow make that more dynamic without having to specify each root folder. I was thinking of something like this:
concat: {
dependencies: {
files: {
"./*/js/plugins.min.js": ["./*/js/vendor/**/*.min.js", "!./*/js/vendor/modernizr/*.js", "!./*/js/vendor/jquery/*.js"],
}
}
}
I'm pretty sure I cannot do it this way, but I could use the expand option, I'm just not sure how I could use it so I can do that under the right root folder, so I won't create the same destination file as many times I run the concat.
Always remember Gruntfiles are javascript :)
grunt.initConfig({
concat: {
dependencies: {
files: (function() {
var files = Object.create(null);
grunt.file.expand({filter: 'isDirectory'}, '*').forEach(function(dir) {
files[dir + '/js/plugins.min.js'] = [
dir + '/js/vendor/**/*.min.js',
'!' + dir + '/js/vendor/modernizr/*.js',
'!' + dir + '/js/vendor/jquery/*.js'
];
});
return files;
}()),
},
},
});
But if your dependency handling logic is this complex you may want to consider using a module loader such as browserify or requirejs. The concat task is really just for joining simple files together.

Resources