Skip to content

Commit 253bd70

Browse files
committed
Use magic-string to generate correct sourcemaps for colocated templates
1 parent 60d499b commit 253bd70

File tree

5 files changed

+15187
-51
lines changed

5 files changed

+15187
-51
lines changed

lib/colocated-broccoli-plugin.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const walkSync = require('walk-sync');
66
const Plugin = require('broccoli-plugin');
77
const logger = require('heimdalljs-logger')('ember-cli-htmlbars:colocated-broccoli-plugin');
88
const FSTree = require('fs-tree-diff');
9+
const MagicString = require('magic-string');
910

1011
module.exports = class ColocatedTemplateProcessor extends Plugin {
1112
constructor(tree) {
@@ -165,10 +166,28 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
165166
jsContents = `import templateOnly from '@ember/component/template-only';\n\nexport default templateOnly();\n`;
166167
}
167168

168-
jsContents = prefix + jsContents;
169-
170169
let jsOutputPath = path.join(this.outputPath, backingClassPath);
171170

171+
if (prefix.length > 0) {
172+
let jsContentsMagic = new MagicString(jsContents);
173+
174+
jsContentsMagic.prepend(prefix);
175+
176+
jsContents = jsContentsMagic.toString();
177+
178+
// It's unclear how to format the comment correctly for coffeescript.
179+
if (!backingClassPath.endsWith('.coffee')) {
180+
let jsContentsMap = jsContentsMagic.generateMap({
181+
file: jsOutputPath,
182+
source: backingClassPath,
183+
includeContent: true,
184+
hires: true,
185+
});
186+
187+
jsContents += `\n//# sourceMappingURL=${jsContentsMap.toUrl()}`;
188+
}
189+
}
190+
172191
switch (method) {
173192
case 'unlink': {
174193
if (filePathParts.ext === '.hbs' && hasBackingClass) {

0 commit comments

Comments
 (0)