diff --git a/README.md b/README.md index c65682be..8724d8e0 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ var files = ['my', 'array', 'of', 'HTML', 'files', 'or', 'http://urls.com'], report : false, banner : false, uncssrc : '.uncssrc', - userAgent : 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)' + userAgent : 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X)', + inject : function(window){ window.document.querySelector('html').classList.add('no-csscalc', 'csscalc'); } }; uncss(files, options, function (error, output) { @@ -103,6 +104,7 @@ Options: -u, --uncssrc Load these options from -n, --noBanner Disable banner -a, --userAgent Use a custom user agent string + -I, --inject Path to javascript file to be executed before uncss runs ``` **Note that you can pass both local file paths (which are processed by [glob](https://github.com/isaacs/node-glob)) and URLs to the program.** @@ -159,6 +161,28 @@ Options: ``` - **userAgent** (String): The user agent string that jsdom should send when requesting pages. May be useful when loading markup from services which use user agent based device detection to serve custom markup to mobile devices. Defaults to `uncss`. +- **inject** (String / Function): Path to a local javascript file which is executed before uncss runs. A function can also be passed directly in. + + Example inject.js file + + ```js + 'use strict'; + + module.exports = function(window) { + window.document.querySelector('html').classList.add('no-csscalc', 'csscalc'); + }; + ``` + + Example of passing inject as a function + + ```js + { + inject: function(window){ + window.document.querySelector('html').classList.add('no-csscalc', 'csscalc'); + } + } + ``` + ### As a PostCSS Plugin UnCSS can be used as a [PostCSS](https://github.com/postcss/postcss) Plugin. diff --git a/bin/uncss b/bin/uncss index 6f5fbab1..69ad4c5e 100755 --- a/bin/uncss +++ b/bin/uncss @@ -30,6 +30,7 @@ .option('-u, --uncssrc ', 'Load these options from ') .option('-n, --noBanner', 'Disable banner') .option('-a, --userAgent ', 'Use a custom useragent string') + .option('-I, --inject ', 'Path to javascript file to be executed before uncss runs') .parse(process.argv); options = { @@ -43,7 +44,8 @@ htmlroot: program.htmlroot, uncssrc: program.uncssrc, banner: !program.noBanner, - userAgent: program.userAgent + userAgent: program.userAgent, + inject: program.inject }; if (program.args.length) { diff --git a/src/jsdom.js b/src/jsdom.js index 40f37e6c..957fb7f2 100644 --- a/src/jsdom.js +++ b/src/jsdom.js @@ -35,6 +35,16 @@ function fromSource(src, options) { }; } + if (options.inject) { + config.onload = function(window) { + if (typeof options.inject === 'function') { + options.inject(window); + } else { + require(path.join(__dirname, options.inject))(window); + } + }; + } + return new Promise((resolve, reject) => { jsdom.env(src, config, (err, res) => { if (err) { diff --git a/src/uncss.js b/src/uncss.js index 23fd6d0e..269f198b 100644 --- a/src/uncss.js +++ b/src/uncss.js @@ -203,7 +203,8 @@ function init(files, options, callback) { banner: true, // gulp-uncss parameters: raw: null, - userAgent: 'uncss' + userAgent: 'uncss', + inject: null }); process(options).then(([css, report]) => callback(null, css, report), callback); diff --git a/tests/jsdom.js b/tests/jsdom.js index 0edecc31..9627cb85 100644 --- a/tests/jsdom.js +++ b/tests/jsdom.js @@ -103,4 +103,34 @@ describe('jsdom', () => { done(); }); }); + + it('Should execute passed in javascript function before uncss runs', (done) => { + const options = { + htmlroot: path.join(__dirname, './jsdom'), + inject: (window) => { + window.document.querySelector('html').classList.add('no-test', 'test'); + } + }; + uncss(['tests/jsdom/inject.html'], options, (err, output) => { + expect(err).to.equal(null); + expect(output).to.include('.no-test .inject'); + expect(output).to.include('.test .inject'); + + done(); + }); + }); + + it('Should load then execute passed in javascript function before uncss runs', (done) => { + const options = { + htmlroot: path.join(__dirname, './jsdom'), + inject: '../tests/jsdom/inject.js' + }; + uncss(['tests/jsdom/inject.html'], options, (err, output) => { + expect(err).to.equal(null); + expect(output).to.include('.no-test .inject'); + expect(output).to.include('.test .inject'); + + done(); + }); + }); }); diff --git a/tests/jsdom/inject.html b/tests/jsdom/inject.html new file mode 100644 index 00000000..62a815de --- /dev/null +++ b/tests/jsdom/inject.html @@ -0,0 +1,11 @@ + + + + + jsdom inject test + + + +
+ + diff --git a/tests/jsdom/inject.js b/tests/jsdom/inject.js new file mode 100644 index 00000000..40bd413d --- /dev/null +++ b/tests/jsdom/inject.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = function(window) { + window.document.querySelector('html').classList.add('no-test', 'test'); +}; diff --git a/tests/jsdom/jsdom.css b/tests/jsdom/jsdom.css index dcde6578..8debc9f6 100644 --- a/tests/jsdom/jsdom.css +++ b/tests/jsdom/jsdom.css @@ -21,3 +21,11 @@ .error { margin: 0; } + +.no-test .inject{ + margin: 0; +} + +.test .inject{ + margin: 0; +} \ No newline at end of file