Skip to content

Commit

Permalink
Source maps support
Browse files Browse the repository at this point in the history
  • Loading branch information
tadatuta committed Dec 23, 2015
1 parent 2c96253 commit 7f07611
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
58 changes: 32 additions & 26 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var path = require('path');
var vm = require('vm');
var SourceMap = require('enb-source-map/lib/file');

function Compiler(runtime) {
this.runtime = runtime;
Expand All @@ -8,6 +10,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 +25,36 @@ 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 SourceMap('bundle.js', { sourceMap: options.sourceMap });
var pathToBundle = path.join(__dirname, engine.toLowerCase(), 'bundle.js');
var relPathToBundle = path.relative(options.to, pathToBundle);

return source;
file
.writeLine('/// -------------------------------------')
.writeLine('/// --------- BEM-XJST Runtime Start ----')
.writeLine('/// -------------------------------------')
.writeLine('var ' + exportName + ' = function(module, exports) {')
.writeFileContent(relPathToBundle, 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('templates', code).write(';')
.writeLine('});')
.writeLine('api.exportApply(exports);')
.writeLine('/// -------------------------------------')
.writeLine('/// ------ BEM-XJST User-code End -------')
.writeLine('/// -------------------------------------');

return 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 7f07611

Please sign in to comment.