glob('./html/**/*.hbs', {}, (er, files) => { let templates = {}; for (const file of files) { const name = path.basename(file, '.hbs').replace(/_/g, '-'); templates[name] = minify( fs.readFileSync(file, 'utf-8'), minifyOptions); } const templatesHolder = util.format( '<script type=\'text/javascript\'>' + 'var templates = %s;' + '</script>', JSON.stringify(templates)); const finalHtml = baseHtml .replace(/(<\/head>)/, templatesHolder + '$1') .replace( /(<title>)(.*)(<\/title>)/, util.format('$1%s$3', config.name)); fs.writeFileSync( './public/index.htm', minify(finalHtml, minifyOptions)); console.info('Bundled HTML'); });
return this.imageSize.setAll($, true).then($ => { let html = $.html() html = this.removeUnusedCss(html) const minifiedHtml = minify(html, { removeComments: true, removeCommentsFromCDATA: true, removeCDATASectionsFromCDATA: true, collapseWhitespace: true, minifyCSS: true }) return this.fixDynamicHrefs(minifiedHtml) })
return es.map(function (file, cb) { try { // if in dev mode html should not be minified // instead only output stream after checking ignore path. if(options.env === 'development'){ file.path = ignorePath(options.ignorePath, unixify(file.path)); } else { file.path = ignorePath(options.ignorePath, unixify(file.path)); file.contents = new Buffer(htmlmin.minify(String(file.contents), options)); } } catch (err) { return cb(new gutil.PluginError('gulp-htmlmin', err, options)); } cb(null, file); });
function minify(ext, file, callback) { try { var minifiedFile = htmlMinifier.minify(file, { collapseWhitespace: true, caseSensitive: true, removeComments: true, removeRedundantAttributes: true }); callback(null, minifiedFile); } catch (err) { callback(err); } }
return new Promise( function( resolve ) { this.grunt.log.subhead( 'PHANTOMAS index.html WRITING STARTED.' ); var templateResults = []; // check if all files were valid json results.forEach( function( result ) { if ( result.isFulfilled() ) { templateResults.push( result.value() ); } }.bind( this ) ); this.grunt.file.write( this.options.indexPath + 'index.html', this.grunt.template.process( this.grunt.file.read( TEMPLATE_FILE ), { data : { additionalStylesheet : this.options.additionalStylesheet, group : this.options.group, meta : this.meta, results : templateResults, url : this.options.url } } ) ); this.grunt.log.ok( 'Phantomas created new \'index.html\' at \'' + this.options.indexPath + '\'.' ); this.grunt.file.write( this.options.indexPath + 'index.html', minify( this.grunt.file.read( this.options.indexPath + 'index.html' ), { removeComments : true, collapseWhitespace : true } ) ); this.grunt.log.ok( 'Phantomas made \'index.html\' at \'' + this.options.indexPath + '\' nice and small.' ); resolve( templateResults ); }.bind( this ) );
export function minify(html) { // Just parse the html to make sure it is correct before minifying HTMLTools.parseFragment(html); return htmlMinifier.minify(html, { collapseWhitespace: true, conservativeCollapse: true, minifyCSS: true, minifyJS: true, processScripts: ['text/template'], removeAttributeQuotes: false, caseSensitive: true, customAttrSurround: [[/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/]], customAttrAssign: [/\)?\]?=/] }); }
HtmlWebpackPlugin.prototype.process = function process(compiler, stats) { var context = _.assign({ assets: stats.assetsByChunkName, paths: _.flatten([ _.values(compiler.options.resolve.externals), this.paths(compiler, stats) ]) }, this.options); return minify.minify(this.template(context), { removeComments: true, collapseWhitespace: true, removeEmptyAttributes: true, minifyJS: true, minifyCSS: true }); };
Injector.prototype.getFile = function (viewPath) { //ToDo: Use fs.stat try { var stat = fs.statSync(viewPath); if (stat.isFile()) { var text = fs.readFileSync(viewPath, 'utf8'); text = text.replace(/[\n\r\t]+/g, " "); var result = htmlmin.minify(text, { collapseWhitespace: true, conservativeCollapse:true }); return result; } } catch (err) { console.log(err.message); } return undefined; }
(next) => { if (!continuable || !sHtml || !$ || !isRelease) { return next(); } console.log('Cleaning HTML...'); let s = $.html(); html = htmlmin(s, { collapseWhitespace: true, preserveLineBreaks: true, quoteCharacter: '"', removeComments: true, removeEmptyAttributes: true, useShortDoctype: true }); return next(); }
}).map(function (filepath) { // Read file source. var content = grunt.file.read(filepath); if (options.minify) { try { content = minify(content, options.minifyOption); } catch (e) { grunt.warn("Minify Error: " + filepath); } } return content; }).join(' ');
htmlProcessor._transform = function (data, encoding, done) { this.push(minify(data.toString(), { caseSensitive: true, collapseWhitespace: true, preserveLineBreaks: false, keepClosingSlash: true, minifyCSS: true, minifyJS: true, removeAttributeQuotes: true, removeComments: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true })); done(); };
return function(next) { var start = Date.now(); var content = fs.readFileSync(p).toString(); var result = htmlminifier.minify(content, { removeComments: true, removeCommentsFromCDATA: true, collapseWhitespace: true, collapseBooleanAttributes: true, removeEmptyAttributes: true, minifyJS: true }); fs.writeFileSync(p, result, 'utf8'); var saved = parseInt((content.length - result.length) / content.length * 100, 10); log.log('update', 'Optimize HTML: %s ' + '[%s saved]'.yellow + ' (%dms)'.grey, p, saved + '%', Date.now() - start); next(); };
fs.readFile(filename, 'utf8', function(err, data) { if (err) throw err; $ = cheerio.load(data); $('head>link').remove(); $('head').append('<link href="app.css" rel="stylesheet">'); $('body>script[type*=javascript]').remove(); $('body').append('<script type="text/javascript" src="app.js"></script>'); var input = $.html(); var output = minify(input, {removeComments:true, collapseWhitespace:true}); process.stdout.write(output); });
return new Promise( function( resolve ) { this.grunt.log.subhead( 'PHANTOMAS index.html WRITING STARTED.' ); var templateResults = []; var images = this.getImages(); // check if all files were valid json results.forEach( function( result ) { if ( result.isFulfilled() ) { templateResults.push( result.value() ); } }.bind( this ) ); this.grunt.file.write( this.options.indexPath + 'index.html', this.grunt.template.process( minify( this.grunt.file.read( TEMPLATE_FILE ), { removeComments : true, // TODO fix me // https://github.com/stefanjudis/grunt-phantomas/issues/93 collapseWhitespace : true } ), { data : { additionalStylesheet : this.options.additionalStylesheet, assertions : this.options.assertions, failedAssertions : this.failedAssertions, group : this.options.group, images : images, meta : phantomas.metadata.metrics, results : templateResults, timestamp : this.timestamp, url : this.options.url, version : this.version } } ) ); this.grunt.log.ok( 'Phantomas created new \'index.html\' at \'' + this.options.indexPath + '\'.' ); resolve( templateResults ); }.bind( this ) );
var minHTML = function (HTML) { return HTMLMin (HTML.toString(), { removeComments: true, removeCommentsFromCDATA: true, removeCDATASectionsFromCDATA: true, collapseWhitespace: true, conservativeCollapse: true, collapseBooleanAttributes: true, // removeAttributeQuotes: true, removeRedundantAttributes: true, // removeEmptyAttributes: true, removeOptionalTags: true, // removeEmptyElements: true, keepClosingSlash: true, caseSensitive: true }).replace(/>\s+</g, '><'); };
exec("uglifyjs2.cmd js/engine.js > build/engine.min.js -c -m", function (error, stdout, stderr) { if (error != null) { console.log(error); } // compress html var data = fs.readFileSync("index.build.html", 'utf8'); var htmlMin = htmlMinify(data, { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true, removeStyleLinkTypeAttributes: true, minifyCSS: true }) fs.writeFileSync('build/index.html', htmlMin); // create zip var zip = fs.createWriteStream('AlchemistsNightmare.zip'); var archive = archiver('zip'); zip.on('close', function () { var zipSize = archive.pointer() / 1024; var current = ("AlchemistsNightmare.zip " + zipSize.toFixed(2) + "KB zipped").bold.white; var left = 13.0 - zipSize; left = left > 0.0 ? (left.toFixed(2) + "KB left!").green : (left.toFixed(2) + "KB over!").red; console.log(current + " " + left); }); archive.pipe(zip); archive.bulk( [ { expand: true, cwd: 'build', src: ['**'], dest: 'AlchemistsNightmare' } ]); archive.finalize(); });
args[args.length - 1] = function(err, result) { if(err || !result) return callback(err, result); try { return callback(err, minify(result, { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true, minifyJS: true, minifyCSS: true, minifyURLs: true, maxLineLength: 80 })); } catch(e) { return callback(e, result); } };
HtmlPages.prototype.compile = function (data, path, callback) { var destinationDir, destinationPath, err, error, result; try { result = this.htmlMinOptions.disabled? data: minify(data, this.htmlMinOptions); destinationPath = this.destinationFn(path); destinationPath = fspath.join(this.publicPath, destinationPath); destinationDir = fspath.dirname(destinationPath); mkdirp.sync(destinationDir); return fs.writeFileSync(destinationPath, result); } catch (_error) { err = _error; console.error("Error while processing '" + path + "': " + (err.toString())); return error = err; } finally { callback(error, ""); } };
function minFile (path) { var text, src = nodePath.normalize((task.filter ? (task.filter.cwd || '.') : '.') + nodePath.sep + path), target = nodePath.normalize(task.dest + nodePath.sep + path), lastChange = fs.statSync(src).mtime.getTime(); if (globalOptions.newer && lastChange === timestamp[task][src]) return; text = grunt.file.read(src, encoding); try { text = minify(text, taskOptions); grunt.file.write(target, text, encoding); timestamp[task][src] = lastChange; } catch (err) { grunt.fail.fatal(src + ln + err); } grunt.log.ok('Minify ' + src + ' => ' + target + ' successfully.'); }
function build () { var typeset = require('../src'); var html = fs.readFileSync(INPUT, 'utf-8'); // Duplicate the 'before' section on the demo var $ = cheerio.load(html, {decodeEntities: false}); $('#panel-2').html($('#panel-1').html()); html = $.html(); // TYPESET! var options = {ignore: '.ts-ignore'}; // the before section html = typeset(html, options); fs.writeFileSync(OUTPUT, minify(html, minifyOpts)); console.log('Built!'); }
}).forEach(function(filepath){ var content = grunt.file.read(filepath); if (options.minify) { try { content = minify(content, options.minifyOption); } catch (e) { grunt.warn("Minify Error: " + filepath); } } // extname to js var dest = filepath.replace(/\.([^.]*)$/, '.js'); kissy_template(content, dest); });
return through.obj(function(file, encoding, callback){ switch(path.extname(file.path)){ case '.js': var result = uglify.minify(file.contents.toString(), {fromString: true}); file.contents = new Buffer(result.code); break; case '.css': file.contents = new Buffer(new cleanCSS().minify(file.contents.toString())); break; case '.html': file.contents = new Buffer(htmlmin.minify(file.contents.toString())); break; } this.push(file); return callback(); });
}, function (err, html) { if (err) { grunt.fail.fatal(err); } else { var result = minify(html, { removeAttributeQuotes: true, collapseWhitespace: true, removeComments: true, minifyJS: true, minifyCSS: true }); grunt.file.write(path.join(file.cwd, file.dest), result); } done(); });
var minifyHtml = function (htmlContent) { return minify(htmlContent, { removeComments: true, removeCommentsFromCDATA: true, removeCDATASectionsFromCDATA: true, collapseWhitespace: true, collapseBooleanAttributes: true, removeAttributeQuotes: true, removeRedundantAttributes: true, removeEmptyAttributes: true, removeScriptTypeAttributes: true, caseSensitive: true, minifyJS: true, minifyCSS: true, keepClosingSlash: true }); };
}).forEach(function(file) { // read from source and write to dist console.log('Copy file: ' + file); var target = path.join(targetDir, path.relative(baseDir, file)); mkdirp.sync(path.dirname(target)); var content = fs.readFileSync(file); if (/\.html?$/.test(file)) { content = minify(content.toString('utf-8'), { removeComments: true, collapseWhitespace: true, conservativeCollapse: true, preserveLineBreaks: true, }); } fs.writeFileSync(target, content); });
readTemplate(cwd, templateUrl, (err, tplSrc) => { if (err) { return cb(err); } let minifiedTplSrc = minify(tplSrc, { collapseWhitespace: true, conservativeCollapse: true, quoteCharacter: '"' }).replace(/[\\$'"]/g, '\\$&'); let tranformedTpl = template.replace(templateUrl, minifiedTplSrc); tranformedTpl = tranformedTpl.replace('templateUrl', 'template'); src = src.replace(template, tranformedTpl); cb(); });
module.exports = function(content, extension){ // TODO: This should ignore js files with ".min" in the name if (extension === 'js') { transformer['uglify-js'].renderSync(content); } if (extension === 'css') { return new CleanCSS().minify(content); } if (extension === 'html') { htmlmin.minify(content, { removeComments: true, collapseWhitespace: true }) } return content; };
Promise.all(promises).then(function(){ contents = $.html(); var HTMLMinifier = require('html-minifier').minify; var minified = HTMLMinifier(contents, { minifyCSS: true, minifyJS: true, collapseWhitespace: true, removeAttributeQuotes: true }); file.contents = new Buffer(minified, encode); if(!cdnBucket) gulp.start('resource'); cb(null, file, encode); });
generate: function (dir, data, cb) { var file = path.join(dir, 'index.html'); var result = compiledTemplates.index(data); var result2 = minify(result, { removeComments: true, collapseWhitespace: true }); fs.outputFile(file, result2, function(err) { if (err) { log.log('error', 'Couldn\'t write the file ' + file + ' err:' + err); } else { log.log('info', 'Wrote file ' + file); } cb(err); }); }
async fetch(url) { let body = await request(url) body = minify(body, { collapseWhitespace: true }) const $ = cheerio.load(body) let title = $('title').text() let p = $('p').map( (i, el) => $(el).text() ).get() let h = $(':header').map( (i, el) => $(el).text().substring(0,64) ).get() let a = $('a').map( (i, el) => $(el).attr('href') ).get() let img = $('img').map( (i, el) => $(el).attr('src') ).get() let parsed_url = URL.parse(url) return { title, a, p, h, img, body, url: parsed_url } }