diff --git a/runtime/loader/browser.json b/runtime/loader/browser.json new file mode 100644 index 000000000..fd212863e --- /dev/null +++ b/runtime/loader/browser.json @@ -0,0 +1,8 @@ +{ + "requireRemap": [ + { + "from": "./index.js", + "to": "./index-browser-dynamic.js" + } + ] +} \ No newline at end of file diff --git a/runtime/loader/index-browser-dynamic.js b/runtime/loader/index-browser-dynamic.js new file mode 100644 index 000000000..c9c1d86a7 --- /dev/null +++ b/runtime/loader/index-browser-dynamic.js @@ -0,0 +1,22 @@ +/* +* Copyright 2011 eBay Software Foundation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +module.exports = function load(templatePath) { + // We make the assumption that the template path is a + // fully resolved module path and that the module exists + // as a CommonJS module + return require(templatePath); +}; \ No newline at end of file diff --git a/runtime/loader/index-browser.js b/runtime/loader/index-browser.js index c9c1d86a7..cefd59f0a 100644 --- a/runtime/loader/index-browser.js +++ b/runtime/loader/index-browser.js @@ -15,8 +15,5 @@ */ module.exports = function load(templatePath) { - // We make the assumption that the template path is a - // fully resolved module path and that the module exists - // as a CommonJS module - return require(templatePath); + throw new Error('Template not found: ' + templatePath); }; \ No newline at end of file diff --git a/runtime/loader/index.js b/runtime/loader/index.js index 689255241..08259d3df 100644 --- a/runtime/loader/index.js +++ b/runtime/loader/index.js @@ -158,4 +158,6 @@ module.exports = function load(templatePath, templateSrc, options) { } }; -module.exports.loadSource = loadSource; \ No newline at end of file +module.exports.loadSource = loadSource; + +require('../stream'); \ No newline at end of file diff --git a/runtime/marko-runtime.js b/runtime/marko-runtime.js index 2de99248b..03f25dff0 100644 --- a/runtime/marko-runtime.js +++ b/runtime/marko-runtime.js @@ -48,17 +48,12 @@ var loader; // If the optional "stream" module is available // then Readable will be a readable stream -var Readable; var AsyncWriter = asyncWriter.AsyncWriter; var extend = require('raptor-util/extend'); - - exports.AsyncWriter = AsyncWriter; -var stream = require('./stream'); - function renderCallback(renderFunc, data, globalData, callback) { var out = new AsyncWriter(); if (globalData) { @@ -192,52 +187,12 @@ Template.prototype = { // created in the context of another rendering job. return shouldEnd ? finalOut.end() : finalOut; }, - stream: function(data) { - if (!stream) { - throw new Error('Module not found: stream'); - } - return new Readable(this, data, this._options); + stream: function() { + throw new Error('You must require("marko/stream")'); } }; -if (stream) { - Readable = function(template, data, options) { - Readable.$super.call(this); - this._t = template; - this._d = data; - this._options = options; - this._rendered = false; - }; - - Readable.prototype = { - write: function(data) { - if (data != null) { - this.push(data); - } - }, - end: function() { - this.push(null); - }, - _read: function() { - if (this._rendered) { - return; - } - - this._rendered = true; - - var template = this._t; - var data = this._d; - - var out = asyncWriter.create(this, this._options); - template.render(data, out); - out.end(); - } - }; - - require('raptor-util/inherit')(Readable, stream.Readable); -} - function createRenderProxy(template) { return function(data, out) { template._(data, out); diff --git a/runtime/stream.js b/runtime/stream.js new file mode 100644 index 000000000..4572ff5e5 --- /dev/null +++ b/runtime/stream.js @@ -0,0 +1,54 @@ +/* +This module is used to monkey patch `Template.prototype` to add a new `stream(templateData)` method. Since +this module is likely not needed in the browser, we have split out the code into a separate module. This module +is always loaded on the server, but if you need streaming in the browser you must add the following +line to your app: + + require('marko/stream'); + +*/ +var stream = require('stream'); +var asyncWriter = require('async-writer'); + +function Readable(template, data, options) { + Readable.$super.call(this); + this._t = template; + this._d = data; + this._options = options; + this._rendered = false; +} + +Readable.prototype = { + write: function(data) { + if (data != null) { + this.push(data); + } + }, + end: function() { + this.push(null); + }, + _read: function() { + if (this._rendered) { + return; + } + + this._rendered = true; + + var template = this._t; + var data = this._d; + + var out = asyncWriter.create(this, this._options); + template.render(data, out); + out.end(); + } +}; + +require('raptor-util/inherit')(Readable, stream.Readable); + +require('./').Template.prototype.stream = function(data) { + if (!stream) { + throw new Error('Module not found: stream'); + } + + return new Readable(this, data, this._options); +}; \ No newline at end of file diff --git a/runtime/stream/index-browser.js b/runtime/stream/index-browser.js deleted file mode 100644 index c22e541cc..000000000 --- a/runtime/stream/index-browser.js +++ /dev/null @@ -1,13 +0,0 @@ -var stream; -var STREAM = 'stream'; - -var streamPath; -try { - streamPath = require.resolve(STREAM); -} catch(e) {} - -if (streamPath) { - stream = require(streamPath); -} - -module.exports = stream; \ No newline at end of file diff --git a/runtime/stream/index.js b/runtime/stream/index.js deleted file mode 100644 index 114e10839..000000000 --- a/runtime/stream/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('stream'); \ No newline at end of file diff --git a/stream.js b/stream.js new file mode 100644 index 000000000..32778c165 --- /dev/null +++ b/stream.js @@ -0,0 +1,12 @@ +/* +This module is used to monkey patch `Template.prototype` to add a new `stream(templateData)` method. Since +streaming module is likely not needed in the browser, we have made this method optional. + +Template.prototype.stream is always available on the server, but if you need streaming in the browser you must add +the following line to your app: + + require('marko/stream'); + +*/ + +require('./runtime/stream'); \ No newline at end of file