Put the minified html in to javascript for grunt task - gruntjs

I have to minify my html and add it in the minified js as an innerHtml. Currently, I am running the following grunt task and manually copying the minified html to the JS file. But , how can I get the minified html as an output and put that inside js, so that I can do all the task in single run.
module.exports = function(grunt) {
grunt.initConfig({
concat: {
css: {
src: ['src/css/style.css',...],
dest: 'dest/css/main.css'
},
js: {
src: ['src/js/angular.min.js',...],
dest: 'dest/js/main.js'
}
},
uglify: {
js: {
src: 'dest/js/main.js',
dest: 'dest/js/main.min.js'
}
},
cssmin: {
css: {
src: 'dest/css/main.css',
dest: 'dest/css/main.min.css'
}
},
minifyHtml: {
options: {
cdata: true
},
dist: {
files: {
'dest/html/index.html': 'src/html/index.html'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-minify-html');
grunt.registerTask('build', ['concat', 'uglify', 'cssmin','minifyHtml']);
};

Have you tried grunt-contrib-copy? Here is what my task looks like:
copy: {
main: {
expand: true,
cwd: 'src/img',
src: '**',
dest: 'dist/img',
flatten: true,
filter: 'isFile',
}
}
npm install grunt-contrib-copy

I had to use the grunt-copy-part-of-file to manage this working.
'copy-part-of-file': {
simple_replace_scripts: {
options: {
sourceFileStartPattern: '<!-- INDEX START -->',
sourceFileEndPattern: '<!-- INDEX END -->',
destinationFileStartPattern: "<!-- JS START -->'",
destinationFileEndPattern: "'<!-- JS END -->"
},
files: {
'dest/js/my.js': ['src/html/index.html']
}
}
}

Related

Grunt not compiling Sass to CSS

I can't figure out what's wrong with my Gruntfile, but the way I understand it is that the sass task will compile abc.scss to abc-exp.css, then, cssmin will take abc-exp.css and generate abc.css. Finally, the watch task will run the css task, which includes sass and cssmin. However, the accurate CSS is only generated the first time I run my tasks, then, on any subsequent changes, nothing is generated.
My project structure is:
_themes/abc/css/abc-exp.css
_themes/abc/css/abc.css
_themes/abc/sass/abc.scss
_themes/abc/sass/partials
_themes/abc/sass/partials/_base.scss
_themes/abc/sass/partials/_variables.scss
_themes/abc/sass/partials/_mixins.scss
Gruntfile:
module.exports = function(grunt){
require("matchdep").filterDev("grunt-*").forEach(grunt.loadNpmTasks);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
build: {
files: {
'_themes/abc/css/abc-exp.css': '_themes/abc/sass/abc.scss'
}
}
},
// autoprefixer: {
// build: {
// src: '_themes/abc/css/abc-exp.css',
// dest: '_themes/abc/css/abc-exp.css'
// }
// },
cssmin: {
build: {
src: '_themes/abc/css/abc-exp.css',
dest: '_themes/abc/css/abc.css'
}
},
// cssbeautifier: {
// files: ['_themes/abc/css/abc-exp.css'],
// options: {
// indent: ' ',
// openbrace: 'end-of-line',
// autosemicolon: false
// }
// },
uglify: {
options: {
},
my_target: {
files: {
'_themes/abc/js/abc-min.js': ['_themes/abc/js/abc.js'],
'_themes/abc/js/abc-bottom-min.js': ['_themes/abc/js/abc-bottom.js']
}
}
},
watch: {
css: {
files: ['_themes/abc/sass/*.scss'],
tasks: ['css']
},
js: {
files: ['_themes/abc/js/abc.js', '_themes/abc/js/abc-bottom.js'],
tasks: ['js']
}
},
browserSync: {
dev: {
bsFiles: {
src: [
'_themes/abc/css/*.css',
'_themes/abc/img/*',
'_themes/abc/js/*.js',
'_themes/abc/**/*.html',
'_themes/abc/**/*.md'
]
},
options: {
watchTask: true,
proxy: 'wifi.dev:8888'
}
}
}
});
// grunt.registerTask('default', ['browserSync', 'watch']);
// grunt.registerTask('css', ['sass', 'autoprefixer', 'cssmin', 'cssbeautifier']);
// grunt.registerTask('buildcss', ['sass', 'autoprefixer', 'cssmin', 'cssbeautifier']);
// grunt.registerTask('js', ['uglify']);
// grunt.registerTask('buildjs', ['uglify']);
grunt.registerTask('default', ['browserSync', 'watch']);
grunt.registerTask('css', ['sass', 'cssmin']);
grunt.registerTask('js', ['uglify']);
};
I think you need to replaced build with dist like this:
sass: {
dist: {
files: {
'_themes/abc/css/abc-exp.css': '_themes/abc/sass/abc.scss'
}
}
},
In order to make the task observable by watch you need to change it from css to sass
watch: {
sass: {
files: ['_themes/abc/sass/**/*.{scss,sass}'],
tasks: ['sass']
},
...
},

Grunt: cssmin not working

I am trying to minify CSS using Grunt cssmin plugin. Below is my code:
// Minify CSS
cssmin: {
build: {
options: {
banner: '/* Minified CSS */'
},
files: {
'htt/css/style.min.css' : ['wp/css/**/*.css']
}
}
},
when i run "grunt cssmin" it gives error of "Unexpected identifier".
You where missing a comma after the js object in the watch task, for future you can edit your answer to provide more information instead of posting it as an answer :)
// Watch Tasks
watch: {
js: {
files: ['wp/js/*.js'],
tasks: ['uglify:dev']
}, <------- Missing comma
css: {
files: ['wp/css/*.css'],
tasks: ['']
}
}
});
#mike
`module.exports = function (grunt) {
// Configure Tasks
grunt.initConfig ({
pkg: grunt.file.readJSON ('package.json'),
// Uglify JS
uglify: {
build: {
src: 'wp/js/*.js',
dest: 'htt/js/script.min.js'
},
dev: {
options: {
beautify: true,
mangel: false,
compress: false,
preserveComments: 'all'
},
src: 'wp/js/*.js',
dest: 'htt/js/script.min.js'
}
},
// Concatenating files
concat: {
build: {
src: ['wp/css/*.css'],
dest: 'htt/css/style.css'
}
},
// Minify CSS
cssmin: {
build: {
//options: {
// banner: '/* Minified CSS */'
//},
files: {
'htt/css/style.min.css' : ['wp/css/**/*.css']
}
}
},
// Watch Tasks
watch: {
js: {
files: ['wp/js/*.js'],
tasks: ['uglify:dev']
}
css: {
files: ['wp/css/*.css'],
tasks: ['']
}
}
});
// Load the Plugins
grunt.loadNpmTasks ('grunt-contrib-uglify');
grunt.loadNpmTasks ('grunt-contrib-watch');
grunt.loadNpmTasks ('grunt-contrib-concat');
grunt.loadNpmTasks ('grunt-contrib-cssmin');
// Register Tasks
grunt.registerTask ('default', ['uglify:dev']);
grunt.registerTask ('build', ['uglify:build', 'cssmin']);
};`
Above is the whole gruntfile.js file

