Skip to content

Commit

Permalink
Merge pull request #332 from ember-cli/feat/add-use-ember-module-flag
Browse files Browse the repository at this point in the history
Adds `useEmberModule` option
  • Loading branch information
Chris Garrett committed Feb 19, 2021
2 parents 7d072dd + c35ba4d commit 14e3488
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
59 changes: 59 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,63 @@ describe('htmlbars-inline-precompile', function () {
expect(transformed).toContain(`hello {{firstName}}`);
});
});

describe('with Ember imports', function () {
it('adds an Ember import if useEmberModule is set to true', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},

useEmberModule: true,
},
],
];

let transpiled = transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);

expect(transpiled).toMatchInlineSnapshot(`
"import _Ember from \\"ember\\";
var compiled = _Ember.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});

it('Uses existing Ember import if one exists', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},

useEmberModule: true,
},
],
];

let transpiled = transform(
"import Foo from 'ember';\nimport hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"
);

expect(transpiled).toMatchInlineSnapshot(`
"import Foo from 'ember';
var compiled = Foo.HTMLBars.template(
/*
hello
*/
\\"precompiled(hello)\\");"
`);
});
});
});
52 changes: 48 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = function (babel) {
return result;
}

function compileTemplate(precompile, template, _options) {
function compileTemplate(precompile, template, emberIdentifier, _options) {
let options = Object.assign({ contents: template }, _options);

let precompileResultString;
Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = function (babel) {

return t.callExpression(
t.memberExpression(
t.memberExpression(t.identifier('Ember'), t.identifier('HTMLBars')),
t.memberExpression(emberIdentifier, t.identifier('HTMLBars')),
t.identifier('template')
),
[templateExpression]
Expand All @@ -87,6 +87,44 @@ module.exports = function (babel) {

return {
visitor: {
Program(path, state) {
let options = state.opts || {};
let useEmberModule = Boolean(options.useEmberModule);

let preexistingEmberImportDeclaration = path
.get('body')
.filter((n) => n.type === 'ImportDeclaration')
.find((n) => n.get('source').get('value').node === 'ember');

if (
// an import was found
preexistingEmberImportDeclaration &&
// this accounts for `import from 'ember'` without a local identifier
preexistingEmberImportDeclaration.node.specifiers.length > 0
) {
state.emberIdentifier = preexistingEmberImportDeclaration.node.specifiers[0].local;
}

state.ensureEmberImport = () => {
if (!useEmberModule) {
// ensures that we can always assume `state.emberIdentifier` is set
state.emberIdentifier = t.identifier('Ember');
return;
}

if (state.emberIdentifier) return;

state.emberIdentifier = path.scope.generateUidIdentifier('Ember');

let emberImport = t.importDeclaration(
[t.importDefaultSpecifier(state.emberIdentifier)],
t.stringLiteral('ember')
);

path.unshiftContainer('body', emberImport);
};
},

ImportDeclaration(path, state) {
let node = path.node;

Expand Down Expand Up @@ -154,7 +192,11 @@ module.exports = function (babel) {

let { precompile, isProduction } = state.opts;

path.replaceWith(compileTemplate(precompile, template, { isProduction }));
state.ensureEmberImport();

path.replaceWith(
compileTemplate(precompile, template, state.emberIdentifier, { isProduction })
);
},

CallExpression(path, state) {
Expand Down Expand Up @@ -222,7 +264,9 @@ module.exports = function (babel) {
options.isProduction = isProduction;
}

path.replaceWith(compileTemplate(precompile, template, options));
state.ensureEmberImport();

path.replaceWith(compileTemplate(precompile, template, state.emberIdentifier, options));
},
},
};
Expand Down

0 comments on commit 14e3488

Please sign in to comment.