Skip to content

Commit e3cf902

Browse files
fix: add a default sourceURL to aid debugging
1 parent cc2842f commit e3cf902

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

js/src/VueTemplateRenderer.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function createComponentObject(model, parentView) {
2828
const isTemplateModel = model.get('template') instanceof TemplateModel;
2929
const templateModel = isTemplateModel ? model.get('template') : model;
3030
const template = templateModel.get('template');
31-
const vuefile = readVueFile(template);
31+
const sourceCodeFile = `VUE_TEMPLATE_SCRIPT_${model.cid}`;
32+
const vuefile = readVueFile(template, sourceCodeFile);
3233

3334
const css = model.get('css') || (vuefile.STYLE && vuefile.STYLE.content);
3435
const cssId = (vuefile.STYLE && vuefile.STYLE.id);
@@ -313,7 +314,7 @@ function aliasRefProps(model) {
313314
}), {});
314315
}
315316

316-
function readVueFile(fileContent) {
317+
function readVueFile(fileContent, sourceURL) {
317318
const component = parseComponent(fileContent, { pad: 'line' });
318319
const result = {};
319320

@@ -327,7 +328,15 @@ function readVueFile(fileContent) {
327328
const module = {
328329
exports: {}
329330
};
330-
const scriptFunction = new Function('module', 'exports', content);
331+
/*
332+
Add sourceURL directive - this helps browser dev tools show better error locations.
333+
But only add it if not already present in the content (users can add it themselves if they want).
334+
*/
335+
const hasSourceURL = /\/\/#\s*sourceURL\s*=/i.test(content);
336+
const contentWithScriptPath = hasSourceURL
337+
? content
338+
: content + `\n//# sourceURL=${sourceURL}`;
339+
const scriptFunction = new Function('module', 'exports', contentWithScriptPath);
331340
scriptFunction(module, module.exports);
332341
result.SCRIPT = module.exports;
333342
} catch (error) {

0 commit comments

Comments
 (0)