Skip to content

Commit fff265b

Browse files
committed
feat(styles): add get-data sass function with initial colors
1 parent 75bf480 commit fff265b

6 files changed

Lines changed: 74 additions & 3 deletions

File tree

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"lodash": "^4.17.11",
6868
"mini-css-extract-plugin": "^0.5.0",
6969
"node-sass": "^4.11.0",
70+
"node-sass-utils": "^1.1.2",
7071
"popper.js": "^1.15.0",
7172
"sass-loader": "^7.1.0",
7273
"semantic-release": "^15.13.3",

src/styles/constants.sass

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$NODE-GREEN: get-data('green')
2+
$NODE-GRAY: get-data('gray')

src/styles/helpers/getData.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict'
2+
3+
const sass = require('node-sass')
4+
const utils = require('node-sass-utils')(sass)
5+
6+
const DEFAULT_FALLBACK = utils.castToSass('#FF0000')
7+
8+
module.exports = data => {
9+
return {
10+
'get-data($key, $fallback: null)': (key, fallback) => {
11+
if (utils.isNull(fallback)) {
12+
fallback = DEFAULT_FALLBACK
13+
}
14+
15+
if (utils.typeOf(key) !== 'string') {
16+
return fallback
17+
}
18+
19+
key = key.getValue()
20+
21+
if (key === '') {
22+
return fallback
23+
}
24+
25+
const parts = key.split('.')
26+
const len = parts.length
27+
28+
let map = data
29+
30+
if (!map) {
31+
return fallback
32+
}
33+
34+
for (let i = 0, part; i < len; i++) {
35+
part = parts[i]
36+
37+
if (part in map) {
38+
map = map[part]
39+
} else {
40+
map = null
41+
}
42+
}
43+
44+
if (map === null) {
45+
return fallback
46+
}
47+
48+
return utils.castToSass(map)
49+
}
50+
}
51+
}

src/styles/index.sass

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
@import 'constants'
2+
13
body
2-
background: #CCC
4+
// background: $COLOR
5+
// background: get-data('brand.primary-color')
6+
background: $NODE-GREEN

webpack.config.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

33
const path = require('path')
4-
const data = require('./data')
54
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
65
const HtmlWebpackPlugin = require('html-webpack-plugin')
76

7+
const data = require('./data')
8+
const brand = require('@nodewell/brand')
9+
const getData = require('./src/styles/helpers/getData')
10+
811
module.exports = {
912
entry: './src/scripts/index.js',
1013
output: {
@@ -52,7 +55,11 @@ module.exports = {
5255
{
5356
loader: 'sass-loader',
5457
options: {
55-
sourceMap: true
58+
sourceMap: true,
59+
// data: `$COLOR: green`,
60+
functions: {
61+
...getData(brand)
62+
}
5663
}
5764
}
5865
]

0 commit comments

Comments
 (0)