myartsev
myartsev

Reputation: 1232

Why does an ember addon using a bower dependency needs to override normalizeEntityName?

You are developing an ember add-on that internally needs a bower package. When you install this add-on in an ember project, you need the bower package to be added to the project's bower dependencies so that your add-on can run.

Ok - easy enough, we just need a default blueprint and a call to afterInstall

module.exports = {
  normalizeEntityName: function() {}, // no-op since we're just adding dependencies

  afterInstall: function() {
    return this.addBowerPackageToProject('a-bower-package');
  }
};

Why is the override to normalizeEntityName necessary? At first I had missed adding this to my blueprint, and spent an ungodly amount of time trying to pinpoint why it was not installing the bower dependency as I was expecting.

I haven't seen a satisfactory answer anywhere, the ember-cli guide explanation doesn't address this and the closest resource I have found was this article, where all it says about the subject is this:

The hero we all need, even if the reason we deserve it can be a bit opaque, normalizeEntityName needs to be overridden as a noop inside of the default blueprint in order for ember g your-addon-name to even work. Just set this as the first property of your exported object, and forget all about it (but never forget it in the future 😀):

So... why do we need it?

Upvotes: 2

Views: 103

Answers (1)

ykaragol
ykaragol

Reputation: 6221

I'll try to explain :) Please do comment for unclear parts.

ember-cli has some commands such as: build, serve, generate, install...

To generate some code from blueprints, we use generate command. The generate command is commonly used for generating something from blueprints. Such as ember g component my-component or ember g route my-route etc...

In this convention, the generate command needs a second parameter as name. The blueprints may use this parameter in their code templates. (code templates does not mean hbs files, does mean every kind of source that will be copied in target app. As mentioned in here)

So the blueprint class checks it whether a name is provided or not in the command line. If you don't provide an entity name in command line, it will throw an exception.

To pass this check, your addon's users need to pass a dummy parameter to your addon, such as:

ember g my-addon dummy-name

To prevent this meaningless and dummy parameter, you need to override the default behaviour. This is done by adding normalizeEntityName hook.

Of course, you can jump in to code of ember-cli and change the behaviour of the blueprint by not throwing an error when no code templates are provided by blueprint. But this give a lot of complexity.

And at the last, here is the some code pieces from ember-cli:

  1. The generate command uses the generateFromBlueprint task to generate code.
  2. blueprint class.
  3. The check for entityname is undefined or not: ember-cli-normalize-entity-name

Upvotes: 2

Related Questions