diff --git a/lib/techs/mock-lang-js.js b/lib/techs/mock-lang-js.js index fb33601..6bc91ce 100644 --- a/lib/techs/mock-lang-js.js +++ b/lib/techs/mock-lang-js.js @@ -8,19 +8,30 @@ module.exports = require('enb/lib/build-flow').create() return vfs.read(source, 'utf8') .then(function (content) { var mock = [ - '(function(global, bem_) {', - ' if(bem_.I18N) return;', - ' global.BEM = bem_;', - ' var i18n = bem_.I18N = function(keyset, key) {', - ' return key;', - ' };', - ' i18n.keyset = function() { return i18n }', - ' i18n.key = function(key) { return key }', - ' i18n.lang = function() { return }', - '})(this, typeof BEM === \'undefined\' ? {} : BEM);' - ].join('\n'); + '(function(global, bem_) {', + ' if(bem_.I18N) return;', + ' global.BEM = bem_;', + ' var i18n = bem_.I18N = function(keyset, key) {', + ' return key;', + ' };', + ' i18n.keyset = function() { return i18n }', + ' i18n.key = function(key) { return key }', + ' i18n.lang = function() { return }', + '})(this, typeof BEM === \'undefined\' ? {} : BEM);' + ].join('\n'), + mapIndex = content.lastIndexOf('//# sourceMappingURL='), + map; - return [mock, content].join('\n'); + // if there is a #sourceMappingURL pragma append + // the mock before it so the source map will be + // valid. We can't insert it in the beginning because + // source map locations will point to the wrong lines. + if (mapIndex !== -1) { + map = content.substring(mapIndex); + content = content.substring(0, mapIndex); + } + + return [content, mock, map].join('\n'); }); }) .createTech();