-
Notifications
You must be signed in to change notification settings - Fork 844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to ignore all unneeded whitespace #51
Comments
@TimothyGu said
I suggest EJS provide an intermediate function to help parsing EJS output. For example, app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(ejs({intermediate: function(input) {
return minify(input); //Implement your own minify function here and ejs will cached this version.
}}) |
You could do something like this (untested): var ejs = require('ejs')
app.engine('ejs', function (filePath, options, callback) {
ejs.__express(filePath, options, function (err, html) {
if (err) return callback(err)
callback(minify(html))
}
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs') |
@TimothyGu app.engine('ejs', function (filePath, options, callback) {
ejs.__express(filePath, options, function (err, html) {
if (err) return callback(err);
console.log(html);
callback(html);
});
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); |
@Fonger said:
This is expected. In the Express.js world, |
I find the following code working! Furthermore, var ejs = require('ejs');
var parse = ejs.parse;
ejs.parse = function(html_ejs, options) {
html_ejs = html_ejs.replace(/^\s+|\s+$|\n/gm,"");
console.log('parse!!!!!!'); // to check if it only runs once.
return parse.apply(this, [html_ejs, options]);
}; I don't know why the author of And this is the forked one. |
OK this works: var ejs = require('ejs')
app.engine('ejs', function (filePath, options, callback) {
ejs.__express(filePath, options, function (err, html) {
if (err) return callback(err)
callback(null, minify(html))
})
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs') Forgot about the first argument to the callback. EDIT: add missing |
@TimothyGu You still miss one |
This mode uses a regex to drop 1. line feeds 2. carriage returns 3. leading whitespaces of lines 4. trailing whitespaces It is not a full-fledged HTML minifier, nor will it be. Fixes #51.
@Fonger said:
Oops, post updated.
Yes that is intended. I have committed 6c12c2e to the drop-whitespace branch of EJS, which incorporates some changes from ejs-shrink, but adapted to EJS v2, so feel free to try it out. ejs-shrink only works on EJS v1 as we don't have a |
@TimothyGu said:
Oops! I didn't find that I was still using old EJS v1. |
@Fonger said:
See https://github.com/mde/ejs/blob/master/CHANGELOG.md#v201-2015-01-02 EJS v2 is a complete rewrite of v1, using OOP and regex rather than one big One negative thing about EJS v2 is that it's much slower than EJS v1, both in terms of compilation and execution. I've already made EJS v2 4 times faster than prior to my involvement in the project, and #50 should give it an additional 30-80% boost, although it is still slower than EJS v1. Note that I have a fork of EJS that uses EJS v1 code base but ported all the v2 features over, but all API changes and features are discussed here as I intend to give my fork 100% compatibility with EJS v2. If you get a chance please test |
@TimothyGu Thank you. res.render('welcome',
{ myVariable: 123 }, {removeWhitespace:true}); The above code doesn't work. It says |
The easiest way to test EJS is simply cloning it and test locally: $ git clone https://github.com/mde/ejs.git
$ git checkout drop-whitespace
$ cd ejs
$ npm install
$ edit 'test.js' // test.js
var ejs = require('./')
, str = '<%= myVariable %>\t\r\n adfs \n\t asdf'
console.log(ejs.render(str, { myVariable: 123 }, { removeWhitespace: true })) But if you really want to test with Express.js, put locals and options in to a single object, like this: res.render('welcome', {
myVariable: 123
, removeWhitespace: true
}) Note that this behavior might change in Express.js version 5. |
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
It is not a full-fledged HTML minifier, nor will it be. It is designed to be as "safe" as possible. Fixes #51.
FYI: I found a way to define this in ExpressJS app i.e. app.set('view engine', 'ejs');
app.locals.rmWhitespace = true; |
nice, thanks |
Benchmarking shows that sometimes too many
will have a very negative effect on performance. It is also one of the major reasons why doT is much faster than EJS, even though theoretically EJS is simpler.
New line slurping does get rid of the new line, but then you still have
The text was updated successfully, but these errors were encountered: