Skip to content

Commit

Permalink
Generate source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanbeevers committed Sep 9, 2015
1 parent 07a613b commit 87c6d10
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function(source) {
var loaderApi = this;
var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query);
var runtimePath = query.runtime || require.resolve("handlebars/runtime");
var srcName = path.basename(this.resourcePath);

if (!versionCheck(handlebars, require(runtimePath))) {
throw new Error('Handlebars compiler version does not match runtime version');
Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = function(source) {
}

var debug = query.debug;
var useSrcMap = this.sourceMap && parseInt(handlebars.VERSION.split('.')[0], 10) >= 3;

var hb = handlebars.create();
var JavaScriptCompiler = hb.JavaScriptCompiler;
Expand Down Expand Up @@ -135,13 +137,27 @@ module.exports = function(source) {

// Precompile template
var template = '';
var srcMap;

try {
if (source) {
template = hb.precompile(source, {
var precompileOptions = {
knownHelpersOnly: firstCompile ? false : true,
knownHelpers: knownHelpers
});
knownHelpers: knownHelpers,
};

if (useSrcMap) {
precompileOptions.srcName = srcName;
}

var result = hb.precompile(source, precompileOptions);

if (useSrcMap) {
template = result.code;
srcMap = JSON.parse(result.map);
} else {
template = result;
}
}
} catch (err) {
return loaderAsyncCallback(err);
Expand Down Expand Up @@ -250,12 +266,12 @@ module.exports = function(source) {
}

// export as module if template is not blank
var slug = template ?
var output = template ?
'var Handlebars = require(' + JSON.stringify(runtimePath) + ');\n'
+ 'module.exports = (Handlebars["default"] || Handlebars).template(' + template + ');' :
'module.exports = function(){return "";};';

loaderAsyncCallback(null, slug);
loaderAsyncCallback(null, output, srcMap);
};

var resolvePartials = function(err) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dependencies": {
"async": "~0.2.10",
"fastparse": "^1.0.0",
"loader-utils": "0.2.x"
"loader-utils": "0.2.x",
"inline-source-map-comment": "^1.0.5"
},
"peerDependencies": {
"handlebars": ">= 1.3.0 < 5"
Expand Down
2 changes: 2 additions & 0 deletions test/lib/WebpackLoaderMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ function WebpackLoaderMock (options) {
this.query = options.query;
this._asyncCallback = options.async;
this._resolveStubs = options.resolveStubs || {};
this.sourceMap = options.sourceMap;
this.resourcePath = options.resourcePath;
}

WebpackLoaderMock.prototype.resolve = function (context, resource, callback) {
Expand Down
22 changes: 18 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var assert = require('assert'),
object: {a: 'a', b: 'b', c: 'c'}
};

function applyTemplate(source, options) {
function applyTemplate(source, map, options) {
var requires = options && options.requireStubs || {},
_require = sinon.spy(function (resource) {
return requires[resource] || require(resource);
Expand All @@ -23,7 +23,7 @@ function applyTemplate(source, options) {

(new Function('module', 'require', source))(_module, _require);

options.test(null, _module.exports(options.data), _require);
options.test(null, _module.exports(options.data), _require, map);
}

function loadTemplate(templatePath) {
Expand All @@ -39,16 +39,18 @@ function testTemplate(loader, template, options, testFn) {

loader.call(new WebpackLoaderMock({
query: options.query,
sourceMap: options.sourceMap,
resolveStubs: resolveStubs,
async: function (err, source) {
resourcePath: template,
async: function (err, source, map) {
if (err) {
// Proxy errors from loader to test function
return testFn(err);
} else if (!source) {
return testFn(new Error('Could not generate template'));
}

applyTemplate(source, {
applyTemplate(source, map, {
data: options.data,
requireStubs: options.stubs,
test: testFn
Expand Down Expand Up @@ -376,4 +378,16 @@ describe('handlebars-loader', function () {
});
});

it('should generate source map', function (done) {
testTemplate(loader, './simple.handlebars', {
sourceMap: true,
data: TEST_TEMPLATE_DATA
}, function (err, output, require, map) {
assert.ok(map);
assert.equal(map.sources.length, 1);
assert.equal(map.sources[0], 'simple.handlebars');
done();
});
});

});

0 comments on commit 87c6d10

Please sign in to comment.