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-resolve-asset

v0.3.0

Published

Imperatively resolves assets fingerprinted by `broccoli-asset-rev`. Allows resolving interpolated paths.

Downloads

566

Readme

ember-cli-resolve-asset

Build Status npm version Download Total Ember Observer Score
code style: prettier dependencies devDependencies

Imperatively resolves assets fingerprinted by broccoli-asset-rev, which allows you to even resolve interpolated paths.

Installation

ember install ember-cli-resolve-asset

If you want to use this in one of your addons, the consuming host application will also need to install ember-cli-resolve-asset. If this not a case, a helpful build error will be shown.

Setup

The following configuration in your ember-cli-build.js is required for this addon to work correctly.

const app = new EmberAddon(defaults, {
  fingerprint: {
    enabled: true, // If false, this addon is disabled also.
    generateAssetMap: true, // Required.
    fingerprintAssetMap: true // Recommended to prevent caching issues.
  },

  'ember-fetch': {
    preferNative: true // Recommended to enable faster preloading for browsers that support it.
  }
});

Usage

Utils

resolveAsset

async resolveAsset(path: string, withoutPrepend = false): Promise<string>

Asynchronously resolves the given path from the asset map.

If the asset map is not loaded yet, this function will wait for it. If loading the asset map fails, the returned Promise will reject. If the path is not listed in the asset map, the returned Promise will reject.

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import fetch from 'fetch';
import resolveAsset from 'ember-cli-resolve-asset';

export default class ApplicationRoute extends Route {
  @service intl;
  @service language;

  async beforeModel() {
    const preferredLanguage = this.language.getPreferredLanguage();
    const translationsPath = await resolveAsset(
      `translations/${preferredLanguage}.json`
    );
    const translations = await fetch(translationsPath);

    this.intl.addTranslations(preferredLanguage, await translations.json());
    this.intl.setLocale(preferredLanguage);
  }
}

resolveAssetSync

resolveAssetSync(path: string, withoutPrepend = false): string

Synchronous version of resolveAsset.

Synchronously resolves the given path from the asset map.

If the asset map is not loaded yet, this function will throw. If the path is not listed in the asset map, this function will throw.

Usage of this function is discouraged in favor of resolveAsset. Only use this function, if using the async version is not feasible and if you can guarantee, that load ran to completion beforehand.

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import fetch from 'fetch';
import {
  resolveAssetSync,
  load as loadAssetMap
} from 'ember-cli-resolve-asset';

export default class ApplicationRoute extends Route {
  @service intl;
  @service language;

  async beforeModel() {
    await loadAssetMap();

    const preferredLanguage = this.language.getPreferredLanguage();
    const translationsPath = resolveAssetSync(
      `translations/${preferredLanguage}.json`
    );
    const translations = await fetch(translationsPath);

    this.intl.addTranslations(preferredLanguage, await translations.json());
    this.intl.setLocale(preferredLanguage);
  }
}

load

async load(): Promise<void>

This function returns a Promise that resolves once the asset map was loaded successfully. Repeatedly calling this function returns the same Promise. After this Promise has resolved, you can use resolveAssetSync.

This function is called automatically by an initializer, to start the loading of the asset map as soon as possible. This means there is no direct need for you to call this function yourself, unless you want to await the asset map explicitly.

Related Projects

  • broccoli-asset-rev: The ember-cli addon that performs the fingerprinting and generates the asset map.
  • ember-fetch: Used by this addon to asynchronously load the asset map.
  • ember-cli-ifa: The original inspiration for this addon. I was dissatisfied with the technical implementation, the bugs it caused and current state of maintenance, which is why I created this addon instead.