Skip to content

Commit 8e8691f

Browse files
goto-bus-stopyoshuawuyts
authored andcommitted
feat(transform): support subarg syntax in transform, closes #140 (#133)
* feat(transform): support subarg syntax in transform This allows you to pass options to sheetify plugins using the browserify subarg syntax on the cli: ```bash browserify -t [ sheetify -u [ sheetify-sass --indentedSyntax ] ] ``` * Remove duplicate `.concat(options.t)`, ref #130
1 parent 6f9c392 commit 8e8691f

5 files changed

Lines changed: 31 additions & 1 deletion

File tree

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ 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.transform = [].concat(options.transform || []).concat(options.t || [])
8685
mapLimit(options.transform, 1, iterate, function (err) {
8786
if (err) return done(err)
8887
done(null, src)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"rimraf": "^2.5.1",
5252
"sheetify-cssnext": "^1.0.0",
5353
"standard": "^8.0.0",
54+
"subarg": "^1.0.0",
5455
"tape": "^4.2.0"
5556
}
5657
}

test/fixtures/simple-plugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function (filename, source, options, done) {
2+
done(null, options.replace)
3+
}

test/plugins.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const browserify = require('browserify')
2+
const subarg = require('subarg')
23
const concat = require('concat-stream')
34
const through = require('through2')
45
const test = require('tape')
@@ -39,4 +40,19 @@ test('plugins', function (t) {
3940
return ws
4041
}
4142
})
43+
44+
t.test('should support subarg syntax', function (t) {
45+
const bpath = path.join(__dirname, 'fixtures/plugins-source.js')
46+
const bOpts = { browserField: false }
47+
const sTransform = require.resolve('./fixtures/simple-plugin')
48+
const args = subarg([ '--transform', '[', sTransform, '--replace', '.test{}', ']' ])
49+
browserify(bpath, bOpts)
50+
.transform(sheetify, args)
51+
.exclude('sheetify/insert')
52+
.bundle()
53+
.pipe(concat(function (bundle) {
54+
t.notEqual(bundle.indexOf(`require('sheetify/insert')(".test{}")`), -1)
55+
t.end()
56+
}))
57+
})
4258
})

transform.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ function transform (filename, options) {
2424
out: ''
2525
})
2626

27+
opts.transform = [].concat(opts.transform || []).concat(opts.t || []).map(function (plugin) {
28+
// resolve subarg syntax
29+
if (typeof plugin === 'object' && Array.isArray(plugin._)) {
30+
return [
31+
plugin._[0],
32+
plugin
33+
]
34+
}
35+
return plugin
36+
})
37+
2738
const bufs = []
2839
const transformStream = through(write, end)
2940
return transformStream

0 commit comments

Comments
 (0)