Skip to content

Commit

Permalink
Source maps support
Browse files Browse the repository at this point in the history
  • Loading branch information
tadatuta committed Jan 16, 2016
1 parent 2c96253 commit b949d9d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
12 changes: 11 additions & 1 deletion bin/bem-xjst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ require('coa').Cmd()
.short('e').long('engine')
.def('bemhtml')
.end()
.opt()
.name('sourceMap').title('Generate source map (default: false)')
.short('s').long('source-map')
.def(false)
.flag()
.end()

.act(function(options) {
var input = [],
deferred = q.defer();
Expand All @@ -51,7 +58,10 @@ require('coa').Cmd()

function finish(source) {
var out = bem_xjst[options.engine].generate(source, {
'no-opt': options['no-opt']
'no-opt': options['no-opt'],
from: options.input.path,
to: options.output.path,
sourceMap: options.sourceMap
});

options.output.write(out);
Expand Down
60 changes: 34 additions & 26 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var vm = require('vm');
var SourceMapFile = require('enb-source-map/lib/file');

function Compiler(runtime) {
this.runtime = runtime;
Expand All @@ -8,6 +9,7 @@ exports.Compiler = Compiler;

Compiler.prototype.generate = function generate(code, options) {
if (!options) options = {};
if (!options.to) options.to = process.cwd();

// It is fine to compile without templates at first
if (!code)
Expand All @@ -22,33 +24,39 @@ Compiler.prototype.generate = function generate(code, options) {

var locals = this.runtime.prototype.locals;

var source = [
'/// -------------------------------------',
'/// --------- BEM-XJST Runtime Start ----',
'/// -------------------------------------',
'var ' + exportName + ' = function(module, exports) {',
this.runtime.source + ';',
' return module.exports ||',
' exports.' + exportName + ';',
'}({}, {});',
'/// -------------------------------------',
'/// --------- BEM-XJST Runtime End ------',
'/// -------------------------------------',
'',
'var api = new ' + engine + '(' + JSON.stringify(options) + ');',
'/// -------------------------------------',
'/// ------ BEM-XJST User-code Start -----',
'/// -------------------------------------',
'api.compile(function(' + locals.join(', ') + ') {',
code + ';',
'});',
'api.exportApply(exports);',
'/// -------------------------------------',
'/// ------ BEM-XJST User-code End -------',
'/// -------------------------------------\n'
].join('\n');
var file = new SourceMapFile(options.to, { sourceMap: options.sourceMap });

return source;
file
.writeLine('/// -------------------------------------')
.writeLine('/// --------- BEM-XJST Runtime Start ----')
.writeLine('/// -------------------------------------')
.writeLine('var ' + exportName + ' = function(module, exports) {')
.writeContent(this.runtime.source).write(';')
.writeLine(' return module.exports ||')
.writeLine(' exports.' + exportName + ';')
.writeLine('}({}, {});')
.writeLine('/// -------------------------------------')
.writeLine('/// --------- BEM-XJST Runtime End ------')
.writeLine('/// -------------------------------------')
.writeLine('')
.writeLine('var api = new ' + engine + '(' + JSON.stringify(options) + ');')
.writeLine('/// -------------------------------------')
.writeLine('/// ------ BEM-XJST User-code Start -----')
.writeLine('/// -------------------------------------')
.writeLine('api.compile(function(' + locals.join(', ') + ') {')
.writeFileContent(options.from, code).write(';')
.writeLine('});')
.writeLine('api.exportApply(exports);')
.writeLine('/// -------------------------------------')
.writeLine('/// ------ BEM-XJST User-code End -------')
.writeLine('/// -------------------------------------');

return options.sourcemap && options.sourcemap.include === false ?
{
content: file.getContent(),
sourcemap: file.getSourceMap()
} :
file.render();
};

Compiler.prototype.compile = function compile(code, options) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"license": "MPL-2.0",
"dependencies": {
"coa": "~0.3.9",
"enb-source-map": "^1.9.0",
"inherits": "^2.0.1",
"minimalistic-assert": "^1.0.0",
"q": "~0.9.3"
Expand Down

0 comments on commit b949d9d

Please sign in to comment.