diff --git a/ember-addon-main.js b/ember-addon-main.js index 5778242..287df0b 100644 --- a/ember-addon-main.js +++ b/ember-addon-main.js @@ -5,6 +5,23 @@ let VersionChecker = require('ember-cli-version-checker'); module.exports = { name: require('./package').name, + _getBabelOptions() { + const parentOptions = this.parent && this.parent.options; + const appOptions = this.app && this.app.options; + const addonOptions = parentOptions || appOptions || {}; + + addonOptions.babel = addonOptions.babel || {}; + addonOptions.babel.plugins = addonOptions.babel.plugins || []; + return addonOptions.babel; + }, + + addBabelPlugin() { + this._getBabelOptions().plugins.push([ + require.resolve('./src/babel-plugin.js'), + { v: 1 }, + ]); + }, + included() { this._super.included.apply(this, arguments); @@ -33,6 +50,8 @@ module.exports = { 'ember-template-imports requires' + '\n\t' + errors.join('\n\t'), ); } + + this.addBabelPlugin(); }, setupPreprocessorRegistry(type, registry) { diff --git a/src/babel-plugin.js b/src/babel-plugin.js new file mode 100644 index 0000000..7b1ee9f --- /dev/null +++ b/src/babel-plugin.js @@ -0,0 +1,37 @@ +const { basename, extname } = require('path'); + +module.exports = function hotReplaceAst( + { types: t }) { + + return { + name: 'add-template-only-names-for-inspector', + visitor: { + CallExpression(path, state) { + let calleePath = path.get('callee'); + + if (!calleePath.isIdentifier()) { + return; + } + + if (calleePath.referencesImport('@ember/component/template-only', 'default')) { + let params = path.node.arguments; + let assignment = path.parentPath.parentPath.node; + let rootName = basename(state.filename).slice(0, -extname(state.filename).length); + let assignmentName = t.identifier('undefined'); + if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') { + assignmentName = t.stringLiteral(rootName + ':' + assignment.left.name); + } + if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') { + assignmentName = t.stringLiteral(rootName + ':' + assignment.id.name); + } + if (assignment.type === 'ExportDefaultDeclaration') { + assignmentName = t.stringLiteral(rootName); + } + + params.length = 0; + params.push(t.identifier('undefined'), assignmentName); + } + } + } + } +} diff --git a/tests/integration/gjs-test.gjs b/tests/integration/gjs-test.gjs index 1def306..245788c 100644 --- a/tests/integration/gjs-test.gjs +++ b/tests/integration/gjs-test.gjs @@ -21,6 +21,7 @@ module('tests/integration/components/gjs', function (hooks) { ); assert.equal(this.element.textContent.trim(), 'Hello, world!'); + assert.equal(Foo.name, 'gjs-test:Foo'); }); test('it works with imports', async function (assert) {