npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-to-css-generator

v1.5.3

Published

Generate CSS style sheets from your css-in-js JSON objects

Downloads

64

Readme

Build Status Coverage Status

js-to-css-generator

Generate CSS style sheets from your css-in-js JSON objects

import { getFile, File, GetFileSettings, Tag } from 'js-to-css-generator';
import { jsToCss, CSSFile, File } from 'js-to-css-generator';
// styles.js

export const something = {
  backgroundColor: '#fff',
  fontSize: 12,
  padding: 16,
};

export const somethingElse = {
  backgroundColor: '#ccc',
  fontSize: 10,
  padding: 8,
};
// Get the style objects as a module via require:
const styles = require('./styles.js');

// ...or assemble all style objects into a single object:
const styles = {
  something: {
    backgroundColor: '#fff',
    fontSize: 12,
    padding: 16,
  },
  somethingElse: {
    backgroundColor: '#ccc',
    fontSize: 10,
    padding: 8,
  },
};

// Generate CSS file
const { writeFileSync } = require('fs');
const { join } = require('path');
const { jsToCss } = require('js-to-css-generator');

const file = {
  name: 'styles.css',
  module: styles,
};

const cssFile = jsToCss(file);

writeFileSync(join(__dirname, cssFile.name), cssFile.css, 'utf8');
.something {
  background-color: #fff;
  font-size: 12px;
  padding: 16px;
}

.something-else {
  background-color: #ccc;
  font-size: 10px;
  padding: 8px;
}
// styles.js

export const something = {
  backgroundColor: '#fff',
  fontSize: 12,
  padding: 16,
  ':hover': {
    backgroundColor: '#ccc',
  },
  '& + &': {
    marginLeft: 8,
  },
};
// Bring in the style objects
const styles = require('./styles.js');

// Generate CSS file
const { writeFileSync } = require('fs');
const { join } = require('path');
const { jsToCss } = require('js-to-css-generator');

const file = {
  name: 'styles.css',
  module: styles,
};

const cssFile = jsToCss(file);

writeFileSync(join(__dirname, cssFile.name), cssFile.css, 'utf8');
.something {
  background-color: #fff;
  font-size: 12px;
  padding: 16px;
}

.something:hover {
  background-color: #ccc;
}

.something + .something {
  margin-left: 8px;
}
// styles.js

export const something = {
  backgroundColor: '#fff',
  fontSize: 12,
  padding: 16,
  ':hover': {
    backgroundColor: '#ccc',
  }
};

export const spacing = {
  '& + &': {
    marginLeft: 8,
  },
};
// Bring in the style objects
const styles = require('./styles.js');

// Generate CSS file
const { writeFileSync } = require('fs');
const { join } = require('path');
const { jsToCss } = require('js-to-css-generator');

const file = {
  name: 'styles.css',
  combinators: {
    spacing: '.something',
  },
  module: styles,
};

const cssFile = jsToCss(file);

writeFileSync(join(__dirname, cssFile.name), cssFile.css, 'utf8');
.something {
  background-color: #fff;
  font-size: 12px;
  padding: 16px;
}

.something:hover {
  background-color: #ccc;
}

.something + .something {
  margin-left: 8px;
}
// styles.js

export const something = {
  backgroundColor: '#fff',
  fontSize: 12,
  padding: 16,
};

export const somethingElse = {
  backgroundColor: '#ccc',
  fontSize: 10,
  padding: 8,
};

export const anotherSomething = {
  backgroundColor: 'green',
  display: 'flex',
  margin: 8,
};

export const oneMore = {
  border: '1px solid #ccc',
  padding: 16,
};
// Bring in the style objects
const styles = require('./styles.js');

// Generate CSS file
const { writeFileSync } = require('fs');
const { join } = require('path');
const { jsToCss } = require('js-to-css-generator');

const file = {
  name: 'styles.css',
  prepend: '.test',
  version: '1',
  overrides: {
    somethingElse: '.my-override',
  },
  ignore: ['anotherSomething'],
  map: true,
  module: styles,
};

const cssFile = jsToCss(file);

writeFileSync(join(__dirname, cssFile.name), cssFile.css, 'utf8');
/* something */
.test-v1-something {
  background-color: #fff;
  font-size: 12px;
  padding: 16px;
}

/* somethingElse */
.my-override {
  background-color: #ccc;
  font-size: 10px;
  padding: 8px;
}

/* oneMore */
.test-v1-one-more {
  border: 1px solid #ccc;
  padding: 16px;
}
const files = [file1, file2, file3];
const cssFiles = jsToCss(files);
/** 
 * @config
 * @prepend .test
 * @map
 */


export const something = {
  backgroundColor: '#fff',
  fontSize: 12,
  padding: 16,
};

