Détail du package

ember-asset-loader

ember-engines46.3kMIT1.0.0

Asset loading support for Ember applications

ember-addon

readme

Ember Asset Loader

Build Status Code Climate

Provides experimental support for the Asset Manifest RFC and Asset Loader Service RFC.

Compatibility

  • Ember.js v3.24 or above
  • Ember CLI v3.24 or above
  • Node.js v14 or above

Usage

Ember Asset Loader does three primary things:

  1. Provides a base class to easily generate an Asset Manifest,
  2. Provides an Ember service to use the generated manifest at runtime, and
  3. Initializes the above service with the above generated manifest.

Generating an Asset Manifest

You can generate an Asset Manifest by creating either a standalone or in-repo addon which extends from the ManifestGenerator base class:

var ManifestGenerator = require('ember-asset-loader/lib/manifest-generator');
module.exports = ManifestGenerator.extend({
  name: 'asset-generator-addon',
  manifestOptions: {
    bundlesLocation: 'engines-dist',
    supportedTypes: [ 'js', 'css' ]
  }
});

The ManifestGenerator will generate an asset manifest and merge it into your build tree during post-processing. It generates the manifest according to the options specified in manifestOptions:

  • The bundlesLocation option is a string that specifies which directory in the build tree contains the bundles to be placed into the asset manifest. This defaults to bundles. Each bundle is a directory containing files that will be downloaded when the bundle is requested. You are responsible for getting the right files into those directories.

  • The supportedTypes option is an array that specifies which types of files should be included into the bundles for the asset manifest. This defaults to [ 'js', 'css' ].

_Note: This class provides default contentFor, postprocessTree, and postBuild hooks so be sure that you call _super if you override one of those methods._

Why isn't a manifest generated by default?

This addon doesn't perform manifest generation just by virtue of being installed because there is no convention for bundling assets within Ember yet. Thus, to prevent introducing unintuitive or conflicting behavior, we provide no default generation and you must perform asset generation in your own addon using the base class provided by this addon.

If no manifest is generated, you'll get a warning at build time to ensure that you understand no manifest has been generated and thus you'll have to provide a manifest manually in order to use the Asset Loader Service. This warning can be disabled via the noManifest option from the consuming application:

var app = new EmberApp(defaults, {
  assetLoader: {
    noManifest: true
  }
});

Generating Custom URIs

Custom URIs are often needed due to serving assets from CDNs or another server that does not share the same root location as your application. Instead of having to write a custom Broccoli plugin or other build-time transform, you can specify a generateURI function as part of your application's options:

var app = new EmberApp(defaults, {
  assetLoader: {
    generateURI: function(filePath) {
      return 'http://cdn.io/' + filePath;
    }
  }
});

The function receives the filePath for each asset and must return a string.

Ignore Files

To ignore specific files during the manifest generation, use filesToIgnore. Both string and regex patterns are accepted.

var app = new EmberApp(defaults, {
  assetLoader: {
    filesToIgnore: [/foo-engine/**/engine-vendor.js$/, 'vendor.js']
  }
});

Using With broccoli-asset-rev

You need to make sure that broccoli-asset-rev runs after your ManifestGenerator addon runs. Here is an example of how to do that:

  1. Create an in-repo-addon: ember generate in-repo-addon asset-generator-addon

  2. Make it generate the manifest by editing lib/asset-generator-addon/index.js as described under "Generating an Asset Manifest" above.

  3. Edit lib/asset-generator-addon/package.json to configure the addon to run after broccoli-asset-rev

{
  "name": "asset-generator-addon",
  "keywords": [
    "ember-addon"
  ],
  "ember-addon": {
    "after": "broccoli-asset-rev"
  }
}

Usage with FastBoot / Server-Side Rendering Solutions

Using lazily loaded assets with a server-side rendering solution, such as FastBoot, is often desirable to maximize performance for your consumers. However, lazy loading assets on your server is not the same as on the client and can actually have negative performance impact. Due to that, the recommendation is to pre-load all your assets in the server.

Additionally, at build time we will generate an assets/node-asset-manifest.js file that should be included in your SSR environment to ensure that your application can correctly access asset information.

See the "How to handle running in Node" issue for more information.

Pre-loading Assets During Testing

For test environments it is often useful to load all of the assets in a manifest upfront. You can do this by using the preloadAssets helper, like so:

// tests/test-helper.js
import preloadAssets from 'ember-asset-loader/test-support/preload-assets';
import manifest from 'app/config/asset-manifest';

preloadAssets(manifest);

Resetting Test State

When testing applications with lazy assets, it is important to reset the state of those assets in between tests. To do this, Ember Asset Loader provides two helpers: cacheLoadedAssetState() and resetLoadedAssetState().

// tests/test-helper.js
import preloadAssets from 'ember-asset-loader/test-support/preload-assets';
import { cacheLoadedAssetState, resetLoadedAssetState } from 'ember-asset-loader/test-support/loaded-asset-state';
import manifest from 'app/config/asset-manifest';

cacheLoadedAssetState();
preloadAssets(manifest).then(() => {
  resetLoadedAssetState(); // Undoes the previous load!
});

It is important to note that resetLoadedAssetState can only remove additional scripts, stylesheets, and modules loaded since cacheLoadedAssetState was called. If any of the loaded assets modified global state, we'll be unable to restore that state. Therefore, it is important to keep your lazy assets encapsulated and make sure they don't modified any state already in the browser.

Note: If you use QUnit, it may be worthwhile to turn on the noglobals config option, to help catch mutated global state.

