Skip to content

expose style rules/properties to js exporter #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions lib/compile-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,55 @@ module.exports = function compileExports(result, importItemMatcher, camelCaseKey
return "";
}

var exportRules = {}
Object.keys(result.exports).forEach(function(key) {
var value = result.exports[key];
var index = value.indexOf(" ")
if (index > 0) value = value.slice(0, index);
var rule = result.rules["."+value]

var camelCaseRule = {};
Object.keys(rule).forEach(function(key) {
var value = rule[key];
key = camelCase(key)
camelCaseRule[key] = value;
});

if (/[\w_][\w\d_]*/.test(key)) {
exportRules[key] = camelCaseRule;
}
if (camelCaseKeys === true) {
var _key = camelCase(key);
if (_key !== key) {
exportRules[key] = camelCaseRule;
}
} else if (camelCaseKeys === 'dashes') {
var _key = dashesCamelCase(key);
if (_key !== key) {
exportRules[key] = camelCaseRule;
}
}
});

var exportJs = Object.keys(result.exports).reduce(function(res, key) {
var valueAsString = JSON.stringify(result.exports[key]);
valueAsString = valueAsString.replace(result.importItemRegExpG, importItemMatcher);
res.push("\t" + JSON.stringify(key) + ": " + valueAsString);

if (/[\w_][\w\d_]*/.test(key)) {
res.push("\t" + JSON.stringify(key) + ": " + valueAsString);
}
if (camelCaseKeys === true) {
res.push("\t" + JSON.stringify(camelCase(key)) + ": " + valueAsString);
var _key = camelCase(key);
if (_key !== key) {
res.push("\t" + JSON.stringify(_key) + ": " + valueAsString);
}
} else if (camelCaseKeys === 'dashes') {
res.push("\t" + JSON.stringify(dashesCamelCase(key)) + ": " + valueAsString);
var _key = dashesCamelCase(key);
if (_key !== key) {
res.push("\t" + JSON.stringify(_key) + ": " + valueAsString);
}
}

return res;
}, []).join(",\n");
}, ["\t\"__styles__\": " + JSON.stringify(exportRules)]).join(",\n");

return "{\n" + exportJs + "\n}";
};
13 changes: 13 additions & 0 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,20 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
annotation: false
}
}).then(function(result) {
var rules = {};
result.root.nodes.forEach(function(node) {
if (node.type == "rule") {
var styles = {};
node.nodes.forEach(function(xttr) {
if (xttr.type == "decl") {
styles[xttr.prop] = xttr.value;
}
});
rules[node.selector] = styles;
}
})
callback(null, {
rules: rules, /* expose style rules to export */
source: result.css,
map: result.map && result.map.toJSON(),
exports: parserOptions.exports,
Expand Down