Skip to content

Commit 892baa8

Browse files
stafyniaksachawtho
authored andcommitted
feat(lib): add esm build
1 parent 99ede3f commit 892baa8

File tree

8 files changed

+96
-39
lines changed

8 files changed

+96
-39
lines changed

README.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Bulma CSS Vars extends [**Bulma**](https://github.com/jgthms/bulma) to use CSS v
66
[![](https://github.com/wtho/bulma-css-vars/workflows/ci/badge.svg)](https://github.com/wtho/bulma-css-vars/actions?query=workflow%3Aci)
77
[![](https://img.shields.io/badge/Demo-green)](https://wtho.github.io/bulma-css-vars)
88

9-
109
This is an extension and a kind of "sass-pre-post-processor" that tries to be as least intrusive as possible to Bulma, while making arbitrary color changes in the bulma color schemes automated, as easy as possible.
1110

1211
There is quite some setup and configuration to be done, but once it is setup, it works like a charm. Read the section [What is the difficulty](#what-is-the-difficulty-why-this-setup), to learn why this setup is required.
1312

1413
## Usage
14+
1515
```bash
1616
npm i -S bulma bulma-css-vars
1717
npm i -D sass
@@ -22,7 +22,7 @@ To use this package, you have to use the dart implementation of sass, the `sass`
2222

2323
```js
2424
// bulma-css-vars.config.js
25-
const { hsl, rgb } = require('bulma-css-vars')
25+
import { hsl, rgb } from 'bulma-css-vars'
2626

2727
// color names have to match bulma's $variable-name, without '$'
2828
// values will be used for initial colors and fallback
@@ -36,7 +36,7 @@ const appColors = {
3636
// reuse variable colors
3737
appColors['text'] = appColors['primary']
3838

39-
module.exports = {
39+
export default {
4040
sassEntryFile: './src/style/main-sass-file.sass',
4141
jsOutputFile: './src/bulma-generated/bulma-colors.js',
4242
sassOutputFile: './src/bulma-generated/generated-vars.sass',
@@ -46,16 +46,17 @@ module.exports = {
4646
transition: '0.5s ease',
4747
}
4848
```
49+
4950
You need to configure `bulma-css-vars` to tell it about your sass setup, especially your sass entry file, the variables you want to modify and where the generated bulma files should be placed.
5051

51-
| Config key | |
52-
| ----------------------- |:---------------------------------------------------------------------------------------------------|
53-
| `sassEntryFile` | Sass Entry File of you Application - relative path form config file |
54-
| `jsOutputFile` | full name of generated js file, can also be `<a-typescript-file>.ts` |
55-
| `sassOutputFile` | full name of generated sass file, should be included in your app styles |
56-
| `cssFallbackOutputFile` | full name of generated css file, should be included in your sass app styles (optional) |
57-
| `colorDefs` | color definitions, names have to match bulma color names (see examples above) |
58-
| `globalWebVar` | if you import js files directly in the browser, you need `true`, see [Direct Web Setup](#direct-web-setup), defaults to `false` |
52+
| Config key | |
53+
| ----------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54+
| `sassEntryFile` | Sass Entry File of you Application - relative path form config file |
55+
| `jsOutputFile` | full name of generated js file, can also be `<a-typescript-file>.ts` |
56+
| `sassOutputFile` | full name of generated sass file, should be included in your app styles |
57+
| `cssFallbackOutputFile` | full name of generated css file, should be included in your sass app styles (optional) |
58+
| `colorDefs` | color definitions, names have to match bulma color names (see examples above) |
59+
| `globalWebVar` | if you import js files directly in the browser, you need `true`, see [Direct Web Setup](#direct-web-setup), defaults to `false` |
5960
| `transition` | will create the [CSS transition](https://developer.mozilla.org/en-US/docs/Web/CSS/transition) shorthand for all colored CSS variables, should consist of `[ <duration> [ <timing-function> [ <time-delay> ] ] ]` |
6061

6162
Some more files have to be setup, which can be achieved using `node ./node_modules/.bin/bulma-css-vars --init`.
@@ -66,6 +67,7 @@ Some more files have to be setup, which can be achieved using `node ./node_modul
6667
@import './bulma-generated/generated-vars.sass';
6768
@import '../node_modules/bulma-css-vars/bulma-cv-lib';
6869
```
70+
6971
Instead of using `bulma-cv-lib.sass`, you can also just use the bulma packages you like. Look inside the `bulma-cv-lib.sass` to understand more, and especially import `functions.sass` right after the original `functions.sass` is loaded.
7072

7173
```json
@@ -74,46 +76,50 @@ Instead of using `bulma-cv-lib.sass`, you can also just use the bulma packages y
7476
"update-bulma-colors": "bulma-css-vars",
7577
}
7678
```
79+
7780
The script `./node_modules/.bin/bulma-css-vars` has to be run whenever you modify the colors in `bulma-css-vars.config.js` and it will update the three output files as well.
7881

7982
```js
8083
// in-the-web-app.js
81-
const { ColorUpdater } = require('bulma-css-vars');
82-
const { bulmaCssVariablesDefs } = require('./bulma-generated/bulma-colors');
84+
import { ColorUpdater } from 'bulma-css-vars'
85+
import { bulmaCssVariablesDefs } from './bulma-generated/bulma-colors'
8386

84-
const updater = new ColorUpdater(bulmaCssVariablesDefs);
87+
const updater = new ColorUpdater(bulmaCssVariablesDefs)
8588

8689
// do the update manually
87-
const updated = colorUpdater.getUpdatedVars(colorName, value);
90+
const updated = colorUpdater.getUpdatedVars(colorName, value)
8891
updated.forEach(({ name, value }) =>
8992
document.documentElement.style.setProperty(name, value)
90-
);
93+
)
9194
// or let it do the updater
92-
colorUpdater.updateVarsInDocument(colorName, value);
95+
colorUpdater.updateVarsInDocument(colorName, value)
9396
```
9497

9598
You can also use TypeScript
9699
```ts
97100
// in-the-web-app.ts
98-
import { ColorUpdater } from 'bulma-css-vars';
99-
import { bulmaCssVariablesDefs } from './bulma-generated/bulma-colors';
101+
import { ColorUpdater } from 'bulma-css-vars'
102+
import { bulmaCssVariablesDefs } from './bulma-generated/bulma-colors'
100103

101104
// the updater do the style change
102-
const colorUpdater = new ColorUpdater(bulmaCssVariablesDefs);
103-
colorUpdater.updateVarsInDocument(colorName, value);
105+
const colorUpdater = new ColorUpdater(bulmaCssVariablesDefs)
106+
colorUpdater.updateVarsInDocument(colorName, value)
104107
```
105108

106109
Annoyingly, the color updater needs knowledge of the current variables, so `bulmaCssVariablesDefs` from the generated js file has to be included in your app. `colorName` has to match the name in the `bulma-css-vars.config.js`.
107110

108111
### Caveats
112+
109113
* Requires Node.js and Dart Sass
110114
* The complexity of the setup
111115

112116
## What is the difficulty? Why this setup?
117+
113118
The problem is, that Bulma relies heavily on sass preprocessing, so if you set a color variable for `$primary`, this color will be modified for hover / focus shades on buttons, inversions for texts on buttons are calculated, and so on.
114119
If we would inject a CSS variable `var(--my-dynamic-primary-color)` here, not only will sass fail, also if you will change this color on runtime, still all the computed shades will remain calculated from the initial color.
115120

116121
## The solution - how it works
122+
117123
First off this library patches original sass functions such as `darken`, `lighten`, as well as bulma helper functions like `findColorInvert`. This is currently only possible thanks to the power of the dart `sass` package, which has more powerful possibilities than the `node-sass` package. The patched functions can handle `var(--variablename)` variables and decide if the original function will be called or if a new, derived variable of the incoming variable gets added.
118124

119125
For example, `darken(var(--primary), 10%)` will turn into `var(--primary--10--darken)`, and a derived variable gets added to the css stylesheets.
@@ -128,6 +134,7 @@ The class doing this should also be included into your application. Whenever you
128134
This way you can keep using the full bulma color richness. Try out the demo and see how the font of the buttons changes on dark / bright colors!
129135

130136
## Direct Web Setup
137+
131138
If you do not use any bundler or web framework, you can also include bulma-css-vars directly. You will still require Node.js and sass.
132139

133140
```sass
@@ -145,20 +152,21 @@ In your html:
145152
</head>
146153
<body>
147154
<!-- [your webpage] -->
148-
155+
149156
<!-- loads window.BulmaColorUpdater -->
150157
<script src="./node_modules/bulma-css-vars/dist/bulma-css-vars.web-bundle.js"></script>
151158

152159
<!-- loads window.bulmaCssVarsDef with your variable definitions -->
153160
<script src="./bulma-colors.js"></script>
154161
<script>
155-
const updater = new BulmaColorUpdater(bulmaCssVarsDef);
156-
updater.updateVarsInDocument('black', '#553292');
162+
const updater = new BulmaColorUpdater(bulmaCssVarsDef)
163+
updater.updateVarsInDocument('black', '#553292')
157164
</script>
158165
</body>
159166
```
160167

161168
## New Features
169+
162170
PRs are welcome!
163171
If you want to build this repository and try out the demo, make sure you use `yarn` and `yarn install` to install the packages in the `demo` folder. `npm` will create symlinks instead of true file copies, which might create problems on referencing relative bulma files.
164172

docs/bundle.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/bundle.js.LICENSE.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*!
2+
* vanilla-picker v2.12.1
3+
* https://vanilla-picker.js.org
4+
*
5+
* Copyright 2017-2021 Andreas Borgen (https://github.com/Sphinxxxx), Adam Brooks (https://github.com/dissimulate)
6+
* Released under the ISC license.
7+
*/

lib/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
{
22
"name": "bulma-css-vars",
3-
"version": "0.6.1",
3+
"version": "0.8.0",
44
"description": "Bulma with CSS variables",
55
"keywords": [
66
"bulma",
77
"bulma.io",
88
"CSS Variables"
99
],
10-
"main": "./dist/index.js",
11-
"browser": "./dist/index.js",
12-
"types": "./dist/index.d.ts",
10+
"main": "./dist/cjs/index.js",
11+
"browser": "./dist/esm/index.js",
12+
"module": "./dist/esm/index.js",
13+
"types": "./dist/types/index.d.ts",
14+
"exports": {
15+
".": {
16+
"import": "./dist/esm/index.js",
17+
"require": "./dist/cjs/index.js"
18+
},
19+
"./*": {
20+
"import": "./dist/esm/*.js",
21+
"require": "./dist/cjs/*.js"
22+
}
23+
},
1324
"files": [
1425
"/dist",
1526
"/bin",
@@ -19,7 +30,7 @@
1930
"bulma-css-vars": "bin/bulma-css-vars.js"
2031
},
2132
"scripts": {
22-
"build": "tsc && webpack",
33+
"build": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json && webpack",
2334
"prepare": "npm run build",
2435
"prepublishOnly": "npm run test && npm run build && cp ../README.md .",
2536
"test": "jest"

lib/tsconfig-esm.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "es2015",
5+
"outDir": "./dist/esm"
6+
}
7+
}

lib/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
33
"baseUrl": "src",
4-
"outDir": "./dist",
5-
"declarationDir": "./dist",
4+
"outDir": "./dist/cjs",
5+
"declarationDir": "./dist/types",
66
"declaration": true,
77
"module": "commonjs",
88
"moduleResolution": "node",

lib/webpack.config.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
const path = require('path')
22

3-
module.exports = {
3+
const esm = {
44
mode: 'production',
55
entry: './src/web-bundle-entry.ts',
66
output: {
7-
path: path.resolve(__dirname, 'dist'),
7+
path: path.resolve(__dirname, 'dist/esm'),
88
filename: 'bulma-css-vars.web-bundle.js',
99
},
1010
module: {
1111
rules: [
1212
{
1313
test: /\.ts$/,
1414
use: 'ts-loader',
15-
exclude: /node_modules/
15+
exclude: /node_modules/,
1616
},
17-
]
17+
],
1818
},
1919
target: 'web',
2020
resolve: {
21-
extensions: ['.ts', '.js']
21+
extensions: ['.ts', '.js'],
2222
},
2323
}
24+
const cjs = {
25+
mode: 'production',
26+
entry: './src/web-bundle-entry.ts',
27+
output: {
28+
path: path.resolve(__dirname, 'dist/cjs'),
29+
filename: 'bulma-css-vars.web-bundle.js',
30+
},
31+
module: {
32+
rules: [
33+
{
34+
test: /\.ts$/,
35+
use: 'ts-loader',
36+
exclude: /node_modules/,
37+
},
38+
],
39+
},
40+
target: 'node',
41+
resolve: {
42+
extensions: ['.ts', '.js'],
43+
},
44+
}
45+
46+
module.exports = [esm, cjs]

0 commit comments

Comments
 (0)