FoundationPress: want to add task to Gruntfile.js

I'm using the excellent FoundationPress and want to add a task to my watch to create a single .css file out of a single .scss file. In my case, this is for CKEditor - because it needs a separate .css file for custom formatting the input area.
However, I get this output whenever I edit my .scss file:
>> File "ckeditor/nb-ckeditor.scss" changed.
Warning: Task "sassckeditor" not found. Use --force to continue.
Aborted due to warnings.
Here's my the content of Gruntfile.js (see ADD 1 and ADD 2 for what I added to the original content):
module.exports = function(grunt) {
require('time-grunt')(grunt);
grunt.initConfig(
{
pkg: grunt.file.readJSON('package.json'),
sass: {
options: {
sourceMap: true
},
dist: {
options: {
outputStyle: 'compressed'
},
files: {
'css/foundation.css': 'scss/foundation.scss'
}
}
},
/* ADD 1 BEGIN */
sassckeditor: {
options: {
sourceMap: true
},
dist: {
options: {
outputStyle: 'compressed'
},
files: {
'ckeditor/nb-ckeditor-fromsass.css': 'ckeditor/nb-ckeditor.scss'
}
}
},
/* ADD 1 END */
copy: {
scripts: {
expand: true,
cwd: 'bower_components/foundation/js/vendor/',
src: '**',
flatten: 'true',
dest: 'js/vendor/'
},
iconfonts: {
expand: true,
cwd: 'bower_components/fontawesome/',
src: ['**', '!**/less/**', '!**/css/**', '!bower.json'],
dest: 'assets/fontawesome/'
},
},
'string-replace': {
fontawesome: {
files: {
'assets/fontawesome/scss/_variables.scss': 'assets/fontawesome/scss/_variables.scss'
},
options: {
replacements: [
{
pattern: '../fonts',
replacement: '../assets/fontawesome/fonts'
}
]
}
},
},
concat: {
options: {
separator: ';',
},
dist: {
src: [
'bower_components/foundation/js/foundation/foundation.js',
'js/custom/*.js'
],
dest: 'js/foundation.js',
},
},
uglify: {
dist: {
files: {
'js/foundation.js': ['js/foundation.js']
},
options: {
preserveComments: false
}
}
},
watch: {
grunt: { files: ['Gruntfile.js'] },
sass: {
files: 'scss/**/*.scss',
tasks: ['sass'],
options: {
livereload:true,
}
},
/* ADD 2 BEGIN */
sassckeditor: {
files: 'ckeditor/*.scss',
tasks: ['sassckeditor'],
options: {
livereload:true,
}
},
/* ADD 2 END */
js: {
files: 'js/custom/**/*.js',
tasks: ['concat', 'uglify'],
options: {
livereload:true,
}
},
all: {
files: '**/*.php',
options: {
livereload:true,
}
}
}
});
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-string-replace');
grunt.registerTask('build', ['copy', 'string-replace:fontawesome', 'sass', 'concat', 'uglify']);
grunt.registerTask('default', ['watch', 'sassckeditor']);
};
What am I doing wrong? Thanks in advance!
From reading your code it looks like the error is coming from trying to run a task by the name of 'sassckeditor' rather than sending the .scss through the 'sass' task. Here's an example of what your code should look like:
/* ADD 2 BEGIN */
sassckeditor: {
files: 'ckeditor/*.scss',
tasks: ['sass'],
options: {
livereload:true,
}
},
/* ADD 2 END */
This will keep your .scss file for the CKEditor separate from the rest of your Sass files, but will still run it thorough the proper 'sass' task that actually compiles the .scss files.
Okay, got this working by simply adding an entry for "files" in my "dist" task:
sass: {
options: {
sourceMap: true
},
dist: {
options: {
outputStyle: 'compressed'
},
files: {
'css/foundation.css': 'scss/foundation.scss',
'css/nb-ckeditor.css': 'scss/nb-ckeditor.scss' // <-- THIS
}
}
},

