From ba9b779ba7fcd11ed0a9c952f20d40fe411d39a3 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Mon, 24 Nov 2014 22:31:20 -0500 Subject: [PATCH] Integrating canopy (with a demo parser from the canopy docs) --- config.json | 5 ++--- gulpfile.js | 2 ++ karma.conf.js | 8 ++++++-- lib/canopy-transform.js | 24 ++++++++++++++++++++++++ package.json | 4 +++- src/js/main.js | 6 ++---- src/js/parser.peg | 4 ++++ src/js/test.js | 3 --- 8 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 lib/canopy-transform.js create mode 100644 src/js/parser.peg delete mode 100644 src/js/test.js diff --git a/config.json b/config.json index bc4a068..ab1aff0 100644 --- a/config.json +++ b/config.json @@ -4,7 +4,7 @@ "other": "./src/**/*.!(html|scss|js)", "html": "./src/**/*.html", "sass": "./src/**/*.scss", - "js": "./src/**/*.js", + "js": ["./src/**/*.js", "./src/**/*.peg"], "spec": "./spec/**/*_spec.js" }, "compass": { @@ -17,7 +17,6 @@ "browserify": { "basedir": "./src/js", "debug": true, - "fullPaths": false, - "transform": ["es6ify"] + "fullPaths": false } } diff --git a/gulpfile.js b/gulpfile.js index 2ad9b2b..ee111b8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -17,6 +17,8 @@ function browserifyPipe() { var bundler = browserify(config.browserify); bundler.add([file.path, es6ify.runtime]); + bundler.transform(require('./lib/canopy-transform')); + bundler.transform(es6ify); file.contents = bundler.bundle(); }); diff --git a/karma.conf.js b/karma.conf.js index 4224ead..81f1b87 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,7 +2,7 @@ var _ = require('lodash'), config = require('./config'); module.exports = function(karma) { - var globs = [config.globs.js, config.globs.spec]; + var globs = _.flatten([config.globs.js, config.globs.spec]); karma.set({ frameworks: ['browserify', 'jasmine'], @@ -19,7 +19,11 @@ module.exports = function(karma) { singleRun: false, browserify: _.defaults({}, config.browserify, { prebundle: function(bundle) { - bundle.add([require('es6ify').runtime]); + var es6ify = require('es6ify'); + + bundle.add([es6ify.runtime]); + bundle.transform(require('./lib/canopy-transform')); + bundle.transform(es6ify); } }) }); diff --git a/lib/canopy-transform.js b/lib/canopy-transform.js new file mode 100644 index 0000000..f475022 --- /dev/null +++ b/lib/canopy-transform.js @@ -0,0 +1,24 @@ +var through = require('through'), + canopy = require('canopy'); + +module.exports = function(file) { + if (!/\.peg$/.test(file)) { + return through(); + } + + var data = ''; + + return through( + function(buf) { + data += buf; + }, + function() { + try { + this.queue(String(canopy.compile(data))); + } catch(err) { + this.emit('error', err); + } + this.queue(null); + } + ); +}; diff --git a/package.json b/package.json index f8dcda0..778bebf 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "private": true, "devDependencies": { "browserify": "^6.3.2", + "canopy": "^0.2.0", "es6ify": "^1.5.1", "gulp": "^3.8.10", "gulp-compass": "^2.0.3", @@ -25,6 +26,7 @@ "karma-jasmine": "^0.3.1", "karma-notify-reporter": "^0.1.1", "karma-phantomjs-launcher": "^0.1.4", - "lodash": "^2.4.1" + "lodash": "^2.4.1", + "through": "^2.3.6" } } diff --git a/src/js/main.js b/src/js/main.js index cae76d1..8c08e82 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -1,5 +1,3 @@ -import test from './test'; +import parser from './parser.peg'; -test('foo'); - -console.log('testing'); +console.log('testing:', parser.parse('[0,1,2,3]')); diff --git a/src/js/parser.peg b/src/js/parser.peg new file mode 100644 index 0000000..2f31a84 --- /dev/null +++ b/src/js/parser.peg @@ -0,0 +1,4 @@ +grammar Lists + value <- list / number + list <- "[" ( value ("," value)* )? "]" + number <- [0-9] diff --git a/src/js/test.js b/src/js/test.js deleted file mode 100644 index 1a96ce9..0000000 --- a/src/js/test.js +++ /dev/null @@ -1,3 +0,0 @@ -export default m => { - console.log(m); -};