From f887072ce24eefef3acf1784297d6fa1dd23c788 Mon Sep 17 00:00:00 2001 From: s2 Date: Tue, 8 May 2018 19:08:46 +0200 Subject: [PATCH] add exclude option --- minifyfromhtml.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/minifyfromhtml.js b/minifyfromhtml.js index 352545c..88f0f90 100644 --- a/minifyfromhtml.js +++ b/minifyfromhtml.js @@ -7,7 +7,7 @@ let babel = require("babel-core"); let CleanCSS = require('clean-css'); let usage = `usage: - minifyfromhtml --js= --css= < + minifyfromhtml --js= --css= --exclude= < the minification process uses babel under the hood, so you can modify the minification with a .babelrc file. @@ -18,11 +18,9 @@ let usage = `usage: https://github.com/jakubpawlowicz/clean-css example: - minifyfromhtml --js=dist/mywidget.min.js --css=dist/mywidget.min.css < example/index.html + minifyfromhtml --js=dist/mywidget.min.js --css=dist/mywidget.min.css --exclude=js/jquery.js < example/index.html `; -let outputDir = argv.o; - if (argv.h) { console.log(usage); return; @@ -33,6 +31,11 @@ if (!argv.js || !argv.css) { return; } +var excludeFiles = argv.exclude || []; +if (typeof(excludeFiles) === 'string') { + excludeFiles = [excludeFiles]; +} + let readStdin = function(cb) { let stdin = ''; process.stdin.resume(); @@ -64,6 +67,15 @@ readStdin(function(html) { //process scripts let scripts = getTagAttrs(dom, 'script', 'src'); + + //remove exluded + excludeFiles.forEach(i => { + var index = scripts.indexOf(i); + if (index !== -1) { + scripts.splice(index, 1); + } + }); + let processedScripts = {}; for (let i = 0; i < scripts.length; i++) { let script = scripts[i]; @@ -94,11 +106,25 @@ readStdin(function(html) { //process css let styles = getTagAttrs(dom, 'link', 'href'); + + //remove exluded + excludeFiles.forEach(i => { + var index = styles.indexOf(i); + if (index !== -1) { + styles.splice(index, 1); + } + }); + let processedStyles = {}; fs.writeFileSync(argv.css, ''); for (let i = 0; i < styles.length; i++) { let style = styles[i]; + if (excludeFiles.indexOf(style) > -1) { + console.log(style + ' excluded'); + continue; + } + let css = fs.readFileSync(style); let cleanCssOptions = '';