From 39c7e6ee86e9b0a31de0907143cf47a6c3040c51 Mon Sep 17 00:00:00 2001 From: s2 Date: Tue, 8 May 2018 17:54:29 +0200 Subject: [PATCH] read cleancss options from .cleancssrc file --- .cleancssrc | 8 ++++++++ minifyfromhtml.js | 26 +++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 .cleancssrc diff --git a/.cleancssrc b/.cleancssrc new file mode 100644 index 0000000..4bc4e2e --- /dev/null +++ b/.cleancssrc @@ -0,0 +1,8 @@ + { + "level": { + "1": { + "specialComments": false, + "rebase": false + } + } +} diff --git a/minifyfromhtml.js b/minifyfromhtml.js index 855cb75..352545c 100644 --- a/minifyfromhtml.js +++ b/minifyfromhtml.js @@ -1,5 +1,6 @@ let argv = require('minimist')(process.argv.slice(2)); -var fs = require('fs'); +let fs = require('fs'); +let path = require('path'); let jsdom = require('jsdom'); let JSDOM = jsdom.JSDOM; let babel = require("babel-core"); @@ -12,8 +13,8 @@ let usage = `usage: the minification with a .babelrc file. https://babeljs.io/ - the css minification process uses clean-css, so you can modify - the minification with + the css minification process uses clean-css. to modify the default settings, + create a file named .cleancssrc and put the json configuration in that file. https://github.com/jakubpawlowicz/clean-css example: @@ -99,14 +100,21 @@ readStdin(function(html) { let style = styles[i]; let css = fs.readFileSync(style); - let cleanCssOptions = { - level: { - 1: { - specialComments: false, - rebase: false + let cleanCssOptions = ''; + + let readConfig = function(configfilepath) { + try { + return JSON.parse(fs.readFileSync(configfilepath + '/.cleancssrc')); + } catch (e) { + if (configfilepath != __dirname) { + configfilepath = path.resolve(path.normalize(configfilepath + '/../')); + return readConfig(configfilepath); + } else { + return {}; } } - }; + } + cleanCssOptions = readConfig(path.resolve('./')); console.log(style + ' -> ' + argv.css); fs.appendFileSync(argv.css, (new CleanCSS(cleanCssOptions).minify(css)).styles + '\n');