Installation

  • git clone https://github.com/ember-engines/ember-asset-loader
  • cd ember-asset-loader
  • npm install
  • bower install

Running

Running Tests

One of three options:

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit http://ember-cli.com/.

changelog

v1.0.0 (2022-05-09)

:boom: Breaking Change

  • #130 Drop Node < 14 (@rwjblue)
  • #104 Update addon to ember-cli@3.12 (@villander)
  • #92 Fix travis errors that are occurring due to ember-cli-sauce config; Bumped node version to 8.x+ and therefore bumped package version to 1.x (@evanfarina)

:rocket: Enhancement

  • #132 update dependencies (@rwjblue)
  • #127 Add support code for future ember-auto-import versions to be used within lazy Ember Engines (@thoov)

:bug: Bug Fix

:memo: Documentation

:house: Internal

Committers: 6

v0.6.1 (2019-06-21)

:bug: Bug Fix

Committers: 1

v0.6.0 (2019-06-10)

:memo: Documentation

  • #75 Removes needless Warning on resetLoadedAssetState (@villander)

Committers: 1

v0.5.1 (2019-03-11)

:bug: Bug Fix

Committers: 1

v0.5.0 (2019-03-11)

:boom: Breaking Change

Committers: 1

v0.4.4 (2019-03-11)

:rocket: Enhancement

  • #79 Add optional to configure additional files to ignore (@sangm)

:bug: Bug Fix

:house: Internal

Committers: 4

v0.4.2 / 2018-03-23

  • Fix issues with scoped bundles (e.g. engines within a scope).

v0.4.1 / 2017-10-06

  • Fix issue causing every rebuild to force a full live reload (even if only CSS has changed).

v0.4.0 / 2017-09-17

  • Support ignoring files by regex
  • Add option to ignore specific files from being added to the manifest

v0.3.1 / 2017-09-07

  • Prevent caching failed assets (#57)
  • Add basic polling (1000ms timeout) to absorb raciness.

v0.3.0 / 2017-08-09

  • Exclude empty files from generated asset-manifest.json.
  • Refactor tests to use broccoli-test-helper API.
  • Add node-test/.eslintrc.js to reduce linting warnings in node-tests/.
  • Avoid issues when the consuming app is using babel 6.
  • Roll ember-cli-sauce back to 1.6.0.
  • Make it possible to extend from Error in Babel 6.
  • Refactor from require to requirejs usage.
  • Update dependency versions to latest.
  • fixup! Update addon blueprint to ember-cli@2.14.
  • Update repository to point to ember-engines/ember-asset-loader.
  • Update addon blueprint to ember-cli@2.14.
  • Post-review README tweaks
  • Add README details about re: broccoli-asset-rev

v0.2.7 / 2017-03-20

  • Call _super before touching this.
  • [docs] Tweak retryLoad description
  • [docs] Add missing param to loadBundle
  • Update links in README
  • Update Travis CI Badge
  • Node 0.12 compat fixes for lib/meta-handler.js.

v0.2.6 / 2016-11-30

  • Sets async to false when loading scripts.
  • Add test helpers to cache and restore browser state

v0.2.5 / 2016-11-11

  • Allow manifest to be transformed based on app or test index

v0.2.4 / 2016-10-30

  • Bump broccoli-funnel to fix rebuild issues.
  • Allow empty bundle directories to avoid issues on rebuild

v0.2.3 / 2016-10-25

  • Refactor meta replacement to allow for customizability

v0.2.2 / 2016-10-20

  • Add generateURI option for customizing URIs

v0.2.1 / 2016-10-19

  • Fix SauceLabs configuration
  • Check DOM for pre-loaded assets

v0.2.0 / 2016-09-21

  • Move asset-manifest modules into config namespace
  • Enable the asset-manifest to be present in SSR environments
  • Have loaders return resolved Promises in non-browser environments

v0.1.5 / 2016-09-13

  • Update ember-cli-qunit to avoid issue with jQuery 3.x.
  • Avoid using Ember's internal symbol utility.

v0.1.4 / 2016-09-02

  • Change Ember to Beta instead of Canary
  • Use findHost to find app

v0.1.3 / 2016-08-30

  • Add tests for manifest-generator
  • Don't break at runtime when not generating a manifest
  • Don't generate asset-manifest.json when using noManifest option
  • Fix potential error in postprocessTree

v0.1.2 / 2016-08-29

  • Make insertion happen as a broccoli plugin.
  • Rework environment variables

v0.1.1 / 2016-08-25

  • Introduce preload-assets helper for testing

v0.1.0 / 2016-08-01

  • Prepare initial release (v0.1.0)
  • Improve documentation and break out default loaders
  • Improve SauceLabs behavior
  • Setup SauceLabs to test against IE
  • Test in Phantom 1.9 and 2.1
  • Improve cross-browser support for default loaders
  • Test against Canary only
  • Create ManifestGenerator base class
  • fixup! Revamp asset manifest generation
  • Add test addon to fix failing test after revamp
  • Revamp asset manifest generation
  • Update tests to verify initialization of asset manifest
  • Insert manifest into index.html for runtime access
  • Run Node tests in Travis
  • Stop building pushes to non-master branches
  • Add tests for asset manifest generation
  • Generate asset manifest during postprocessTree
  • Add basic Acceptance test for loading assets during getHandler
  • Fix CodeClimate linting issues
  • Improve README
  • Initial implementation
  • Finish initial configuration
  • Initial Commit from Ember CLI v2.7.0-beta.6