Skip to content

Commit

Permalink
feat: support emotion
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Jun 7, 2019
1 parent 8cb7bc9 commit 4b600a4
Show file tree
Hide file tree
Showing 49 changed files with 1,016 additions and 165 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
dist/
/coverage/
.cache/
public/
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
/coverage/
/coverage/
.size-snapshot.json
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
dist/
package.json
CHANGELOG.md
.cache
13 changes: 7 additions & 6 deletions config/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import commonjs from 'rollup-plugin-commonjs'
import { terser } from 'rollup-plugin-terser'
import { sizeSnapshot } from 'rollup-plugin-size-snapshot'

export const getRollupConfig = ({ pwd, buildName, name }) => {
export const getRollupConfig = ({
pwd,
buildName,
name = null,
globals = {},
}) => {
const SOURCE_DIR = path.resolve(pwd, 'src')
const DIST_DIR = path.resolve(pwd, 'dist')
const input = `${SOURCE_DIR}/index.js`
Expand All @@ -21,10 +26,6 @@ export const getRollupConfig = ({ pwd, buildName, name }) => {
],
})

const globals = {
'styled-components': 'styled',
}

const umdConfig = {
input,
output: {
Expand Down Expand Up @@ -78,5 +79,5 @@ export const getRollupConfig = ({ pwd, buildName, name }) => {
return [esmConfig, cjsConfig]
}

return [esmConfig, cjsConfig, umdConfig, minConfig]
return [esmConfig, cjsConfig, ...(name ? [umdConfig, minConfig] : [])]
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
"bundlesize": [
{
"path": "./packages/system/dist/xstyled-system.min.js",
"maxSize": "4.5 kB"
"maxSize": "4.1 kB"
},
{
"path": "./packages/styled-components/dist/xstyled.min.js",
"maxSize": "4 kB"
"path": "./packages/styled-components/dist/xstyled-styled-components.min.js",
"maxSize": "1.5 kB"
},
{
"path": "./packages/emotion/dist/xstyled-emotion.min.js",
"maxSize": "1.7 kB"
}
],
"scripts": {
Expand All @@ -28,13 +32,16 @@
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/preset-react": "^7.0.0",
"@emotion/core": "^10.0.10",
"@emotion/styled": "^10.0.11",
"@testing-library/react": "^8.0.1",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.8.0",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"bundlesize": "^0.17.2",
"codecov": "^3.5.0",
"conventional-github-releaser": "^3.1.3",
"emotion-theming": "^10.0.10",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^4.3.0",
Expand All @@ -47,16 +54,17 @@
"jest-dom": "^3.4.0",
"jest-styled-components": "^6.3.1",
"lerna": "^3.14.1",
"prettier": "^1.18.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"rollup": "^1.13.1",
"rollup": "^1.14.3",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-node-resolve": "^5.0.1",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-size-snapshot": "^0.9.0",
"rollup-plugin-terser": "^5.0.0",
"shx": "^0.3.2",
"styled-components": "^4.2.1"
"styled-components": "^4.3.1"
}
}
3 changes: 3 additions & 0 deletions packages/babel-preset-emotion-css-prop/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
!/dist/**/*.js
*.test.js
7 changes: 7 additions & 0 deletions packages/babel-preset-emotion-css-prop/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 Smooth Code

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions packages/babel-preset-emotion-css-prop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @emotion/babel-preset-emotion-css-prop

Same interface as [babel-preset-css-prop](https://github.com/emotion-js/emotion/tree/master/packages/babel-preset-css-prop).

See [xstyled documentation](https://www.smooth-code.com/open-source/xstyled/)
27 changes: 27 additions & 0 deletions packages/babel-preset-emotion-css-prop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@xstyled/babel-preset-emotion-css-prop",
"description": "A babel preset to automatically enable emotion's css prop with xstyled",
"version": "1.1.1",
"sideEffects": false,
"main": "dist/xstyled-babel-preset-emotion-css-prop.cjs.js",
"module": "dist/xstyled-babel-preset-emotion-css-prop.es.js",
"author": "Greg Bergé <berge.greg@gmail.com>",
"license": "MIT",
"scripts": {
"build": "rollup -c",
"prebuild": "shx rm -rf dist",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@babel/plugin-transform-react-jsx": "^7.3.0",
"@babel/runtime": "^7.4.4",
"@emotion/babel-plugin-jsx-pragmatic": "^0.1.2",
"babel-plugin-emotion": "^10.0.9"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
}
}
8 changes: 8 additions & 0 deletions packages/babel-preset-emotion-css-prop/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getRollupConfig } from '../../config/rollup'
import pkg from './package.json'

export default getRollupConfig({
pwd: __dirname,
buildName: 'xstyled-babel-preset-emotion-css-prop',
pkg,
})
47 changes: 47 additions & 0 deletions packages/babel-preset-emotion-css-prop/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// see https://github.com/emotion-js/emotion/blob/master/packages/babel-preset-css-prop/src/index.js
import jsx from '@babel/plugin-transform-react-jsx'
import pragmatic from '@emotion/babel-plugin-jsx-pragmatic'
import emotion from 'babel-plugin-emotion'

const pragmaName = '___xstyledEmotionJSX'

// pull out the emotion options and pass everything else to the jsx transformer
// this means if @babel/plugin-transform-react-jsx adds more options, it'll just work
// and if babel-plugin-emotion adds more options we can add them since this lives in
// the same repo as babel-plugin-emotion

export default (
api,
{
pragma,
sourceMap,
autoLabel,
labelFormat,
instances = [],
...options
} = {},
) => {
return {
plugins: [
[
pragmatic,
{
export: 'jsx',
module: '@xstyled/emotion',
import: pragmaName,
},
],
[jsx, { pragma: pragmaName, pragmaFrag: 'React.Fragment', ...options }],
[
emotion,
{
sourceMap,
autoLabel,
labelFormat,
instances: ['@xstyled/emotion', ...instances],
cssPropOptimization: true,
},
],
],
}
}
55 changes: 55 additions & 0 deletions packages/babel-preset-emotion-css-prop/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { transform } from '@babel/core'
import preset from './index'

const testPreset = code => {
const result = transform(code, {
presets: [preset],
configFile: false,
})

return result.code
}

const code = `
import * as React from 'react'
export let Button = props => {
return (
<>
<button
css={{
color: 'hotpink',
}}
{...props}
/>
</>
)
}
`

describe('preset', () => {
it('transforms code', () => {
expect(testPreset(code)).toMatchInlineSnapshot(`
"import { jsx as ___xstyledEmotionJSX } from \\"@xstyled/emotion\\";
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from 'react';
var _ref = process.env.NODE_ENV === \\"production\\" ? {
name: \\"1v4u9bq-Button\\",
styles: \\"color:hotpink;label:Button;\\"
} : {
name: \\"1v4u9bq-Button\\",
styles: \\"color:hotpink;label:Button;\\",
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInVua25vd24iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBT1UiLCJmaWxlIjoidW5rbm93biIsInNvdXJjZXNDb250ZW50IjpbIlxuICBpbXBvcnQgKiBhcyBSZWFjdCBmcm9tICdyZWFjdCdcblxuICBleHBvcnQgbGV0IEJ1dHRvbiA9IHByb3BzID0+IHtcbiAgICByZXR1cm4gKFxuICAgICAgPD5cbiAgICAgICAgPGJ1dHRvblxuICAgICAgICAgIGNzcz17e1xuICAgICAgICAgICAgY29sb3I6ICdob3RwaW5rJyxcbiAgICAgICAgICB9fVxuICAgICAgICAgIHsuLi5wcm9wc31cbiAgICAgICAgLz5cbiAgICAgIDwvPlxuICAgIClcbiAgfVxuIl19 */\\"
};
export let Button = props => {
return ___xstyledEmotionJSX(React.Fragment, null, ___xstyledEmotionJSX(\\"button\\", _extends({
css: _ref
}, props)));
};"
`)
})
})
3 changes: 3 additions & 0 deletions packages/core/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
!/dist/**/*.js
*.test.js
7 changes: 7 additions & 0 deletions packages/core/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 Smooth Code

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @xstyled/core

Core utilities of xstyled. This package must not be used directly.

See [xstyled documentation](https://www.smooth-code.com/open-source/xstyled/)
22 changes: 22 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@xstyled/core",
"description": "xstyled core utilities",
"version": "1.1.1",
"sideEffects": false,
"main": "dist/xstyled-core.cjs.js",
"module": "dist/xstyled-core.es.js",
"author": "Greg Bergé <berge.greg@gmail.com>",
"license": "MIT",
"scripts": {
"build": "rollup -c",
"prebuild": "shx rm -rf dist",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@babel/runtime": "^7.4.4",
"@xstyled/system": "^1.1.1"
}
}
8 changes: 8 additions & 0 deletions packages/core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getRollupConfig } from '../../config/rollup'
import pkg from './package.json'

