From 7f07611cc8111e05620fb6372eed0e85b7b6e3e4 Mon Sep 17 00:00:00 2001 From: Vladimir Grinenko Date: Wed, 23 Dec 2015 14:43:09 +0300 Subject: [PATCH] Source maps support --- lib/compiler.js | 58 +++++++++++++++++++++++++++---------------------- package.json | 1 + 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index e13262b2..fba6f15e 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -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; @@ -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) @@ -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) { diff --git a/package.json b/package.json index b0d18586..2208c389 100644 --- a/package.json +++ b/package.json @@ -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"