Skip to content
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

Author ES modules, use Rollup for bundling #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
.env
.DS_Store
/node_modules
/.history
/.history
dist/*.js
dist/*.map
26 changes: 8 additions & 18 deletions css-ns.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
module.exports = createCssNs;
module.exports.createCssNs = createCssNs;
module.exports.createOptions = createOptions;
module.exports.createReact = createReact;
module.exports.nsAuto = nsAuto;
module.exports.nsString = nsString;
module.exports.nsArray = nsArray;
module.exports.nsObject = nsObject;
module.exports.nsReactElement = nsReactElement;

var FILE_BASENAME = /.*[\/\\]([\w-]+).*/;

function isString(x) {
Expand Down Expand Up @@ -43,7 +33,7 @@ var assertRegexOption = assertOptionType.bind(null, isRegex, 'RegExp');
var assertStringOption = assertOptionType.bind(null, isString, 'string');
var assertObjectOption = assertOptionType.bind(null, isObject, 'object');

function createOptions(raw) {
export function createOptions(raw) {
if (raw._cssNsOpts) return raw; // already processed, let's skip any extra work
if (isString(raw)) return createOptions({ namespace: raw }); // shorthand for just specifying the namespace
assert(isObject(raw), 'Options must be provided either as an object or a string, got: ' + raw);
Expand All @@ -60,15 +50,15 @@ function createOptions(raw) {
};
}

function createCssNs(options) {
export function createCssNs(options) {
var opt = createOptions(options);
var ns = nsAuto.bind(null, opt);
ns.ns = ns; // allows: const { ns, React } = createCssNs(__filename);
if (opt.React) ns.React = createReact(opt, ns);
return ns;
}

function createReact(options, ns) {
export function createReact(options, ns) {
var opt = createOptions(options);
ns = ns || nsAuto.bind(null, opt); // avoid creating another bound version of nsAuto() if our caller already provided one
assert(opt.React, 'React support must be explicitly enabled by providing the "React" option');
Expand All @@ -82,7 +72,7 @@ function createReact(options, ns) {
});
}

function nsAuto(options, x) {
export function nsAuto(options, x) {
var opt = createOptions(options);
if (isReactElement(opt, x))
return nsReactElement(opt, x);
Expand All @@ -96,7 +86,7 @@ function nsAuto(options, x) {
return x;
}

function nsString(options, string) {
export function nsString(options, string) {
assert(isString(string), 'nsString() expects string input, got: ' + string);
var opt = createOptions(options);
return string.split(/\s+/).map(function(cls) {
Expand All @@ -113,7 +103,7 @@ function nsString(options, string) {
}).join(' ').trim();
}

function nsArray(options, array) {
export function nsArray(options, array) {
assert(isArray(array), 'nsArray() expects array input, got: ' + array);
var opt = createOptions(options);
return array
Expand All @@ -122,15 +112,15 @@ function nsArray(options, array) {
.join(' ');
}

function nsObject(options, object) {
export function nsObject(options, object) {
assert(isObject(object), 'nsObject() expects object input, got: ' + object);
var opt = createOptions(options);
return nsArray(opt, Object.keys(object).map(function(key) {
return object[key] ? key : null;
}));
}

function nsReactElement(options, el) {
export function nsReactElement(options, el) {
if (isString(el)) return el; // we're mapping a text node -> leave it be
var opt = createOptions(options);
assert(isReactElement(opt, el), 'nsReactElement() expects a valid React element, got: ' + el);
Expand Down
2 changes: 1 addition & 1 deletion css-ns.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('chai').assert; // @see http://chaijs.com/api/assert/
var cssNs = require('./css-ns');
var cssNs = require('./dist/css-ns');
var React = require('react');
var ReactDOMServer = require('react-dom/server');

Expand Down
Loading