Skip to content

Commit

Permalink
Merge pull request #20 from zgreen/pr/19
Browse files Browse the repository at this point in the history
Pr/19
  • Loading branch information
zgreen authored Jun 21, 2017
2 parents 8ae5205 + 55f21ce commit 837bd59
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 199 deletions.
91 changes: 53 additions & 38 deletions lib/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,33 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
* Clean the original root node passed to the plugin, removing custom atrules,
* properties. Will additionally delete nodes as appropriate if
* `preserve === false`.
*
* @param {Object} root The root PostCSS object.
* @param {boolean} preserve Preserve identified critical CSS in the root?
/**
* Append to an existing critical CSS file?
*/
var append = false;

/**
* Clean the original root node passed to the plugin, removing custom atrules,
* properties. Will additionally delete nodes as appropriate if
* `preserve === false`.
*
* @param {Object} root The root PostCSS object.
* @param {boolean} preserve Preserve identified critical CSS in the root?
*/
function clean(root, preserve) {
root.walkAtRules('critical', function (atRule) {
if (preserve === false && !atRule.nodes) {
root.removeAll();
if (preserve === false) {
if (atRule.nodes && atRule.nodes.length) {
atRule.remove();
} else {
root.removeAll();
}
} else {
atRule.remove();
if (atRule.nodes && atRule.nodes.length) {
atRule.replaceWith(atRule.nodes);
} else {
atRule.remove();
}
}
});
// @TODO `scope` Makes this kind of gnarly. This could be cleaned up a bit.
Expand Down Expand Up @@ -71,24 +84,24 @@ function clean(root, preserve) {
});
}

/**
* Do a dry run, console.log the output.
*
* @param {string} css CSS to output.
/**
* Do a dry run, console.log the output.
*
* @param {string} css CSS to output.
*/
function doDryRun(css) {
console.log(
// eslint-disable-line no-console
(0, _chalk.green)('Critical CSS result is: ' + (0, _chalk.yellow)(css)));
}

/**
* Do a dry run, or write a file.
*
* @param {bool} dryRun Do a dry run?
* @param {string} filePath Path to write file to.
* @param {Object} result PostCSS root object.
* @return {Promise} Resolves with writeCriticalFile or doDryRun function call.
/**
* Do a dry run, or write a file.
*
* @param {bool} dryRun Do a dry run?
* @param {string} filePath Path to write file to.
* @param {Object} result PostCSS root object.
* @return {Promise} Resolves with writeCriticalFile or doDryRun function call.
*/
function dryRunOrWriteFile(dryRun, filePath, result) {
var css = result.css;
Expand All @@ -98,12 +111,12 @@ function dryRunOrWriteFile(dryRun, filePath, result) {
});
}

/**
* Confirm a node has no child nodes other than a specific node.
*
* @param {array} nodes Nodes array to check.
* @param {Object} node Node to check.
* @return {boolean} Whether or not the node has no other children.
/**
* Confirm a node has no child nodes other than a specific node.
*
* @param {array} nodes Nodes array to check.
* @param {Object} node Node to check.
* @return {boolean} Whether or not the node has no other children.
*/
function hasNoOtherChildNodes() {
var nodes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
Expand All @@ -114,26 +127,27 @@ function hasNoOtherChildNodes() {
}).length === 0;
}

/**
* Write a file containing critical CSS.
*
* @param {string} filePath Path to write file to.
* @param {string} css CSS to write to file.
/**
* Write a file containing critical CSS.
*
* @param {string} filePath Path to write file to.
* @param {string} css CSS to write to file.
*/
function writeCriticalFile(filePath, css) {
_fs2.default.writeFile(filePath, css, function (err) {
_fs2.default.writeFile(filePath, css, { flag: append ? 'a' : 'w' }, function (err) {
append = true;
if (err) {
console.error(err);
process.exit(1);
}
});
}

/**
* Primary plugin function.
*
* @param {object} options Object of function args.
* @return {function} function for PostCSS plugin.
/**
* Primary plugin function.
*
* @param {object} options Object of function args.
* @return {function} function for PostCSS plugin.
*/
function buildCritical() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
Expand All @@ -148,6 +162,7 @@ function buildCritical() {
minify: true,
dryRun: false
}, filteredOptions);
append = false;
return function (css) {
var dryRun = args.dryRun,
preserve = args.preserve,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-critical-css",
"version": "2.1.5",
"version": "3.0.0",
"description": "Generate critical CSS using PostCSS",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -31,7 +31,7 @@
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^3.0.1",
"flow-bin": "^0.45.0",
"flow-bin": "^0.48.0",
"minimist": "^1.2.0",
"tape": "^4.6.0"
},
Expand Down
Loading

0 comments on commit 837bd59

Please sign in to comment.