Fixes #487 - Add marko compiler type and version to compiled output.

This commit is contained in:
austinkelleher 2017-02-07 10:46:47 -05:00
parent ffdc91382f
commit 645d184da0
30 changed files with 210 additions and 42 deletions

View File

@ -121,7 +121,9 @@ if (args.vdom) {
}
var compileOptions = {
output: output
output: output,
compilerType: 'markoc',
compilerVersion: markoPkgVersion || markocPkgVersion
};
var force = args.force;
@ -377,4 +379,4 @@ if (args.clean) {
});
}
}
}

View File

@ -9,6 +9,7 @@ var FunctionDeclaration = require('./ast/FunctionDeclaration');
var FunctionCall = require('./ast/FunctionCall');
var Literal = require('./ast/Literal');
var Identifier = require('./ast/Identifier');
var Comment = require('./ast/Comment');
var If = require('./ast/If');
var ElseIf = require('./ast/ElseIf');
var Else = require('./ast/Else');
@ -273,6 +274,10 @@ class Builder {
return new HtmlComment({comment});
}
comment(comment) {
return new Comment({comment});
}
htmlElement(tagName, attributes, body, argument, openTagOnly, selfClosed) {
if (typeof tagName === 'object' && !(tagName instanceof Node)) {
let def = arguments[0];

View File

@ -6,6 +6,7 @@ const Literal = require('./ast/Literal');
const Identifier = require('./ast/Identifier');
const ok = require('assert').ok;
const Container = require('./ast/Container');
const Comment = require('./ast/Comment');
const isValidJavaScriptVarName = require('./util/isValidJavaScriptVarName');
class CodeWriter {
@ -84,7 +85,7 @@ class CodeWriter {
// Do nothing
} else if (this._code.endsWith(';')) {
this._code += '\n';
} else if (this._code.endsWith('\n' + this.currentIndent)) {
} else if (this._code.endsWith('\n' + this.currentIndent) || node instanceof Comment) {
// Do nothing
} else {
this._code += ';\n';
@ -260,4 +261,4 @@ class CodeWriter {
}
}
module.exports = CodeWriter;
module.exports = CodeWriter;

View File

@ -17,6 +17,8 @@ var Walker = require('./Walker');
var EventEmitter = require('events').EventEmitter;
var utilFingerprint = require('./util/fingerprint');
const markoPkgVersion = require('../package.json').version;
const FLAG_PRESERVE_WHITESPACE = 'PRESERVE_WHITESPACE';
const deresolveOptions = {
@ -102,7 +104,12 @@ class CompileContext extends EventEmitter {
this.options = options || {};
const writeVersionComment = this.options.writeVersionComment;
this.outputType = this.options.output || 'html';
this.compilerType = this.options.compilerType || 'marko';
this.compilerVersion = this.options.compilerVersion || markoPkgVersion;
this.writeVersionComment = writeVersionComment !== 'undefined' ? writeVersionComment : true;
this._vars = {};
this._uniqueVars = new UniqueVars();
@ -737,4 +744,4 @@ CompileContext.prototype.util = {
isJavaScriptReservedWord: require('./util/isJavaScriptReservedWord')
};
module.exports = CompileContext;
module.exports = CompileContext;

36
compiler/ast/Comment.js Normal file
View File

@ -0,0 +1,36 @@
'use strict';
const Node = require('./Node');
function _isMultilineComment(comment) {
return comment && comment.indexOf('\n') !== -1;
}
class Comment extends Node {
constructor(def) {
super('Comment');
const comment = def.comment;
if (_isMultilineComment(comment)) {
this.comment = `/*\n${comment}\n*/`;
} else {
this.comment = `// ${comment}`;
}
}
generateCode(codegen) {
return this;
}
writeCode(writer) {
var name = this.comment;
writer.write(name);
}
toString() {
return this.comment;
}
}
module.exports = Comment;

View File

@ -11,6 +11,12 @@ function createVarsArray(vars) {
});
}
function _buildVersionComment(builder, context) {
const version = context.compilerVersion;
const compilerType = context.compilerType;
return builder.comment(`Compiled using ${compilerType}@${version} - DO NOT EDIT`);
}
class TemplateRoot extends Node {
constructor(def) {
super('TemplateRoot');
@ -86,6 +92,10 @@ class TemplateRoot extends Node {
let body = [];
if (context.writeVersionComment) {
body.push(_buildVersionComment(builder, context));
}
let staticNodes = context.getStaticNodes([templateDeclaration]);
if (staticNodes.length) {
body = body.concat(staticNodes);
@ -155,4 +165,4 @@ class TemplateRoot extends Node {
}
}
module.exports = TemplateRoot;
module.exports = TemplateRoot;

View File

@ -37,8 +37,14 @@ if (g.__MARKO_CONFIG) {
preserveWhitespace: false,
// The default output mode for compiled templates
output: 'html'
output: 'html',
/**
* Whether the version should be written to the template as a comment e.g.
* // Compiled using marko@4.0.0 - DO NOT EDIT
*/
writeVersionComment: true
};
}
module.exports = config;
module.exports = config;

View File

@ -238,4 +238,4 @@ exports.registerTaglib = function(path) {
clearCaches();
};
exports.isVDOMSupported = true;
exports.isVDOMSupported = true;

View File

@ -1,15 +1,14 @@
var fs = require('fs');
var path = require('path');
exports.check = function(marko, markoCompiler, expect, helpers, done) {
var compiler = require('marko/compiler');
var templatePath = path.join(__dirname, 'template.marko');
compiler.compileFileForBrowser(templatePath, {}, function(err, compiledTemplate) {
compiler.compileFileForBrowser(templatePath, {
writeVersionComment: false
}, function(err, compiledTemplate) {
helpers.compare(compiledTemplate.code, '.js');
done();
});
};
};

View File

@ -5,9 +5,11 @@ exports.check = function(marko, markoCompiler, expect, helpers, done) {
var compiler = require('marko/compiler');
var templatePath = path.join(__dirname, 'template.marko');
var compiledTemplate = compiler.compileFileForBrowser(templatePath, {});
var compiledTemplate = compiler.compileFileForBrowser(templatePath, {
writeVersionComment: false
});
helpers.compare(compiledTemplate.code, '.js');
done();
};
};

View File

@ -7,11 +7,11 @@ exports.check = function(marko, markoCompiler, expect, helpers, done) {
var templateSrc = fs.readFileSync(templatePath, { encoding: 'utf8' });
compiler.compileForBrowser(templateSrc, templatePath, {}, function(err, compiledTemplate) {
compiler.compileForBrowser(templateSrc, templatePath, {
writeVersionComment: false
}, function(err, compiledTemplate) {
helpers.compare(compiledTemplate.code, '.js');
done();
});
};
};

View File

@ -0,0 +1,13 @@
var marko_template = module.exports = require("marko/vdom").t();
function render(input, out) {
var data = input;
out.t("Hello ");
out.t(data.name);
out.t("!");
}
marko_template._ = render;

View File

@ -0,0 +1 @@
-- Hello ${data.name}!

View File

@ -0,0 +1,24 @@
var fs = require('fs');
var path = require('path');
var markoVersion = require('../../../../package.json').version;
function _appendMarkoVersionComment(str) {
return '// Compiled using marko@' + markoVersion + ' - DO NOT EDIT\n' + str;
}
exports.check = function(marko, markoCompiler, expect, helpers, done) {
var compiler = require('marko/compiler');
var templatePath = path.join(__dirname, 'template.marko');
var expectedPath = path.join(__dirname, 'expected.js');
var templateSrc = fs.readFileSync(templatePath, { encoding: 'utf8' });
var compiledTemplate = compiler.compileForBrowser(templateSrc, templatePath);
var expected = fs.readFileSync(expectedPath, { encoding: 'utf8' });
compiledTemplate.code = _appendMarkoVersionComment(compiledTemplate.code);
expected = _appendMarkoVersionComment(expected);
expect(compiledTemplate.code).to.deep.equal(expected);
done();
};

View File

@ -6,9 +6,11 @@ exports.check = function(marko, markoCompiler, expect, helpers, done) {
var templatePath = path.join(__dirname, 'template.marko');
var templateSrc = fs.readFileSync(templatePath, { encoding: 'utf8' });
var compiledTemplate = compiler.compileForBrowser(templateSrc, templatePath, {});
var compiledTemplate = compiler.compileForBrowser(templateSrc, templatePath, {
writeVersionComment: false
});
helpers.compare(compiledTemplate.code, '.js');
done();
};
};

View File

@ -1,8 +1,9 @@
exports.check = function(marko, markoCompiler, expect, helpers, done) {
exports.check = function(marko, markoCompiler, expect, helpers, done) {
var compiler = require('marko/compiler');
compiler.configure(); // Use defaults
expect(compiler.config.writeToDisk).to.equal(true);
expect(compiler.config.preserveWhitespace).to.equal(false);
expect(compiler.config.writeVersionComment).to.equal(true);
compiler.configure({
preserveWhitespace: true
@ -14,4 +15,4 @@ exports.check = function(marko, markoCompiler, expect, helpers, done) {
expect(compiler.config.writeToDisk).to.equal(true);
expect(compiler.config.preserveWhitespace).to.equal(false);
done();
};
};

View File

@ -0,0 +1,4 @@
/*
This is a comment
on multiple lines
*/

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function(builder) {
return builder.comment('This is a comment\non multiple lines');
};

View File

@ -0,0 +1 @@
// This is a comment

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function(builder) {
return builder.comment('This is a comment');
};

View File

@ -0,0 +1,13 @@
var marko_template = module.exports = require("marko/html").t(__filename),
marko_helpers = require("marko/runtime/html/helpers"),
marko_escapeXml = marko_helpers.x;
function render(input, out) {
var data = input;
out.w("Hello " +
marko_escapeXml(data.name) +
"!");
}
marko_template._ = render;

View File

@ -0,0 +1 @@
-- Hello ${data.name}!

View File

@ -0,0 +1,15 @@
var expect = require('chai').expect;
var markoVersion = require('../../../../package.json').version;
function _appendMarkoVersionComment(str) {
return '// Compiled using markoc@' + markoVersion + ' - DO NOT EDIT\n' + str;
}
exports.test = function(helpers) {
helpers.spawnSync(['template.marko']);
var compiledFile = helpers.readSync('template.marko.js').toString();
var expectedFile = _appendMarkoVersionComment(helpers.readSync('expected.js'));
expect(compiledFile).to.deep.equal(expectedFile);
};

View File

@ -24,11 +24,13 @@ describe('compiler (browser target)', function() {
main = require(mainPath);
}
var compilerOptions = { writeVersionComment: false };
if (main && main.checkError) {
var e;
try {
compiler.compileFileForBrowser(templatePath);
compiler.compileFileForBrowser(templatePath, compilerOptions);
} catch(_e) {
e = _e;
}
@ -41,7 +43,7 @@ describe('compiler (browser target)', function() {
done();
} else {
var compiledTemplate = compiler.compileFileForBrowser(templatePath, main && main.compilerOptions);
var compiledTemplate = compiler.compileFileForBrowser(templatePath, Object.assign(compilerOptions, main && main.compilerOptions));
if(main && main.checkTemplate) {
main.checkTemplate(compiledTemplate);
@ -56,4 +58,4 @@ describe('compiler (browser target)', function() {
}
});
});
});

View File

@ -22,11 +22,13 @@ describe('compiler', function() {
main = require(mainPath);
}
var compilerOptions = { writeVersionComment: false };
if (main && main.checkError) {
var e;
try {
compiler.compileFile(templatePath);
compiler.compileFile(templatePath, compilerOptions);
} catch(_e) {
e = _e;
}
@ -39,14 +41,14 @@ describe('compiler', function() {
done();
} else if(main && main.checkTemplate) {
var template = require('marko').load(templatePath, main.compilerOptions);
var template = require('marko').load(templatePath, Object.assign(compilerOptions, main.compilerOptions));
main.checkTemplate(template);
done();
} else {
var compiledSrc = compiler.compileFile(templatePath, main && main.compilerOptions);
var compiledSrc = compiler.compileFile(templatePath, Object.assign(compilerOptions, main && main.compilerOptions));
helpers.compare(compiledSrc, '.js');
done();
}
});
});
});

View File

@ -17,10 +17,11 @@ describe('inline', function() {
var indexPath = path.join(dir, 'index.js');
var inlineCompiler = compiler.createInlineCompiler(indexPath);
var compilerOptions = { writeVersionComment: false };
var src = fs.readFileSync(indexPath, { encoding: 'utf8' });
src = src.replace(/marko`([^`]*)`/g, function(match, templateSrc) {
var compiled = inlineCompiler.compile(templateSrc);
var compiled = inlineCompiler.compile(templateSrc, compilerOptions);
return compiled.code;
});
@ -57,4 +58,4 @@ describe('inline', function() {
});
});
});

View File

@ -16,10 +16,16 @@ describe('markoc' , function() {
var autoTestDir = nodePath.join(__dirname, 'autotests/markoc');
autotest.scanDir(autoTestDir, function run(dir, helpers, done) {
var testModule = require(nodePath.join(dir, 'test.js'));
const testModule = require(nodePath.join(dir, 'test.js'));
helpers.existsSync = function(filename) {
return fs.existsSync(nodePath.join(dir, filename));
};
helpers.readSync = function(filename) {
return fs.readFileSync(nodePath.join(dir, filename));
};
helpers.spawnSync = function(args, options) {
options = options || {};
if (!options.cwd) {

View File

@ -24,7 +24,7 @@ describe('compiler (vdom)', function() {
main = require(mainPath);
}
var compilerOptions = { output: 'vdom' };
var compilerOptions = { output: 'vdom', writeVersionComment: false };
if (main && main.checkError) {
var e;
@ -51,4 +51,4 @@ describe('compiler (vdom)', function() {
}
});
});
});

View File

@ -34,11 +34,13 @@ describe('marko-widgets (compilation, deprecated)', function() {
main = require(mainPath);
}
var compilerOptions = { writeVersionComment: false };
if (main && main.checkError) {
var e;
try {
compiler.compileFile(templatePath);
compiler.compileFile(templatePath, compilerOptions);
} catch(_e) {
e = _e;
}
@ -50,9 +52,9 @@ describe('marko-widgets (compilation, deprecated)', function() {
main.checkError(e);
done();
} else {
var actualSrc = compiler.compileFile(templatePath, main && main.compilerOptions);
var actualSrc = compiler.compileFile(templatePath, Object.assign(compilerOptions, main && main.compilerOptions));
actualSrc = stripVarData(actualSrc);
var actualPath = path.join(testPath, './actual.js');
var expectedPath = path.join(testPath, './expected.js');
fs.writeFileSync(actualPath, actualSrc);

View File

@ -35,11 +35,13 @@ describe('marko-widgets (compilation)', function() {
main = require(mainPath);
}
var compilerOptions = { writeVersionComment: false };
if (main && main.checkError) {
var e;
try {
compiler.compileFile(templatePath);
compiler.compileFile(templatePath, compilerOptions);
} catch(_e) {
e = _e;
}
@ -51,7 +53,7 @@ describe('marko-widgets (compilation)', function() {
main.checkError(e);
done();
} else {
var actualSrc = compiler.compileFile(templatePath, main && main.compilerOptions);
var actualSrc = compiler.compileFile(templatePath, Object.assign(compilerOptions, main && main.compilerOptions));
actualSrc = stripVarData(actualSrc);
var actualPath = path.join(testPath, './actual.js');
@ -63,4 +65,4 @@ describe('marko-widgets (compilation)', function() {
}
});
});
});
});