Skip to content

Commit

Permalink
Use magic-string to generate correct sourcemaps for colocated templates
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Nov 15, 2023
1 parent 60d499b commit 253bd70
Show file tree
Hide file tree
Showing 5 changed files with 15,187 additions and 51 deletions.
23 changes: 21 additions & 2 deletions lib/colocated-broccoli-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const walkSync = require('walk-sync');
const Plugin = require('broccoli-plugin');
const logger = require('heimdalljs-logger')('ember-cli-htmlbars:colocated-broccoli-plugin');
const FSTree = require('fs-tree-diff');
const MagicString = require('magic-string');

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

jsContents = prefix + jsContents;

let jsOutputPath = path.join(this.outputPath, backingClassPath);

if (prefix.length > 0) {
let jsContentsMagic = new MagicString(jsContents);

jsContentsMagic.prepend(prefix);

jsContents = jsContentsMagic.toString();

// It's unclear how to format the comment correctly for coffeescript.
if (!backingClassPath.endsWith('.coffee')) {
let jsContentsMap = jsContentsMagic.generateMap({
file: jsOutputPath,
source: backingClassPath,
includeContent: true,
hires: true,
});

jsContents += `\n//# sourceMappingURL=${jsContentsMap.toUrl()}`;
}
}

switch (method) {
case 'unlink': {
if (filePathParts.ext === '.hbs' && hasBackingClass) {
Expand Down
Loading

0 comments on commit 253bd70

Please sign in to comment.