vault/ui/docs/ember-engines.md
claire bontempo ea292e8142
Ui: Add contributing pattern doc (#19897)
* format readme to prepare for pattern info

* small text changes

* add markdown files for each section

* readme updates

* routing md draft

* add table of contents

* add oidc pr sample

* update routing

* add decorator section

* serializer docs

* add table of contents

* update readme

* add title

* add decorator section

* models readme

* update comments

* modify examples

* add bullets and more comments

* what the heck fix bullet

* model docs

* form docs

* routing doc

* serializer/adapter

* adds docs for model-validations decorator (#20596)

* UI Docs: Components (#20602)

* Add CSS best practices (#20370)

* wip--saving work

* wip

* friday morning....

* update

* fix exists to exist

* one more change

* UI docs: Add ember engine creation documentation (#20789)

---------

Co-authored-by: Jordan Reimer <zofskeez@gmail.com>
Co-authored-by: Chelsea Shaw <82459713+hashishaw@users.noreply.github.com>
Co-authored-by: Angel Garbarino <Monkeychip@users.noreply.github.com>
Co-authored-by: Kianna <30884335+kiannaquach@users.noreply.github.com>
2023-05-30 10:24:35 -07:00

176 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [Ember Engines](https://ember-engines.com/docs)
This is a quickstart guide inspired by [ember engine quickstart](https://ember-engines.com/docs/quickstart) on how to set up an ember engine in Vault!
## Create a new in-repo engine:
`ember g in-repo-engine <engine-name>`
_This blueprint in-repo engine command will add a new folder `lib/<engine-name>` and add the engine to our main apps `package.json`_
## Engines package.json:
```json
{
"name": "<engine-name>",
"dependencies": {
"ember-cli-htmlbars": "*",
"ember-cli-babel": "*"
},
"ember-addon": {
"paths": ["../core"]
}
}
```
For our application, we want to include the **ember-addon** path `../core`
By adding this **ember-addon** path, we are able to share elements between your in-repo addon and the Vault application[^1].
## Configure your Engine
In the engines `index.js` file:
```js
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
/* eslint-disable node/no-extraneous-require */
const { buildEngine } = require('ember-engines/lib/engine-addon');
module.exports = buildEngine({
name: '<engine-name>',
lazyLoading: {
enabled: false,
},
isDevelopingAddon() {
return true;
},
});
```
Within your Engines `config/environment.js` file:
```js
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
// config/environment.js
'use strict';
module.exports = function (environment) {
const ENV = {
modulePrefix: '<engine-name>',
environment: environment,
};
return ENV;
};
```
Within your Engines `addon/engine.js` file:
```js
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Engine from '@ember/engine';
import loadInitializers from 'ember-load-initializers';
import Resolver from 'ember-resolver';
import config from './config/environment';
const { modulePrefix } = config;
export default class <EngineName>Engine extends Engine {
modulePrefix = modulePrefix;
Resolver = Resolver;
dependencies = {
services: ['router', 'store', 'secret-mount-path', 'flash-messages'],
externalRoutes: ['secrets'],
};
}
loadInitializers(<EngineName>Engine, modulePrefix);
```
### Service Dependencies:
The services in the example above are common services that we often use in our engines. If your engine requires other services from the main application, add them to the services array.
#### Notes:
- Service dependencies are OPTIONAL. If your engine does not use any external services, you do not need to include a services dependency array.
- Remember to include any dependencies here in the engine's dependencies in app/app.js
### External Route Dependencies:
The external route dependencies allow you to link to a route outside of your engine. In this example, we list 'secrets' in the externalRoute and the route is defined in the `app.js` file.
#### Notes:
- In order to link to the other routes in the main app using the `LinkToExternal` component from your engine, you need to add the route to the `app/app.js` and your engines `addon/engine.js`. More information on [Linking to An External Context.](https://ember-engines.com/docs/link-to-external).
## Additional info about your engine's `application.hbs`:
- Optional step: Add some text in the engines `application.hbs` file (to see if your engine was set up correctly).
- **NOTE: Most of our existing engines do not keep the generated `application.hbs` template file. If nothing will be added to it and it remains as just an `{{outlet}}` it can safely be removed.**
## Register your engine with our main application:
In our `app/app.js` file in the engines object, add your engines name and dependencies.
```js
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Application from '@ember/application';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'vault/config/environment';
export default class App extends Application {
...
engines = {
<engine-name>: {
dependencies: {
services: ['router', 'store', 'secret-mount-path', 'flash-messages', <any-other-dependencies-you-have>],
externalRoutes: {
secrets: 'vault.cluster.secrets.backends',
},
},
},
};
}
loadInitializers(App, config.modulePrefix);
```
If you used `ember g in-repo-engine <engine-name>` to generate the engines blueprint, it should have added `this.mount(<engine-name>)` to the main apps `router.js` file (this adds your engine and its associated routes). \*Move `this.mount(<engine-name>)` to match your engines route structure. For more information about [Routable Engines](https://ember-engines.com/docs/quickstart#routable-engines).
### Important Notes:
- Anytime a new engine is created, you will need to `yarn install` and **RESTART** ember server!
- To add `package.json` **dependencies** or **devDependencies**, you can copy + paste the dependency into corresponding sections. Most of the time, we will want to use "\*" in place of the version number to ensure all the dependencies have the latest version.
### Common blueprint commands:
- **Generating In-repo engines routes:** `ember generate route <route-name> --in-repo <in-repo-name>` - _generates tests and route files and templates_
- **Remove In-repo engines routes:** `ember destroy route <route-name> --in-repo <in-repo-name>` - _removes tests and route files and templates_
- **Generating In-repo engines components:** `ember generate component <component-name> --in-repo <in-repo-name>`- _generates tests and component files and templates_
- **Remove In-repo engines components:** `ember destroy component <component-name> --in-repo <in-repo-name>`- _removes tests and component files and templates_
[^1]: https://ember-engines.com/docs/quickstart#create-as-in-repo-engine