mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Made stream code optional and avoid dynamic requires
This commit is contained in:
parent
96778ad240
commit
c45ffa088e
8
runtime/loader/browser.json
Normal file
8
runtime/loader/browser.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"requireRemap": [
|
||||||
|
{
|
||||||
|
"from": "./index.js",
|
||||||
|
"to": "./index-browser-dynamic.js"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
22
runtime/loader/index-browser-dynamic.js
Normal file
22
runtime/loader/index-browser-dynamic.js
Normal 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);
|
||||||
|
};
|
||||||
@ -15,8 +15,5 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function load(templatePath) {
|
module.exports = function load(templatePath) {
|
||||||
// We make the assumption that the template path is a
|
throw new Error('Template not found: ' + templatePath);
|
||||||
// fully resolved module path and that the module exists
|
|
||||||
// as a CommonJS module
|
|
||||||
return require(templatePath);
|
|
||||||
};
|
};
|
||||||
@ -158,4 +158,6 @@ module.exports = function load(templatePath, templateSrc, options) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.loadSource = loadSource;
|
module.exports.loadSource = loadSource;
|
||||||
|
|
||||||
|
require('../stream');
|
||||||
@ -48,17 +48,12 @@ var loader;
|
|||||||
|
|
||||||
// If the optional "stream" module is available
|
// If the optional "stream" module is available
|
||||||
// then Readable will be a readable stream
|
// then Readable will be a readable stream
|
||||||
var Readable;
|
|
||||||
|
|
||||||
var AsyncWriter = asyncWriter.AsyncWriter;
|
var AsyncWriter = asyncWriter.AsyncWriter;
|
||||||
var extend = require('raptor-util/extend');
|
var extend = require('raptor-util/extend');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
exports.AsyncWriter = AsyncWriter;
|
exports.AsyncWriter = AsyncWriter;
|
||||||
|
|
||||||
var stream = require('./stream');
|
|
||||||
|
|
||||||
function renderCallback(renderFunc, data, globalData, callback) {
|
function renderCallback(renderFunc, data, globalData, callback) {
|
||||||
var out = new AsyncWriter();
|
var out = new AsyncWriter();
|
||||||
if (globalData) {
|
if (globalData) {
|
||||||
@ -192,52 +187,12 @@ Template.prototype = {
|
|||||||
// created in the context of another rendering job.
|
// created in the context of another rendering job.
|
||||||
return shouldEnd ? finalOut.end() : finalOut;
|
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) {
|
function createRenderProxy(template) {
|
||||||
return function(data, out) {
|
return function(data, out) {
|
||||||
template._(data, out);
|
template._(data, out);
|
||||||
|
|||||||
54
runtime/stream.js
Normal file
54
runtime/stream.js
Normal 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);
|
||||||
|
};
|
||||||
@ -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;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
module.exports = require('stream');
|
|
||||||
12
stream.js
Normal file
12
stream.js
Normal 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');
|
||||||
Loading…
x
Reference in New Issue
Block a user