Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ but templates were used in an attempt to make it easier to contribute to this pr

See [`docs/lab-json.md`](docs/lab-json.md)

## webpack-loader

The lab-loader imports a `lab.json` file and converts it into [styled-components][sc] for use in webpack.

```js
// example webpack.config.js
module.exports = {
module: {
rules: [
{
test: /lab\.json$/,
loader: '@compositor/lab/loader'
}
]
}
}
```

```js
// example usage
import { Heading, Button } from './lab.json'

const example = (
<div>
<Heading>Hello</Heading>
<Button>Beep</Button>
</div>
)
```

## Roadmap

- Vue.js export (currently in alpha)
Expand Down
51 changes: 51 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// lab webpack loader
// currently only supports styled-components
const { getOptions } = require('loader-utils')
const lab = require('./index')

const template = ({
name,
type,
propsString,
styleString,
system = []
}) => (`
export const ${name} = styled(${type})([], props => (${styleString}),
space,
fontSize,
width,
color,
${system.map(name => `system.${name}`)}
)
${name}.defaultProps = ${propsString}
${name}.displayName = '${name}'
`)

module.exports = function (src) {
const callback = this.async()
const opts = getOptions(this)

const config = JSON.parse(src)
const modules = lab(config, {
harmony: true,
template
})
.filter(m => m.name !== 'index')
const moduleNames = modules.map(m => m.name)

const code = `
import styled from 'styled-components'
import system, {
space,
fontSize,
width,
color
} from 'styled-system'
${modules.map(m => m.module).join('\n\n')}
export default {
${moduleNames.join(',\n')}
}
`

callback(null, code)
}
1 change: 1 addition & 0 deletions loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/loader')