Made stream code optional and avoid dynamic requires

This commit is contained in:
Patrick Steele-Idem 2016-09-20 12:19:52 -06:00
parent 96778ad240
commit c45ffa088e
9 changed files with 102 additions and 66 deletions

View File

@ -0,0 +1,8 @@
{
"requireRemap": [
{
"from": "./index.js",
"to": "./index-browser-dynamic.js"
}
]
}

View File

@ -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);
};

View File

@ -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);
};

View File

@ -158,4 +158,6 @@ module.exports = function load(templatePath, templateSrc, options) {
}
};
module.exports.loadSource = loadSource;
module.exports.loadSource = loadSource;
require('../stream');

View File

@ -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);

54
runtime/stream.js Normal file
View File

@ -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);
};

View File

@ -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;

View File

@ -1 +0,0 @@
module.exports = require('stream');

12
stream.js Normal file
View File

@ -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');