From c9807b1a498a3d58b6af9683a08dce6688056907 Mon Sep 17 00:00:00 2001 From: Bermi Ferrer Date: Wed, 14 Aug 2013 15:08:12 +0200 Subject: [PATCH] Adding support for CommonJS browser environments. This commit replaces lib/server.js with lib/browser.js when the code is being bundled as CommonJS for browser environments using tools that look at the "browser" option on the package.json file as outlined on https://gist.github.com/shtylman/4339901. The change allows to require the "dustjs-linkedin" on browser environments without having to make use of the "vm" module as it currently occurs when the lib/server.js is being bundled. I've tested this change by running: browserify --standalone dust . > ./dist/browserified-dust.js and updating the test/test.html scripts to include the browserified version instead of lib/[dust,parser,compiler].js. A sample browserified version can be found on https://gist.github.com/bermi/03ee5117c0dfca74322b. It happens to be just 7KB larger than the combined version. --- lib/browser.js | 11 +++++++++++ package.json | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 lib/browser.js diff --git a/lib/browser.js b/lib/browser.js new file mode 100644 index 00000000..39ea1354 --- /dev/null +++ b/lib/browser.js @@ -0,0 +1,11 @@ +// Including compiler for CommonJS browser environments. +// this replaces server.js via the "browser" attribute on package.json +// for bundling environments like browserify +module.exports = function (dust) { + var parser = require('./parser'), + compiler = require('./compiler')(dust); + + compiler.parse = parser.parse; + dust.compile = compiler.compile; + dust.optimizers = compiler.optimizers; +}; diff --git a/package.json b/package.json index 2e8ecda2..688f84fe 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,9 @@ "uglify-js" : "1.3.3", "pegjs" : "0.7.0" }, + "browser": { + "./lib/server.js": "./lib/browser.js" + }, "license": "MIT", "engine": { "node": ">=0.5"