Skip to content

Commit dcd43c1

Browse files
authored
Merge pull request #13 from cho45/sourcemaps2
Add sourceMappingURL
2 parents f07f147 + 44f941a commit dcd43c1

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/micro-template.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ const template = function (id, data) {
2323
)
2424
).join('') +
2525
`/*end*/ return __this.ret;` +
26-
`} catch (e) { throw new Error('TemplateError: ' + e + ' (on ${name} line ' + __this.line + ')'); }` +
27-
`//@ sourceURL=${name}\n`
26+
`} catch (e) { throw new Error('TemplateError: '+e+' (on ${name} line ' + __this.line + ')',{cause:e}); }` +
27+
`//# sourceURL=${name}\n` +
28+
`//# sourceMappingURL=data:application/json,${encodeURIComponent(JSON.stringify({version:3, file:name, sources:[`${name}.ejs`], sourcesContent:[string], mappings:";;AAAA;"+Array(line-1).fill('AACA').join(';')}))}`
2829
);
2930
const func = new Function("__this", ...keys, body);
3031
return function (stash) { return func.call(null, me.context = { escapeHTML: me.escapeHTML, line: 1, ret : '', stash: stash }, ...keys.map(key => stash[key])) };

test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import fs from 'fs';
99
// template.get の上書き
1010
template.get = function (id) { return fs.readFileSync('test/data-' + id + '.tmpl', 'utf-8') };
1111

12+
function templateWithCompiledFunction(stringTemplate, data) {
13+
data = Object.assign({}, data);
14+
data.__context = {};
15+
stringTemplate += `\n<% __context.compiled = arguments.callee; %>`;
16+
const result = template(stringTemplate, data);
17+
return [result, data.__context.compiled];
18+
}
19+
1220
// --- template 基本テスト ---
1321
test('template renders with data', (t) => {
1422
const result = template('<b><%= foo %></b><i><%= bar %></i>', { foo: 'foo', bar: 'bar' });
@@ -252,6 +260,18 @@ test('reference not found throws', (t) => {
252260
);
253261
});
254262

263+
test('TemplateError has correct cause', (t) => {
264+
let thrownError;
265+
assert.throws(() => {
266+
template('<%= notfound %>', {});
267+
}, e => {
268+
thrownError = e;
269+
return e instanceof Error && /TemplateError: ReferenceError: notfound is not defined/.test(e.message);
270+
});
271+
assert(thrownError.cause instanceof ReferenceError);
272+
assert.strictEqual(thrownError.cause.message, 'notfound is not defined');
273+
});
274+
255275
test('template with explicit throw', (t) => {
256276
assert.throws(() => template('<% throw new Error("fail") %>', {}), e => e instanceof Error && /TemplateError: Error: fail/.test(e.message));
257277
});
@@ -304,3 +324,18 @@ test('template with properties script tags', (t) => {
304324
});
305325
assert.strictEqual(result, 'foobar');
306326
});
327+
328+
test('template output includes sourceMappingURL comment', (t) => {
329+
const [_, compiledFunction] = templateWithCompiledFunction('foo bar', {});
330+
const compiledFunctionString = compiledFunction.toString();
331+
console.log(compiledFunctionString);
332+
const match = compiledFunctionString.match(/\n\/\/\# sourceMappingURL=data:application\/json,(.+)\n/);
333+
assert(match, 'sourceMappingURL comment is present and correctly formatted');
334+
const json = decodeURIComponent(match[1]);
335+
let sourceMap;
336+
assert.doesNotThrow(() => sourceMap = JSON.parse(json), 'sourceMappingURL JSON is valid');
337+
console.log(sourceMap);
338+
assert.strictEqual(sourceMap.version, 3, 'source map version is 3');
339+
assert.ok(sourceMap.sourcesContent, 'source map has sourcesContent');
340+
assert.ok(sourceMap.mappings, 'source map has mappings');
341+
});

0 commit comments

Comments
 (0)