Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Nov 2, 2019
1 parent 9eb832d commit c7cbff7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 51 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var postcss = require('postcss');
var compress = require('csso').syntax.compress;
var postcssToCsso = require('./lib/postcssToCsso.js');
var cssoToPostcss = require('./lib/cssoToPostcss.js');
const postcss = require('postcss');
const { compress } = require('csso').syntax;
const postcssToCsso = require('./lib/postcssToCsso.js');
const cssoToPostcss = require('./lib/cssoToPostcss.js');

var postcssCsso = postcss.plugin('postcss-csso', function postcssCsso(options) {
const postcssCsso = postcss.plugin('postcss-csso', function postcssCsso(options) {
return function(root, result) {
result.root = cssoToPostcss(compress(postcssToCsso(root), options).ast);
};
});

postcssCsso.process = function(css, options) {
return postcss([postcssCsso(options)]).process(css);
return postcss([postcssCsso(options)]).process(css, { from: undefined });
};

module.exports = postcssCsso;
48 changes: 15 additions & 33 deletions lib/cssoToPostcss.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
var postcss = require('postcss');
var generate = require('csso').syntax.generate;
var hasOwnProperty = Object.prototype.hasOwnProperty;
const postcss = require('postcss');
const { generate } = require('csso').syntax;

var DEFAULT_RAWS = {
const DEFAULT_RAWS = {
before: '',
after: '',
between: '',
semicolon: false,
left: '',
right: ''
};
var ROOT_RAWS = {
const ROOT_RAWS = {
semicolon: true
};
var DECL_RAWS = {
const DECL_RAWS = {
before: '',
after: '',
between: ':',
important: '!important'
};

function clone(source) {
var result = Object.create(Object.getPrototypeOf(source));

for (var key in source) {
if (hasOwnProperty.call(source, key)) {
result[key] = source[key];
}
}

return result;
return Object.assign(
Object.create(Object.getPrototypeOf(source)),
source
);
}

function listToPostcss(list, used) {
var result = [];
var before = '';
const result = [];
let before = '';

list.each(function(node) {
if (node.type === 'Raw' || node.type === 'Space') {
// attach raw and spaces to next node
before += node.value;
} else {
var postcssNode = cssoToPostcss(node, used);
const postcssNode = cssoToPostcss(node, used);

if (before !== '') {
postcssNode.raws = clone(postcssNode.raws);
Expand All @@ -57,11 +51,11 @@ function listToPostcss(list, used) {
}

function cssoToPostcss(node, used) {
var postcssNode = node.loc ? node.loc.postcssNode : null;
let postcssNode = node.loc ? node.loc.postcssNode : null;

if (postcssNode) {
// used is null when WeakSet is not supported
if (used === null || used.has(postcssNode)) {
if (used.has(postcssNode)) {
// make node clone if it's already used in resulting tree
postcssNode = clone(postcssNode);
} else {
Expand Down Expand Up @@ -136,17 +130,5 @@ function cssoToPostcss(node, used) {
};

module.exports = function(node) {
var result;
var used = null;

// node.js 0.10 doesn't support for WeakSet -> always clone nodes
if (typeof WeakSet === 'function') {
// use weak set to avoid using the same original postcss node twice
// in resulting tree, since nodes are changing on tree building
used = new WeakSet();
}

result = cssoToPostcss(node, used);

return result;
return cssoToPostcss(node, new WeakSet());
};
21 changes: 9 additions & 12 deletions lib/postcssToCsso.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var parse = require('csso').syntax.parse;
const { parse } = require('csso').syntax;

function getInfo(postcssNode) {
return {
postcssNode: postcssNode
postcssNode
};
}

Expand All @@ -12,21 +12,17 @@ function appendChildren(cssoNode, nodes) {
}

function parseToCsso(css, config, postcssNode) {
var cssoNode;

try {
cssoNode = parse(css || '', config);
const cssoNode = parse(css || '', config);
cssoNode.loc = getInfo(postcssNode);
return cssoNode;
} catch (e) {
if (e.name === 'CssSyntaxError') {
throw postcssNode.error(e.message, { index: e.offset });
}

throw e;
}

cssoNode.loc = getInfo(postcssNode);

return cssoNode;
}

function postcssToCsso(node) {
Expand All @@ -48,8 +44,8 @@ function postcssToCsso(node) {
)
};

case 'atrule':
var cssoNode = {
case 'atrule': {
const cssoNode = {
type: 'Atrule',
loc: getInfo(node),
name: node.name,
Expand All @@ -67,6 +63,7 @@ function postcssToCsso(node) {
}

return cssoNode;
}

case 'decl':
return parseToCsso(
Expand All @@ -82,6 +79,6 @@ function postcssToCsso(node) {
value: node.raws.left + node.text + node.raws.right
};
}
}
};

module.exports = postcssToCsso;

0 comments on commit c7cbff7

Please sign in to comment.