Skip to content

Commit 683539e

Browse files
author
Alexander Ryzhikov
committed
Postcss-js droped
1 parent e26917b commit 683539e

File tree

8 files changed

+54
-46
lines changed

8 files changed

+54
-46
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"lodash": "^4.16.4",
4343
"postcss": "^5.2.5",
4444
"postcss-cssnext": "^2.8.0",
45-
"postcss-import": "7.1.3",
46-
"postcss-js": "^0.1.3"
45+
"postcss-import": "7.1.3"
4746
},
4847
"devDependencies": {
4948
"ava": "^0.16.0",

src/lib/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ export default function (source) {
1111

1212
const transformToConfig = options.es5 ? utils.toES5Config : utils.toConfig
1313

14-
const end = (err, data = {}, map) => {
14+
const end = (err, result, path, map) => {
1515
if (err) {
1616
_done(err)
1717
}
18-
return _done(null, transformToConfig(data), map)
18+
19+
if (!result) {
20+
return _done(null, {}, map)
21+
}
22+
23+
return _done(null, transformToConfig(result.root, path), map)
1924
}
2025

2126
const emitWarnings = (result) => {
@@ -35,6 +40,6 @@ export default function (source) {
3540
utils.getPostcss(true)
3641
.process(source, { from: this.resourcePath })
3742
.then(emitWarnings)
38-
.then((result) => end(null, utils.objectify(result.root, this.resourcePath), result.map))
43+
.then((result) => end(null, result, this.resourcePath, result.map))
3944
.catch(onError)
4045
}

src/lib/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export default function sync (source, filepath, options = {}) {
44
const transformToConfig = options.es5 ? utils.toES5Config : utils.toConfig
55
const root = utils.getPostcss(false).process(source, { from: filepath }).root
66

7-
return transformToConfig(utils.objectify(root, filepath))
7+
return transformToConfig(root, filepath)
88
}

src/lib/utils.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
import postcssJs from 'postcss-js'
21
import compose from 'lodash/fp/compose'
3-
import get from 'lodash/fp/get'
42
import cond from 'lodash/fp/cond'
5-
import lowerFirst from 'lodash/lowerFirst'
63
import postcss from 'postcss'
74
import cssnext from 'postcss-cssnext'
85
import atImport from 'postcss-import'
96

107
const isDev = () => process.env.NODE_ENV === 'development'
118
const isProd = () => !isDev()
129

13-
const transform = (data) => {
14-
if (!data) {
15-
return {}
16-
}
17-
return Object
18-
.keys(data)
19-
.reduce((acc, k) => {
20-
const v = data[k]
21-
22-
k = k.replace(/^-+/, '') // strips '-'
23-
k = lowerFirst(k)
24-
acc[k] = v.endsWith('px') ? parseInt(v, 10) : v
25-
return acc
26-
}, {})
27-
}
28-
2910
const toProdExport = (code) => `export default ${code}`
3011
const toDevExport = (code) => `let config = ${code};
31-
if (typeof global.Proxy !== 'undefined') {
12+
if (typeof Proxy !== 'undefined') {
3213
config = new Proxy(config, {
3314
get(target, key) {
3415
if (key !== '__esModule' && !target[key]) {
@@ -52,21 +33,29 @@ const toExport = cond([
5233
])
5334

5435
const toString = (data) => `${JSON.stringify(data, null, '\t')}`
55-
const toData = compose(transform, get(':root'))
5636

57-
export const toConfig = compose(toExport, toString, toData)
58-
export const toES5Config = compose(toES5Export, toString, toData)
37+
const objectify = (root, filepath) => {
38+
const result = {}
39+
40+
if (!root) {
41+
return result
42+
}
43+
44+
root.walkDecls((rule) => {
45+
if (rule.source.input.file !== filepath) {
46+
return
47+
}
48+
if (rule.parent && rule.parent.selectors.find((sel) => sel === ':root')) {
49+
const v = rule.value // replace "--"
5950

60-
export const objectify = (root, filepath) => {
61-
// removes imported rules, so we have only computed output
62-
root.walkRules((r) => {
63-
if (r.source.input.file !== filepath) {
64-
r.remove()
51+
result[rule.prop.replace(/^-+/, '')] = v.endsWith('px') ? parseInt(v, 10) : v
6552
}
6653
})
67-
return postcssJs.objectify(root)
54+
return result
6855
}
6956

57+
export const toConfig = compose(toExport, toString, objectify)
58+
export const toES5Config = compose(toES5Export, toString, objectify)
7059
export const getPostcss = (async) => postcss()
7160
.use(atImport({ async }))
7261
.use(cssnext({ features: { customProperties: { preserve: 'computed' } } }))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import './basic.css';
2+
3+
:root {
4+
--size: 1rem;
5+
}

test/fixtures/multipleRoots.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:root {
2+
--size: 10em;
3+
}
4+
5+
:root {
6+
--size: 11em;
7+
--color: violet;
8+
}

test/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ test('imports drop', async (t) => {
2828
t.is(typeof result.size, 'undefined')
2929
})
3030

31+
test('imports overwrite', async (t) => {
32+
const result = await runner('./computedOverwrite.css')
33+
34+
t.true(result.size === '1rem')
35+
})
36+
3137
test('works with @apply', async (t) => {
3238
const result = await runner('./apply.css')
3339

@@ -49,3 +55,10 @@ test('sync loader works with es5 ', (t) => {
4955

5056
t.true(result.size === '10em')
5157
})
58+
59+
test('works with multiple roots', async (t) => {
60+
const result = await runner('./multipleRoots.css')
61+
62+
t.true(result.size === '11em')
63+
t.true(result.color === 'violet')
64+
})

yarn.lock

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,6 @@ callsites@^0.2.0:
11721172
version "0.2.0"
11731173
resolved "https://registry.yarnpkg.com/callsites/-/callsites-0.2.0.tgz#afab96262910a7f33c19a5775825c69f34e350ca"
11741174

1175-
camelcase-css@^1.0.1:
1176-
version "1.0.1"
1177-
resolved "https://registry.yarnpkg.com/camelcase-css/-/camelcase-css-1.0.1.tgz#157c4238265f5cf94a1dffde86446552cbf3f705"
1178-
11791175
camelcase-keys@^2.0.0:
11801176
version "2.1.0"
11811177
resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-2.1.0.tgz#308beeaffdf28119051efa1d932213c91b8f92e7"
@@ -3492,13 +3488,6 @@ postcss-initial@^1.3.1:
34923488
lodash.template "^4.2.4"
34933489
postcss "^5.0.19"
34943490

3495-
postcss-js@^0.1.3:
3496-
version "0.1.3"
3497-
resolved "https://registry.yarnpkg.com/postcss-js/-/postcss-js-0.1.3.tgz#8c0a5daa1c918b3073c4806a3c5b332c67250c03"
3498-
dependencies:
3499-
camelcase-css "^1.0.1"
3500-
postcss "^5.0.21"
3501-
35023491
postcss-media-minmax@^2.1.0:
35033492
version "2.1.2"
35043493
resolved "https://registry.yarnpkg.com/postcss-media-minmax/-/postcss-media-minmax-2.1.2.tgz#444c5cf8926ab5e4fd8a2509e9297e751649cdf8"

0 commit comments

Comments
 (0)