Skip to content

Commit 875753f

Browse files
authored
use transform (#135)
* use transform * Add transforms from package.json (#136) * Remove unnecessary options from transform-package tests (#137)
1 parent 5ba9874 commit 875753f

File tree

10 files changed

+132
-30
lines changed

10 files changed

+132
-30
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ like [browserify](http://browserify.org/) does.
1111
## Features
1212
- __clarity:__ namespace CSS, no more need for naming schemes
1313
- __modularity:__ import and reuse CSS packages from npm
14-
- __extensibility:__ transform CSS using existing plugins, or write your own
14+
- __extensibility:__ transform CSS using existing transforms, or write your own
1515
- __transparency:__ inline CSS in your views
1616
- __simplicity:__ tiny API surface and a minimal code base
1717

@@ -140,8 +140,8 @@ $ browserify index.js \
140140
[css-extract][2] can also write to a stream from the JS api, look at the
141141
documentation to see how.
142142

143-
## Plugins
144-
Sheetify uses [plugins](#plugins) that take CSS and apply a transform.
143+
## Transforms
144+
Sheetify uses [transforms](#transforms) that take CSS and apply a transform.
145145
For example include
146146
[sheetify-cssnext](https://github.com/sheetify/sheetify-cssnext) to support
147147
autoprefixing, variables and more:
@@ -164,9 +164,9 @@ const tree = html`
164164
document.body.appendChild(tree)
165165
```
166166

167-
Compile with browserify using `-t [ sheetify -u sheetify-cssnext ]`:
167+
Compile with browserify using `-t [ sheetify -t sheetify-cssnext ]`:
168168
```sh
169-
$ browserify -t [ sheetify -u sheetify-cssnext ] index.js > bundle.js
169+
$ browserify -t [ sheetify -t sheetify-cssnext ] index.js > bundle.js
170170
```
171171

172172
Transforms the CSS into:
@@ -177,15 +177,11 @@ h1 {
177177
}
178178
```
179179

180-
The following plugins are available:
181-
- [sheetify-cssnext](https://github.com/sheetify/sheetify-cssnext) - use
182-
tomorrow's CSS syntax today
183-
184180
## API
185181
Browserify transforms accept either flags from the command line using
186182
[subargs](https://github.com/substack/subarg):
187183
```sh
188-
$ browserify -t [ sheetify -u sheetify-cssnext ] index.js > bundle.js
184+
$ browserify -t [ sheetify -t sheetify-cssnext ] index.js > bundle.js
189185
```
190186

191187
Or the equivalent options by passing in a configuration object in the
@@ -194,14 +190,14 @@ JavaScript API:
194190
const browserify = require('browserify')
195191

196192
const b = browserify(path.join(__dirname, 'transform/source.js'))
197-
b.transform('sheetify', { use: [ 'sheetify-cssnext' ] })
193+
b.transform('sheetify', { transform: [ 'sheetify-cssnext' ] })
198194
b.bundle().pipe(process.stdout)
199195
```
200196

201197
The following options are available:
202198
```txt
203199
Options:
204-
-u, --use Consume a sheetify plugin
200+
-t, --transform Consume a sheetify transform
205201
```
206202

207203
## FAQ

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ function parseCss (src, filename, prefix, options, done) {
8282
// one at the time
8383
// (str, str, obj, fn) -> null
8484
function applyTransforms (filename, src, options, done) {
85-
options.use = [].concat(options.use || []).concat(options.u || [])
86-
mapLimit(options.use, 1, iterate, function (err) {
85+
options.transform = [].concat(options.transform || []).concat(options.t || [])
86+
mapLimit(options.transform, 1, iterate, function (err) {
8787
if (err) return done(err)
8888
done(null, src)
8989
})

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
],
2323
"dependencies": {
2424
"falafel": "^2.1.0",
25+
"fast-json-parse": "^1.0.2",
26+
"findup": "^0.1.5",
2527
"insert-css": "^2.0.0",
2628
"map-limit": "0.0.1",
2729
"postcss": "^5.0.10",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
._75161010 .hello,._75161010 .world {
2+
-webkit-transform: translate(0, 0);
3+
transform: translate(0, 0);
4+
}
5+
._75161010 .hello,._75161010 .world {
6+
-webkit-transform: translate(0, 0);
7+
transform: translate(0, 0);
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"sheetify": {
3+
"transform": [
4+
[
5+
"sheetify-cssnext",
6+
{
7+
"sourcemap": false
8+
}
9+
]
10+
]
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const sf = require('sheetify')
2+
3+
const prefix = sf`
4+
:host .hello, :host .world {
5+
transform: translate(0, 0);
6+
}
7+
`
8+
9+
var foo = {}
10+
foo.bar = sf`
11+
:host .hello, :host .world {
12+
transform: translate(0, 0);
13+
}
14+
`
15+
16+
console.log(prefix)

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ require('./import')
33
require('./plugins')
44
require('./json')
55
require('./noparse')
6+
require('./transform-package')

test/plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('plugins', function (t) {
2323
const bpath = path.join(__dirname, 'fixtures/plugins-source.js')
2424
browserify(bpath, bOpts)
2525
.transform(sheetify, {
26-
use: [ [ 'sheetify-cssnext', { sourcemap: false } ] ]
26+
transform: [ [ 'sheetify-cssnext', { sourcemap: false } ] ]
2727
})
2828
.transform(function (file) {
2929
return through(function (buf, enc, next) {

test/transform-package.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const browserify = require('browserify')
2+
const concat = require('concat-stream')
3+
const through = require('through2')
4+
const test = require('tape')
5+
const path = require('path')
6+
const fs = require('fs')
7+
8+
const sheetify = require('../')
9+
10+
test('transform-package', function (t) {
11+
t.test('should transform CSS from package.json with options', function (t) {
12+
t.plan(1)
13+
14+
const expath = path.join(__dirname, 'fixtures/transform-package/expected.css')
15+
const expected = fs.readFileSync(expath, 'utf8').trim()
16+
17+
const ws = concat(function (buf) {
18+
const res = String(buf).trim()
19+
t.equal(res, expected, 'CSS was transformed')
20+
})
21+
22+
const bpath = path.join(__dirname, 'fixtures/transform-package/source.js')
23+
24+
browserify(bpath)
25+
.transform(sheetify)
26+
.transform(function (file) {
27+
return through(function (buf, enc, next) {
28+
const str = buf.toString('utf8')
29+
this.push(str.replace(/sheetify\/insert/g, 'insert-css'))
30+
next()
31+
})
32+
})
33+
.plugin('css-extract', { out: outFn })
34+
.bundle()
35+
36+
function outFn () {
37+
return ws
38+
}
39+
})
40+
})

transform.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const cssResolve = require('style-resolve').sync
22
const staticEval = require('static-eval')
3+
const parse = require('fast-json-parse')
34
const mapLimit = require('map-limit')
45
const through = require('through2')
56
const falafel = require('falafel')
7+
const findup = require('findup')
68
const xtend = require('xtend')
79
const path = require('path')
810
const fs = require('fs')
@@ -18,12 +20,10 @@ function transform (filename, options) {
1820

1921
const opts = xtend(options || {
2022
basedir: process.cwd(),
21-
use: [],
23+
transform: [],
2224
out: ''
2325
})
2426

25-
opts.use = [].concat(opts.use || []).concat(opts.u || [])
26-
2727
const bufs = []
2828
const transformStream = through(write, end)
2929
return transformStream
@@ -54,19 +54,25 @@ function transform (filename, options) {
5454
return
5555
}
5656

57-
try {
58-
const tmpAst = falafel(src, { ecmaVersion: 8 }, identifyModuleName)
59-
ast = falafel(tmpAst.toString(), { ecmaVersion: 8 }, extractNodes)
60-
} catch (err) {
61-
return self.emit('error', err)
62-
}
63-
64-
// transform all detected nodes and
65-
// close stream when done
66-
mapLimit(nodes, Infinity, iterate, function (err) {
57+
findTransforms(filename, function (err, transforms) {
6758
if (err) return self.emit('error', err)
68-
self.push(ast.toString())
69-
self.push(null)
59+
60+
opts.transform = transforms.concat(opts.transform || []).concat(opts.t || [])
61+
62+
try {
63+
const tmpAst = falafel(src, { ecmaVersion: 8 }, identifyModuleName)
64+
ast = falafel(tmpAst.toString(), { ecmaVersion: 8 }, extractNodes)
65+
} catch (err) {
66+
return self.emit('error', err)
67+
}
68+
69+
// transform all detected nodes and
70+
// close stream when done
71+
mapLimit(nodes, Infinity, iterate, function (err) {
72+
if (err) return self.emit('error', err)
73+
self.push(ast.toString())
74+
self.push(null)
75+
})
7076
})
7177

7278
function identifyModuleName (node) {
@@ -164,6 +170,27 @@ function transform (filename, options) {
164170
})
165171
}
166172
}
173+
174+
function findTransforms (filename, done) {
175+
findup(filename, 'package.json', function (err, pathname) {
176+
// no package.json found - just run local transforms
177+
if (err) return done(null, [])
178+
179+
var filename = path.join(pathname, 'package.json')
180+
fs.readFile(filename, function (err, file) {
181+
if (err) return done(err)
182+
var parsed = parse(file)
183+
if (parsed.err) return done(parsed.err)
184+
var json = parsed.value
185+
var d = json.sheetify
186+
if (!d) return done(null, [])
187+
var t = d.transform
188+
if (!t || !Array.isArray(t)) return done(null, [])
189+
190+
return done(null, t)
191+
})
192+
})
193+
}
167194
}
168195

169196
function cooked (node) { return node.value.cooked }

0 commit comments

Comments
 (0)