npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-cli-embedded

v4.1.1

Published

Control how your Ember application boots in non-Ember pages/apps.

Downloads

257

Readme

ember-cli-embedded

CI Ember Observer Score License: MIT

Makes it easier to embed your Ember application in another (non-Ember) app.

This addon gives you more control over how and when your Ember app will boot and also allows how to add/override some configuration so that the Ember app can boot with some context-dependent config.

We found it especially useful, for example, when migrating an existing app to Ember part by part.

Compatibility

  • Ember.js v4.8 or above
  • Ember CLI v4.8 or above
  • Node.js v18 or above

Installation

ember install ember-cli-embedded

Usage

Configuration

This plugin is opt-in by default, it does nothing to your app unless you configure it.

In your config/environment.js, add the following config to the ENV:

let ENV = {
  ...,
  modulePrefix: 'my-app-name',

  embedded: {
    delegateStart: true,
    config: { // optional
      // Default values for the config passed at boot
    },
  },

  /*
  * 1. If this key is undefined or set to `true`, you will have to start your app with `MyAppName.start(...)`
  * 2. If this key is set to `SomeOtherAppName`, you will have to start your app with `SomeOtherAppName.start(...)`
  * 3. If this key is set to `false`, you will NOT be able to start your app with `.start(...)` at all
  */
  exportApplicationGlobal: 'SomeOtherAppName'
}

Doing so will make your application hold until you manually start it. (read on to learn more)

For compatibility reasons, this behaviour will work as long as the value of embedded is truthy but we plan to remove it in a future version.
Please stick to the config format above.

Start your app

In your JS code, execute MyAppName.start(/* optionalConfig */) to resume the boot of your application. As per the example, it takes an optional configuration as its first argument.

Attention :warning:

  1. Your app will not start unless you call MyAppName.start(/* optionalConfig */) method.
  2. Calling MyAppName.start(...) will not work if you've set exportApplicationGlobal: false in your config/environment.js

Access the config from your application

Via the Service embedded

Consider the following config/environment.js file:

let ENV = {
  ...,
  modulePrefix: 'my-app',
  embedded: {
    config: {
      option1: 'value-1',
    },
  },
  ...
}

And the application is started that way:

<script>
  MyApp.start({ option2: "value-2" });
</script>

Then in your Services, Components, Controllers...

class MyService extends Service {
  @service embedded;

  @action
  logSomeConfigKey() {
    // Will log 'value-1'
    console.log(this.embedded.option1);
  }
}

Via the container itself

Sometimes it can be more convenient to access the data directly from the container.

Following the previous example:

import { getOwner } from '@ember/application';

...

// Returns the raw config
let embeddedConfig = getOwner(this).lookup('config:embedded');

// Will log 'value-2'
console.log(embeddedConfig.option2);

Override your APP configuration

The startup object will be merged into your APP configuration key, which is very useful, for instance, if you want to change the rootElement of your application and other context-sensitive values.

Consider the following config/environment.js file:

let ENV = {
  APP: {
    rootElement: `#some-element`,
  },

  modulePrefix: "my-app",

  embedded: {
    config: {
      option1: "value-1",
    },
  },
};

And the application is started that way:

<script>
  MyApp.start({ option2: "value-2" });
</script>

This would result in:

import APP_ENV_CONFIG from "my-app/config/environment";

assert.deepEqual(APP_ENV_CONFIG, {
  APP: {
    option2: "value-2",
    rootElement: `#some-element`,
  },

  embedded: {
    config: {
      option1: "value-1",
    },
  },
});

About the test environment

In your test suite, you will probably want to let your application start automatically without this addon interfering.

To do that, make sure to disable the addon for the test environment:

// file `config/environment.js`

if (environment === "test") {
  ENV.embedded.delegateStart = false;
}

TypeScript support

If your consuming application relies on TypeScript, you can make your life a bit easier by using the included types:

File /types/my-project/index.d.ts

import type BaseEmbeddedService from "ember-cli-embedded/services/embedded";

declare global {
  type EmbeddedService = BaseEmbeddedService<{
    one: string;
    two?: string;
  }>;
}

export {};

File /app/components/my-component.ts

import Component from "@glimmer/component";
import { service } from "@ember/service";

export default class MyComponent extends Component {
  /**
   * To know why the modifier `declare` should be used when injecting a Service:
   * https://github.com/typed-ember/ember-cli-typescript/issues/1406#issuecomment-778660505
   */
  @service
  declare embedded: EmbeddedService;

  get one() {
    // Return type inferred: `string | undefined`
    return this.embedded.get("one");
  }

  get two() {
    // TypeScript returns an error as `twoo` is not a recognised key
    return this.embedded.get("twoo");
  }
}

Contributing

See the Contributing guide for details.

Contributors

License

This project is licensed under the MIT License.