This is a guide for addon authors who want to publish their addon in v2 format.
The actual V2 Format RFC only cares what format you publish to NPM. It doesn't necessarily care about your authoring format or toolchain. But in this guide, we are picking good defaults, and we hope to polish this experience until it's ready to become a new RFC as the default new Ember Addon authoring blueprint.
The best candidates to convert to V2 are addons that provide only run-time features, like components, helpers, modifiers, and services. That kind of addon should definitely port to V2.
In contrast, addons that are primarily an extension to the build system (like ember-cli-sass
or ember-cli-typescript
) are not good candidates to be V2 addons, at present. V1 addons will continue to work through @embroider/compat
for Embroider apps.
If your addon is a mix of both build-time and run-time features, consider replacing the build-time features with @embroider/macros
. This would let you drop all your custom build-time code and port to V2. Alternatively, if you really need build customizations, you can provide users with instructions and utilities (like a webpack rule or plugin) to add those customizations to their Embroider build. We do not let V2 addons automatically manipulate the app's build pipeline. Thar be dragons.
Traditionally, an Ember addon is a single NPM package that combines both the actual addon code and a "dummy" app for hosting tests and docs. This was problematic for several reasons. V2 addons instead require clean separation between addon and app, so you're going to be working with more than one distinct NPM package: one for the addon, one for the test-app, and optionally one for the documentation site.
Our recommended way to manage these multiple packages is using a monorepo, via Yarn, NPM, or PNPM Workspaces. The example in this guide assumes a Yarn Workspaces monorepo.
In this part of the guide, our goal is to separate our existing V1 addon from its "dummy" app and rename the dummy app to "test-app". At the end of this part, you will still have a V1 addon but it will be independent of its test-app, making it much easier to convert to V2 format in a subsequent Part.
For a complete example of a PR that performed these steps on a real addon, see ember-cli/ember-page-title#227.
The steps:
-
Delete yarn.lock.
-
At the top-level of your repo, make new directories named
test-app
and_addon
-
Move these files and directories into
_addon
- addon
- addon-test-support
- app
- blueprints
- config/environment.js (moves to
_addon/config/environment.js
) - index.js
-
Now you can rename
_addon
toaddon
without a name collision.- yes, this means you will have an
addon/addon
directory. This looks silly, but it will go away when we finish porting the addon to v2.
- yes, this means you will have an
-
These things stay at the top level:
- .git
- .github
- changelog, code of conduct, contributing, license, and readme
Move everything else into
test-app
-
Move everything under
test-app/tests/dummy
to directly undertest-app
instead.- for example,
test-app/tests/dummy/app
becomestest-app/app
- you will be merging config directories because both
test-app/config
andtest-app/tests/dummy/config
will exist at the start of this step. They shouldn't have any file collisions because you already moved the one colliding file (environment.js
) toaddon/config/environment.js
in a previous step.
- for example,
-
Make a new top-level package.json for our new monorepo:
{ "private": true, "workspaces": ["addon", "test-app"] }
-
Make a new top-level .gitignore:
# you definitely want this: node_modules # and you can put in anything else that tends to accumulate in your environment: yarn-error.log .DS_Store
-
Copy
test-app/package.json
toaddon/package.json
-
Edit
addon/package.json
to remove alldevDependencies
,scripts
, andember-addon.configPath
. -
Edit
test-app/package.json
. For each package independencies
, either remove it (if it's only used by the addon and not the test-app) or move it todevDependencies
(if it's actually used by the test-app).- For example,
"ember-cli-babel"
and"ember-cli-htmlbars"
most likely need to move todevDependencies
because test-app still needs JS and template transpilation.
- For example,
-
In
test-app/package.json
, add your addon as adevDependency
of the test-app by name and exact version. Our monorepo setup will see this and link our two packages together. For example, ifaddon/package.json
has this:"name": "ember-page-title", "version": "8.0.0",
Then you would add this to
test-app/package.json
:"devDependencies": { "ember-page-title": "8.0.0" }
-
In test-app/package.json
, change the top-level "name" to "test-app", remove the "ember-addon" section, and remove "ember-addon" from keywords. -
At the top-level of the project, run
yarn install
. -
In
test-app/ember-cli-build.js
switch from the dummy app build pipeline to the normal app build pipeline:-const EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); +const EmberApp = require('ember-cli/lib/broccoli/ember-app'); ... -let app = new EmberAddon(defaults, { +let app = new EmberApp(defaults, {
You may also find other places in
ember-cli-build.js
that refer to files undertests/dummy
. Update those paths to point directly to their new locations directly inside test-app instead. -
Search for all uses of the word "dummy" in the test-app. If they're referring to the app name, replace them with "test-app". This includes
modulePrefix
intest-app/config/environment.js
anddummy.js
anddummy.css
intest-app/app/index.html
and intest-app/tests/index.html
. -
Try to boot your test-app and run the tests. Debug as needed to get things passing again.
cd test-app ember s ember test
-
At this point all tests and lint scripts work the same way as before inside the test-app. But we will also want linting, prettier, etc for the newly-separated addon workspace too.
You could create one unified config at the top of the monorepo if you want, but I think it's simpler over the long run to manage each workspace separately. It's nice that the test-app is a totally stock Ember app that can be updated by ember-cli-update -- including all the default linting setup.
Copy .gitignore, .eslintrc.js, .eslintignore, .prettierrc.js, .prettierignore, and .template-lintrc.js from test-app to addon.
Edit them down so they only cover the thing the addon workspace has. For example, there's no dummy app or tests inside the addon workspace anymore, so the eslintrc will get simpler.
Copy eslint, relevant eslint plugins, prettier, ember-template-lint, and npm-run-all from
test-app/package.json
devDependencies
toaddon/package.json
devDependencies
.Copy the lint-related scripts from
test-app/package.json
toaddon/package.json
.Test that
yarn lint
works inside theaddon
workspace. -
Remove
test-app/config/ember-cli-update.json
because it still says you're using the addon blueprint and next time you run ember-cli-update intest-app
it uses the app blueprint instead. -
Edit
.github/workflows/ci.yml
to run tests in the right directory. For example:- name: Test run: yarn test:ember --launch ${{ matrix.browser }} + working-directory: test-app
And make separate linting steps for both workspaces:
- - name: Lint - run: yarn lint + - name: Lint Addon + run: yarn lint + working-directory: addon + - name: Lint Test App + run: yarn lint + working-directory: test-app
-
If you're using volta, move the volta config to the top-level package.json and make both workspaces say:
"volta": { "extends": "../package.json" }
At this point, you should still have a fully-working V1 Addon, and if you want you can test, review, and merge this work before moving on.
Many addons have a deployable documentation app. Usually it is the same app as the test suite.
This causes a lot of pain because the test suite needs to support every Ember version your addon supports, and when your docs site is mixed in with your test suite, your docs site also needs to support every Ember version, and that's unnecessarily difficult. Documentation apps deal with lots of typical production app concerns (deployment, styling, server-side rendering) that mean they benefit from using many additional addons, which makes broad version compatibility challenging.
The solution is to split the docs from the test suite. The docs app can pick a single Ember version, and it can stay on older, deprecated patterns as long as you like without impacting your ability to test your addon against the latest Ember canary. When you get test failures on Ember Canary, they will be real failures that impact your users, not irrelevant failures caused by forcing your docs app and all its dependencies to upgrade to Canary.
To split out the docs, you could start by just copying all of test-app
into a new docs
directory. Add your new docs
workspace to the top-level package.json. Then edit both apps down to eliminate documentation and deployment features from test-app
and eliminate test-suite concerns from docs
. It's still appropriate for docs
to have its own tests of course, to prove that the docs pages themselves render correctly.
When the docs app is ready, expand the CI settings to cover linting and testing of the docs app, just like we did when we expanded it to cover linting of both addon
and test-app
above.
For a complete example of a PR that splits docs from test-app, see ember-cli/ember-page-title#228.
In this part, we address potential blockers before we actually switch to V2. This lets you test your changes and make sure they're still working before we move on to V2 format.
-
Make sure your test-app (and docs app if you have one) has
ember-auto-import
>= 2. Once you convert your addon to v2 format, it can only be consumed by apps that have ember-auto-import >= 2. This also means you should plan to make a semver major release to communicate this new requirement to your users. -
Make sure all the files in the
addon/app
contain only reexport statements. If there's anything that's not a reexport statement, move that code into somewhere in theaddon/addon
directory and reexport it fromaddon/app
. This was already best practice, but we're about to enforce it. -
Make sure all the reexports in
addon/app
follow the default naming convention, such thataddon/app/components/whatever.js
contains only a reexport ofyour-addon-name/components/whatever
. If the names don't align, move files around insideaddon/addon
until they do. -
Make sure your
addon/index.js
file isn't trying to do anything "interesting". Ideally it contains nothing other than your addon's name.- if it was using
app.import()
orthis.import()
, port those usages toember-auto-import
instead - if you're trying to modify your own source code based on the presence of other packages or based on development vs testing vs production, switch to
@embroider/macros
instead - if you have other cases you're not sure what to do with, ask in an issue on this repo, or https://discuss.emberjs.com, or the #dev-embroider channel in the Ember community discord.
- if it was using
In this part we actually convert our addon from v1 to v2 format by reorganizing it and setting up a default toolchain for building and publishing it.
For an example of a complete PR that applies these steps to a real addon, see ember-cli/ember-page-title#229
Now that we've separated the test-app and docs app concerns from the addon, we can focus on reorganizing the addon itself to V2 format.
-
Rename the
addon/addon
directory toaddon/src
. -
If you have an
addon/addon-test-support
directory, move it toaddon/src/test-support
. -
In
addon/package.json
, remove any of these that appear independencies
:- ember-cli-htmlbars
- ember-cli-babel
- ember-auto-import
- @embroider/macros
All of these implement standard features of V2 addons that don't need to come as dependencies.
-
yarn add @embroider/addon-shim
. This is the only dependency a v2 addon needs (in order to interoperate with ember-cli. -
We're going to set up a default build pipeline for things like template colocation and decorator support. Install these dev dependencies:
yarn add --dev @embroider/addon-dev rollup @rollup/plugin-babel @babel/core @babel/plugin-transform-class-properties @babel/plugin-proposal-decorators
-
Grab the example babel config and save it as
addon/babel.config.json
- If you addon requires template transforms in order to publish to a shareable format. Apply transforms using the
babel-plugin-ember-template-compilation
. View how to use this in the example babel.config.js
- If you addon requires template transforms in order to publish to a shareable format. Apply transforms using the
-
Grab the example rollup config and save it as
addon/rollup.config.js
. -
Identify your app reexports. This is the list of modules from your addon that get reexported by files in the
addon/app
directory. -
Delete the
addon/app
directory. You aren't going to need it anymore. -
Edit
addon/rollup.config.js
. Customize thepublicEntrypoints
so it includes
- every module that users should be allowed to import from your addon
- every module in the app reexports you identified in the previous step
- Still editing
addon/rollup.config.js
, customize theappReexports
to match all your app reexports as identified above. - Delete your
addon/index.js
file. - Create a new
addon/addon-main.js
file (this replacesaddon/index.js
) with this exact content:
const { addonV1Shim } = require('@embroider/addon-shim');
module.exports = addonV1Shim(__dirname);
- In your
addon/.eslintrc.js
, replace "index.js" with "addon-main.js" so that our new file will lint correctly as Node code. - In your
addon/package.json
, add these things:"exports": { ".": "./dist/index.js", "./*": "./dist/*", "./test-support": "./dist/test-support/index.js", "./addon-main.js": "./addon-main.js" }, "files": [ "addon-main.js", "dist" ], "scripts": { "build": "rollup --config", "prepublishOnly": "rollup --config", "start": "rollup --config --watch" }, "ember-addon": { "main": "addon-main.js", "type": "addon", "version": 2 }
- In the
addon
directory, runyarn start
to start building the addon. - In a separate shell, you should be able to go into the
test-app
directory and runyarn start
oryarn test
and see your tests passing.
When all tests are passing, you have a fully-working V2 addon and you're ready to release it. To publish, you will run npm publish
in the addon
directory.