Skip to content

Commit 6b3e0de

Browse files
asvnyrichard-lopes
authored andcommitted
Add support for unpkg.com and upgrade build setup (#101)
* Add support for unkpg.com and upgrade build setup * Remove unsed package and syntax fix
1 parent 543eff3 commit 6b3e0de

13 files changed

+7273
-111
lines changed

.babelrc

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"presets": [
33
[
4-
"env", {
4+
"@babel/env", {
5+
"modules": false,
56
"exclude": [
6-
"transform-es2015-typeof-symbol"
7+
"transform-typeof-symbol"
78
]
89
}
910
],
10-
"react"
11+
"@babel/react"
1112
],
12-
"plugins": [
13-
[
14-
"module-resolver",
15-
{
16-
"alias": {
17-
"cxs": "./src"
18-
}
19-
}
20-
]
21-
]
22-
}
13+
14+
"env": {
15+
"test": {
16+
"plugins": [
17+
"add-module-exports",
18+
"transform-es2015-modules-commonjs"
19+
],
20+
}
21+
}
22+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ coverage
33
dist
44
package-lock.json
55
coverage.lcov
6+
node_modules
7+
tmp
8+
yarn-error.log

package.json

+39-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"version": "6.2.0",
44
"description": "Fast af css-in-js in under 1kb",
55
"main": "dist/index.js",
6+
"unpkg": "dist/index.js",
67
"scripts": {
7-
"prepublish": "babel src -d dist",
8+
"prepublish": "rollup -c",
89
"build": "node docs/build.js",
910
"start": "webpack-dev-server",
1011
"size": "npm run prepublish && bundlesize",
@@ -13,36 +14,61 @@
1314
"test": "nyc ava"
1415
},
1516
"keywords": [],
17+
"files": [
18+
"dist/component.js",
19+
"dist/component.min.js",
20+
"dist/createTheme.js",
21+
"dist/createTheme.min.js",
22+
"dist/index.js",
23+
"dist/index.min.js",
24+
"dist/monocomponent.js",
25+
"dist/monocomponent.min.js",
26+
"dist/monolithic.js",
27+
"dist/monolithic.min.js",
28+
"dist/ThemeProvider.js",
29+
"dist/ThemeProvider.min.js"
30+
],
1631
"author": "Brent Jackson",
1732
"license": "MIT",
1833
"devDependencies": {
19-
"ava": "^0.21.0",
20-
"babel-cli": "^6.24.1",
21-
"babel-loader": "^7.1.1",
22-
"babel-plugin-module-resolver": "^2.7.1",
23-
"babel-preset-env": "^1.6.0",
24-
"babel-preset-react": "^6.24.1",
25-
"babel-register": "^6.26.0",
34+
"@babel/cli": "^7.0.0",
35+
"@babel/core": "^7.0.0",
36+
"@babel/preset-env": "^7.0.0",
37+
"@babel/preset-react": "^7.0.0",
38+
"@babel/register": "^7.0.0",
39+
"ava": "1.0.0-beta.1",
40+
"babel-loader": "^8.0.2",
41+
"babel-plugin-add-module-exports": "^0.2.1",
42+
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
2643
"browser-env": "^3.1.0",
2744
"bundlesize": "^0.12.1",
2845
"bytes": "^2.5.0",
29-
"codecov": "^2.3.0",
46+
"codecov": "^3.1.0",
3047
"loglevel": "^1.4.1",
31-
"nyc": "^11.0.3",
48+
"nyc": "^13.0.1",
3249
"react": "^15.6.1",
3350
"react-dom": "^15.6.1",
3451
"react-live": "^1.7.0",
3552
"react-test-renderer": "^15.6.1",
53+
"rollup": "^0.65.2",
54+
"rollup-plugin-babel": "^4.0.3",
55+
"rollup-plugin-commonjs": "^9.1.6",
56+
"rollup-plugin-node-resolve": "^3.4.0",
57+
"rollup-plugin-uglify": "^5.0.2",
3658
"standard": "^10.0.2",
3759
"styled-system": "^1.0.0-12",
3860
"webpack": "^3.3.0",
3961
"webpack-dev-server": "^2.6.0"
4062
},
4163
"ava": {
4264
"require": [
43-
"babel-register"
65+
"@babel/register"
4466
],
45-
"babel": "inherit"
67+
"babel": {
68+
"testOptions": {
69+
"babelrc": true
70+
}
71+
}
4672
},
4773
"nyc": {
4874
"exclude": [
@@ -68,7 +94,7 @@
6894
},
6995
{
7096
"path": "./dist/component.js",
71-
"maxSize": "0.7 kb"
97+
"maxSize": "0.75 kb"
7298
},
7399
{
74100
"path": "./dist/monolithic.js",

rollup.config.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import babel from 'rollup-plugin-babel'
2+
import { uglify } from 'rollup-plugin-uglify'
3+
import commonjs from 'rollup-plugin-commonjs'
4+
import resolve from 'rollup-plugin-node-resolve'
5+
import path from 'path'
6+
7+
let cxsIndex = path.resolve(__dirname, 'src/index.js')
8+
let cxsMonolithic = path.resolve(__dirname, 'src/monolithic.js')
9+
let cxsCreateTheme = path.resolve(__dirname, 'src/createTheme.js')
10+
11+
function inputFactory (input, name, isProduction) {
12+
let pluginForProduction = isProduction ? [uglify()] : []
13+
let outputName = input + (isProduction ? '.min' : '')
14+
15+
return {
16+
input: path.resolve(__dirname, `src/${input}.js`),
17+
output: {
18+
file: path.resolve(__dirname, `dist/${outputName}.js`),
19+
format: 'umd',
20+
name,
21+
globals: {
22+
'react': 'React',
23+
'react-dom': 'ReactDOM',
24+
'prop-types': 'PropTypes',
25+
[cxsIndex]: 'cxs',
26+
[cxsCreateTheme]: 'cxsCreateTheme',
27+
[cxsMonolithic]: 'cxsMonolithic'
28+
}
29+
},
30+
plugins: [ resolve(), commonjs(), babel() ].concat(pluginForProduction),
31+
external: ['react', 'react-dom', 'prop-types', cxsIndex, cxsMonolithic, cxsCreateTheme]
32+
}
33+
}
34+
35+
export default [
36+
inputFactory('index', 'cxs'),
37+
inputFactory('component', 'cxsComponent'),
38+
inputFactory('createTheme', 'cxsCreateTheme'),
39+
inputFactory('monocomponent', 'cxsMonocomponent'),
40+
inputFactory('monolithic', 'cxsMonolithic'),
41+
inputFactory('ThemeProvider', 'cxsThemeProvider'),
42+
43+
inputFactory('index', 'cxs', true),
44+
inputFactory('component', 'cxsComponent', true),
45+
inputFactory('createTheme', 'cxsCreateTheme', true),
46+
inputFactory('monocomponent', 'cxsMonocomponent', true),
47+
inputFactory('monolithic', 'cxsMonolithic', true),
48+
inputFactory('ThemeProvider', 'cxsThemeProvider', true)
49+
]

src/ThemeProvider.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const React = require('react')
2-
const PropTypes = require('prop-types')
3-
const createTheme = require('./createTheme')
1+
import createTheme from './createTheme'
2+
import PropTypes from 'prop-types'
3+
import React from 'react'
4+
45
const h = React.createElement
56

67
class ThemeProvider extends React.Component {
@@ -24,4 +25,4 @@ ThemeProvider.childContextTypes = {
2425
])
2526
}
2627

27-
module.exports = ThemeProvider
28+
export default ThemeProvider

src/component.js

+40-34
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1-
const h = require('react').createElement
2-
const PropTypes = require('prop-types')
3-
const cxs = require('./index')
4-
5-
module.exports = C => (...args) => {
6-
const Comp = (props, context = {}) => {
7-
const stylePropKeys = [
8-
...Object.keys(Comp.propTypes || {}),
9-
'css'
10-
]
11-
const styleProps = Object.assign({ theme: context.theme || {} }, props)
12-
13-
const next = {}
14-
for (let key in props) {
15-
if (stylePropKeys.includes(key)) continue
16-
next[key] = props[key]
17-
}
18-
next.className = [
19-
next.className,
20-
...args.map(a => typeof a === 'function' ? a(styleProps) : a)
1+
import _cxs from './index'
2+
import PropTypes from 'prop-types'
3+
import React from 'react'
4+
5+
const h = React.createElement
6+
7+
function cxs (C) {
8+
return (...args) => {
9+
const Comp = (props, context = {}) => {
10+
const stylePropKeys = [
11+
...Object.keys(Comp.propTypes || {}),
12+
'css'
13+
]
14+
const styleProps = Object.assign({ theme: context.theme || {} }, props)
15+
16+
const next = {}
17+
for (let key in props) {
18+
if (stylePropKeys.includes(key)) continue
19+
next[key] = props[key]
20+
}
21+
next.className = [
22+
next.className,
23+
...args.map(a => typeof a === 'function' ? a(styleProps) : a)
2124
.filter(s => !!s)
22-
.map(s => cxs(s)),
23-
cxs(props.css || {})
24-
].join(' ').trim()
25+
.map(s => _cxs(s)),
26+
_cxs(props.css || {})
27+
].join(' ').trim()
2528

26-
return h(C, next)
27-
}
29+
return h(C, next)
30+
}
2831

29-
Comp.contextTypes = {
30-
theme: PropTypes.oneOfType([
31-
PropTypes.object,
32-
PropTypes.func
33-
])
34-
}
32+
Comp.contextTypes = {
33+
theme: PropTypes.oneOfType([
34+
PropTypes.object,
35+
PropTypes.func
36+
])
37+
}
3538

36-
return Comp
39+
return Comp
40+
}
3741
}
3842

39-
module.exports.css = cxs.css
40-
module.exports.reset = cxs.reset
43+
cxs.css = _cxs.css
44+
cxs.reset = _cxs.reset
45+
46+
export default cxs

src/createTheme.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
module.exports = theme => Object.assign(function (keys) {
2-
return keys.split('.')
1+
function createTheme (theme) {
2+
return Object.assign(function (keys) {
3+
return keys.split('.')
34
.reduce((a, b) => (a && a[b]) ? a[b] : null, theme)
4-
}, theme)
5+
}, theme)
6+
}
7+
8+
export default createTheme

src/index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@ const parse = (obj, child = '', media) =>
2525
})
2626
.join(' ')
2727

28-
module.exports = (...styles) =>
29-
styles.map(style => parse(style))
30-
.join(' ').trim()
28+
function cxs (...styles) {
29+
return styles
30+
.map(style => parse(style))
31+
.join(' ')
32+
.trim()
33+
}
3134

32-
module.exports.css = () => rules.sort().join('')
35+
cxs.css = () => rules.sort().join('')
3336

34-
module.exports.reset = () => {
37+
cxs.reset = () => {
3538
cache = {}
3639
while (rules.length) rules.pop()
3740
}
3841

39-
module.exports.prefix = val => prefix = val
42+
cxs.prefix = val => (prefix = val)
4043

4144
if (typeof document !== 'undefined') {
4245
const sheet = document.head.appendChild(
@@ -47,3 +50,5 @@ if (typeof document !== 'undefined') {
4750
sheet.insertRule(rule, sheet.cssRules.length)
4851
}
4952
}
53+
54+
export default cxs

0 commit comments

Comments
 (0)