Home

Awesome

<p align="center"><img src="logo.png" width="240"></p> <p align="center"> <a href="https://www.npmjs.com/package/mole"><img src="https://img.shields.io/npm/v/mole.svg"></a> <a href="https://discord.gg/BDEvF8m"><img src="https://img.shields.io/discord/617327499554193445"></a> </p> <hr />

[!NOTE] Mole is being refactored. To try the latest release checkout release-refactor-esmodule.

Mole is a platform agnostic preprocessor that allows you to create your own design system framework. There are a lot of ways to use mole. Some examples include, creating your own CSS framework, managing design tokens for different platforms, or something else entirely.

Usage

Setup your project and install mole as a dependency.

npm install mole --save-dev

Build output files using

mole.build()

Configure mole using one of the methods below.

See the examples for different ways of configuring your project.

Configuration

By default mole will look for a file called mole.config.js at the root of your project.

// mole.config.js
module.exports = {
	theme: 'theme.js', // The path of your theme file (supports .js and .jsonnet)
	model: ['model-name'], // The name or path of any models you want to use (optional)
	template: ['template-name'], // The name or path of any templates you want to use
	output: [
		// You can have one or more outputs
		{ css: { file: 'styles.css' } },
		{ ios: { file: 'styles.h' } },
		{ android: { file: 'styles.xml' } },
	],
}

You can override the location of the config file by using mole.config().

mole.config('src/mole.config.js')

Properties




Themes

A theme is a file used to describe different design decisions, characteristics, traits or tokens. Mole is fairly unopinionated about how you use it so you can structure your theme data how you like. In fact a theme is completely optional if you prefer.

Below is a trivial example of a theme

{
	font: {
		size: [16, 19, 22, 26, 30, 35]
	}
}

Theme data is accessible inside models and is immutable from inside them. When you create a model this returns an object which updates the main model and is then available to use by templates when they are rendered.

To avoid logic responsible for describing certain design characteristics being stored in models, you can can describe theme data using a more expressive method using Jsonnet which includes functions from it's standard library.

Example using Jsonnet

{
    font: {
        size: [
            std.ceil(16 * std.pow($.number['golden ratio'], n))
            for n in std.range(0, 5)
        ]
    }
}

Models

Models act like middleware which allow you to create a data structure separate from theme data so it can be used by different templates for different platforms and languages.

When more than one model is assigned to an output the data from each model is merged together.

To use a named model

mole.use('model', 'model-name', (theme, name, str) => {
	// Do something here to modifying the theme data
	theme.newProperty = []

	return theme
})

Templates

Templates allow you to format data for a specific platform or language. You can create templates by either using template strings (using Nunjucks) or a function.

When multiple templates are specified the strings from each template are merged into one.

An example of using a function

mole.use('template', 'font-size', (model, theme, name, str) => {
	let scale = model[name]

	for (let i = 0; i < scale.length; i++) {
		str`
            .$font-${i} {
                font-size: ${scale[i]}
            }`
	}

	return str()
})

An example of using a template string

mole.use('template', 'font-size',

    `.font-{{modifier}} {
        font-size: {{value}};
    }`
})

API

Installation

Setup your project and install mole as a dependency.

npm install mole --save-dev

Development

To install

npm install mole@next --save-dev

To run/compile

npm run build

To test

npm run test

To test and watch for changes

npm run dev