From 2e00705b6401ce532f530ca0b0d5d92d0451bfcf Mon Sep 17 00:00:00 2001 From: javi Date: Thu, 4 Dec 2014 16:29:20 +0100 Subject: [PATCH] adds support for browserify this pr also changes some code to make compatible with the browser. It exposes the global variable "carto" One of the problems is mapnik reference, the module is not read to work with browserify since it loads modules dynamically. It does not make sense to make it compatible since mapnik-reference is not going to be used so people must to set the reference needed before parse/render: carto.tree.Reference.setData(reference) --- Makefile | 11 +++++++++++ lib/carto/index.js | 4 +++- lib/carto/tree/reference.js | 2 +- package.json | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 60277de..682e3f5 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,11 @@ # expresso = ./node_modules/.bin/mocha +BROWSERIFY = ./node_modules/.bin/browserify +TREE_FILES = $(shell ls ./lib/carto/tree/*.js) + +# generates the requirement options for browserify for modules loaded dinamically (see, lib/carto/index.js) +BROWSERIFY_REQUIRES = $(foreach var,$(TREE_FILES), -r $(var):$(subst .js,,$(subst ./lib/carto,.,$(var)))) lint: ./node_modules/.bin/jshint lib/carto/*.js lib/carto/tree/*.js @@ -17,4 +22,10 @@ endif check: test +dist: + mkdir -p dist + +dist/carto.uncompressed.js: dist $(shell $(BROWSERIFY) --list lib/carto/index.js) $(TREE_FILES) + $(BROWSERIFY) --require $(BROWSERIFY_REQUIRES) --debug lib/carto/index.js --standalone carto > $@ + .PHONY: test diff --git a/lib/carto/index.js b/lib/carto/index.js index a0265d8..3b96899 100644 --- a/lib/carto/index.js +++ b/lib/carto/index.js @@ -4,7 +4,9 @@ var util = require('util'), function getVersion() { - if (parseInt(process.version.split('.')[1], 10) > 4) { + if (typeof(this['window']) !== undefined) { + return require('../../package.json').version.split('.'); + } else if (parseInt(process.version.split('.')[1], 10) > 4) { return require('../../package.json').version.split('.'); } else { // older node diff --git a/lib/carto/tree/reference.js b/lib/carto/tree/reference.js index de0945a..64fe76f 100644 --- a/lib/carto/tree/reference.js +++ b/lib/carto/tree/reference.js @@ -5,7 +5,6 @@ (function(tree) { var _ = require('underscore'), - mapnik_reference = require('mapnik-reference'), ref = {}; ref.setData = function(data) { @@ -24,6 +23,7 @@ ref.setData = function(data) { }; ref.setVersion = function(version) { + var mapnik_reference = require('mapnik-reference'); if (mapnik_reference.version.hasOwnProperty(version)) { ref.setData(mapnik_reference.version[version]); return true; diff --git a/package.json b/package.json index 94fdc21..6c061a6 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "jshint": "0.2.x", "sax": "0.1.x", "istanbul": "~0.2.14", - "coveralls": "~2.10.1" + "coveralls": "~2.10.1", + "browserify": "~7.0.0" }, "scripts": { "pretest": "npm install",