/** @class .my-override */
export const somethingElse = {
  backgroundColor: '#ccc',
  fontSize: 10,
  padding: 8,
};

/** @ignore */
export const anotherSomething = {
  backgroundColor: 'green',
  display: 'flex',
  margin: 8,
};

export const oneMore = {
  border: '1px solid #ccc',
  padding: 16,
};
/** 
 * @config
 * @prepend .test
 * @map
 */
const { readFileSync } = require('fs');

// Bring in the style objects
const styles = require('./styles.js');

const srcFile = readFileSync('./styles.js', 'utf8');
const file = getFile(srcFile, styles, { name: 'styles.css', version: '1' });

const cssFile = jsToCss(file);

writeFileSync(join(__dirname, cssFile.name), cssFile.css, 'utf8');
/* something */
.test-v1-something {
  background-color: #fff;
  font-size: 12px;
  padding: 16px;
}

/* somethingElse */
.my-override {
  background-color: #ccc;
  font-size: 10px;
  padding: 8px;
}

/* oneMore */
.test-v1-one-more {
  border: 1px solid #ccc;
  padding: 16px;
}
// Get the style objects as a module via require:
const styles = require('./styles.js');

// Generate CSS file
const { writeFileSync } = require('fs');
const { join } = require('path');
const { jsToCss } = require('js-to-css-generator');

const file = {
  name: 'styles.css',
  prepend: '.💩',
  module: styles,
};

const cssFile = jsToCss(file);

writeFileSync(join(__dirname, cssFile.name), cssFile.css, 'utf8');
.💩-something {
  background-color: #fff;
  font-size: 12px;
  padding: 16px;
}

.💩-something-else {
  background-color: #ccc;
  font-size: 10px;
  padding: 8px;
}
export type File = {
  // any name that you would like to use as an identifier for the File
  name?: string,

  // optional string to use to prepend all of your classes for scope
  prepend?: string, 

  // optional object with key/value pairs where the keys match the
  // style object names you want to override and values that are the
  // class names to use as the overrides
  overrides?: Record<string, string>, 

  // optional object with key/value pairs where the keys match the
  // style object names you want to affect and values that are the
  // class names to use for the combinators. The value will replace
  // any '&' in your style names.
  combinators?: Record<string, string>,

  // optional array of names of style objects you don't want to
  // include as part of the output CSS file
  ignore?: string[],

  // optional string representation of the file version to add as
  // part of the class name
  version?: string,
  
  // optionally map the original object name to the outputted class
  // name via including a code comment above the class name with the
  // original object name
  map?: boolean,

  // object that contains all of your styled objects
  module: Record<string, Record<string, string | number>>,
};
export type CSSFile = {
  // any name that you gave your {File}
  name?: string,
  
  // The CSS file string content
  css: string,
  
  // JavaScript Map object of key/value pairs that maps the JS object name to a class name.
  objectToStyleMap: Map<string, string>,

  // JavaScript Map object of key/value pairs that maps the class name name to a JS object.
  styleToObjectMap: Map<string, string>,
};
export type Tags = {
  class?: string,
  combinator?: string,
  config?: string,
  ignore?: string,
  map?: string,
  prepend?: string,
};
{
  class: '@class',
  combinator: '@combinator',
  config: '@config',
  ignore: '@ignore',
  map: '@map',
  prepend: '@prepend',
};
export type GetFileSettings = {
  // any name that you gave your {File}
  name?: string, 

  // optional string to use to prepend all of your classes for scope
  prepend?: string, 

  // optional string representation of the file version to add as
  // part of the class name
  version?: string, 

  // RegEx string used to find specific objects and jsdoc inside the
  // source file. This string should include 'XXXXXX' in it which
  // will be a placeholder for the object name to search for.
  styleRegEx?: string, 

  // Object used to define what the jsdoc tags are for parsing
  tags?: Tags, 
};

Within the module you'll find the following directories and files:

package.json
CHANGELOG.md -- history of changes to the module
LICENSE
README.md -- this file
/lib
  └───/es5
    └───/getFile
      └───index.d.ts - 872 Bytes
      └───index.js - 4.1 KB
      └───index.d.ts - 141 Bytes
      └───index.js - 430 Bytes
    └───/jsToCss
      └───index.d.ts - 519 Bytes
      └───index.js - 11.25 KB
      └───types.d.ts - 795 Bytes
      └───types.js - 79 Bytes
  └───/es6
    └───/getFile
      └───index.d.ts - 872 Bytes
      └───index.js - 3.81 KB
      └───index.d.ts - 141 Bytes
      └───index.js - 76 Bytes
    └───/jsToCss
      └───index.d.ts - 519 Bytes
      └───index.js - 11.05 KB
      └───types.d.ts - 795 Bytes
      └───types.js - 12 Bytes

MIT