GRUNT: Concatenated CSS files not reflecting in index.html page

I have setup grunt task to concatenate the CSS files into a combined.css file and it successfully concatenates the CSS files.
grunt.initConfig({
concat: {
options: {
separator: '\n\n\n',
base: "../../../",
},
css: {
src: [ '../../my/demo/v1.0/1.css',
'../../my/demo/v1.0/2css',
'../../my/demo/v1.0/3.css',
],
dest: '../../my/demo/v1.0/combined123.css',
},
});
grunt.loadNpmTasks('grunt-concat-css');
grunt.registerTask('default', ['concat']);
However, I am not able to figure out how should I replace 1.css, 2.css and 3.css in the index.html with the combined123.css file using Grunt.
When manually trying to replacing these css files with the css files in index.html, running the grunt command reverts back the changes made to index.html. When I do the View Source Code for the page in browser, it shows the multiple css files instead of the latest concatenated combined123.css file.
How can I replace these css files with the combined123.css file in the tag of index.html, using the Grunt task.
This is the Gruntfile.js
grunt.initConfig({
root: root,
//concat:css task implemented here as mentioned in above code snippet
connect: {
server: {
options: {
port: grunt.option('port') || 9090,
keepalive: false,
base: "../../../"
}
}
},
open: {
all: {
path: 'http://localhost:<%= connect.server.options.port%>/my/demo/v1.0/dest/index.html'
}
},
watch: {
html: {
files: ['views/*.html', 'index_temp.html'],
tasks: ['mergeFiles']
},
livereload: {
files: ['dest/index.html'],
options: { livereload: true }
}
},
htmlbuild: {
dist: {
src: 'index_temp.html',
dest: '.tmp/',
options: {
beautify: true,
relative: true,
sections: {
headers: 'views/abc.html',
input: 'views/def.html',
}
}
}
},
replace: {
example: {
src: ['.tmp/index_temp.html'],
dest: 'dest/index.html',
replacements: [ {
from: /^\s*\n/gm,
to: ''
},
{
from: /\s+$/,
to: ''
}],
}
});
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-concat-css');
grunt.registerTask('server', ['mergeFiles', 'open' ,'connect', 'watch' ]);
grunt.registerTask('default', ['mergeFiles', 'open' ,'connect', 'watch', 'concat']);
grunt.registerTask('mergeFiles', ["htmlbuild:dist", "replace:example"]);
Any help would be much appreciated.

Invoking grunt concat two times with different options

I have different options to concat JS files and CSS files.
How can I configure grunt to run such a configuration ?
This doesn't work :
concat: {
js: { // Custom options for JS
options: {
separator: '\n',
sourceMap: true,
banner: '...',
},
core: {
src: ['src/core/*.js', 'src/core/**/*.js'],
dest: 'assets/xxxx.js'
}
},
css: { // Default options for CSS
core: {
src: ['src/core/*.css', 'src/core/**/*.css'],
dest: 'assets/xxxx.css'
}
}
}
This is ugly, but it works :
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.renameTask('concat', 'concatCss');
grunt.loadNpmTasks('grunt-contrib-concat');
// The task is then loaded two times, under 'concat' and 'concatCss' names.
...
concat: {
options: {
separator: '\n',
sourceMap: true,
banner: '...',
},
core: {
src: ['src/core/*.js', 'src/core/**/*.js'],
dest: 'assets/xxxx.js'
},
concatCss: { // Default options for CSS
core: {
src: ['src/core/*.css', 'src/core/**/*.css'],
dest: 'assets/xxxx.css'
}
}

Resources