export default getRollupConfig({
pwd: __dirname,
buildName: 'xstyled-core',
pkg,
})
5 changes: 5 additions & 0 deletions packages/core/src/createBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { system } from '@xstyled/system'

export function createBox() {
return [`&&{`, system, `}`]
}
4 changes: 4 additions & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './createBox'
export * from './propGetters'
export * from './transform'
export * from './util'
File renamed without changes.
30 changes: 30 additions & 0 deletions packages/core/src/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable no-continue, no-loop-func, no-cond-assign */
import { propGetters } from './propGetters'

const PROP_REGEXP = /(\s*)([^&{}:;\n]+):\s*([^&{}:;\n]+)(\s*);/g
const IMPORTANT_REGEXP = /\s*!important\s*/

export function transform(rawValue) {
if (typeof rawValue !== 'string') return rawValue
let matches
let lastIndex = 0
const values = []
while ((matches = PROP_REGEXP.exec(rawValue))) {
const [, start, prop, propValue, end] = matches
const getter = propGetters[prop]
if (getter) {
const hasImportant = IMPORTANT_REGEXP.test(propValue)
const cleanValue = propValue.replace(IMPORTANT_REGEXP, '')
values.push(rawValue.slice(lastIndex, matches.index))
values.push(
p =>
`${start}${prop}: ${getter(cleanValue)(p)}${
hasImportant ? ' !important' : ''
};${end}`,
)
lastIndex = matches.index + matches[0].length
}
}
values.push(rawValue.slice(lastIndex, rawValue.length))
return values
}

0 comments on commit 4b600a4

Please sign in to comment.