Skip to content

Commit

Permalink
Merge pull request #21 from tivac/rollup
Browse files Browse the repository at this point in the history
Rollup plugin & exports rework
  • Loading branch information
tivac committed Mar 30, 2016
2 parents 6e93373 + aaba56b commit 55f2e1c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 29 deletions.
4 changes: 2 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

var fs = require("fs"),

objectify = require("../index.js").objectify;
transform = require("../");

fs.readFile(process.argv[2], function(errin, file) {
var result;
Expand All @@ -13,7 +13,7 @@ fs.readFile(process.argv[2], function(errin, file) {
throw new Error(errin);
}

result = objectify(file);
result = transform(file);

if(process.argv[3]) {
return fs.writeFile(process.argv[3], result, function(errout) {
Expand Down
20 changes: 20 additions & 0 deletions browserify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

var path = require("path"),

through = require("through2"),
sink = require("sink-transform"),

transform = require("./");

module.exports = function browserify(file) {
if(path.extname(file) !== ".js") {
return through();
}

return sink.str(function(source, done) {
this.push(transform(source).toString("utf8"));

done();
});
};
22 changes: 2 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
"use strict";

var path = require("path"),

through = require("through2"),
sink = require("sink-transform"),
falafel = require("falafel"),
var falafel = require("falafel"),

objectify = require("./src/objectify");

function transform(source) {
module.exports = function transform(source) {
return falafel({
source : source,
ecmaVersion : 6
}, objectify);
}

module.exports = function(file) {
if(path.extname(file) !== ".js") {
return through();
}

return sink.str(function(source, done) {
this.push(transform(source).toString());

done();
});
};

module.exports.objectify = transform;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},
"devDependencies": {
"concat-stream": "^1.5.0",
"eslint": "^2.0.0",
"eslint-config-arenanet": "^2.0.1",
"eslint": "^2.5.3",
"eslint-config-arenanet": "^2.0.3",
"istanbul": "^0.4.0",
"mithril": "^0.2.0",
"tap-difflet": "^0.4.0",
Expand Down
24 changes: 21 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Turn [mithril](http://mithril.js.org) html functions like `m(".fooga")` into sta

for speeeeeed.

Use via CLI, API, or as a [Browserify](http://browserify.org/) transform!
Use via CLI, API, [Browserify](http://browserify.org/), or [Rollup](http://rollupjs.org)!

Please file an issue if you come across any cases that this doesn't handle, I'd love to improve the number of structures I can rewrite!

Expand All @@ -51,7 +51,7 @@ Accepts an input file and optional output file. No output file will echo the res
Accepts a string or buffer, returns a buffer.

```js
var objectify = require("mithril-objectify").objectify;
var objectify = require("mithril-objectify");

console.log(objectify(`m(".fooga.wooga.booga")`);

Expand All @@ -70,9 +70,27 @@ Use as a browserify transform, either via the CLI or via code.
```js
var build = require("browserify")();

build.transform("mithril-objectify");
build.transform("mithril-objectify/browserify");

b.add("./client.js");

b.bundle().pipe(process.stdout);
```
### Rollup
Use as a rollup plugin.
```js
rollup.rollup({
entry : "./entry.js",
plugins : [
require("mithril-objectify/rollup")()
]
})
.then(function(bundle) {
return bundle.write({
dest : "./out/source.js"
});
})
```
13 changes: 13 additions & 0 deletions rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

var transform = require("./");

module.exports = function rollup() {
return {
transformBundle : function(code) {
return {
code : transform(code).toString("utf8")
};
}
};
};
2 changes: 1 addition & 1 deletion test/_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var vm = require("vm"),

objectify = require("../").objectify;
objectify = require("../");

function process(code) {
return objectify(code).toString("utf8");
Expand Down
2 changes: 1 addition & 1 deletion test/_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = function(file, contents, done) {
var Readable = require("stream").Readable,
concat = require("concat-stream"),
objectify = require("../"),
objectify = require("../browserify"),
readable = new Readable(),
through;

Expand Down
20 changes: 20 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,23 @@ test("transform", function(t) {
);
});
});

test("Rollup plugin", function(t) {
var r = require("../rollup"),
obj;

t.plan(5);

t.equal(typeof r, "function");

obj = r();

t.equal(typeof obj, "object");
t.ok("transformBundle" in obj);

t.equal(typeof obj.transformBundle, "function");
t.deepEqual(
obj.transformBundle('m("#fooga")'),
{ code : '({ tag: "div", attrs: { "id": "fooga" }, children: [] })' }
);
});

0 comments on commit 55f2e1c

Please sign in to comment.