Coder Social home page Coder Social logo

nullvoxpopuli / ember-cli-ifa Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adopted-ember-addons/ember-cli-ifa

1.0 2.0 0.0 192 KB

Ember CLI addon for injecting fingerprinted asset map file into Ember app

License: MIT License

JavaScript 85.54% HTML 14.46%

ember-cli-ifa's Introduction

Build Status Code Climate

Ember-cli-ifa

Inject fingerprinted assetMap.json file into your app and provide initializer, service, and helper to dynamically reference fingerprinted assets.

When to use this addon?

  1. If you have dynamic asset file names returned from API and/or you build paths based on several properties.
  2. If you can't put your asset filemames into css or to have path as static in your .js files.
  3. If you build your image/asset paths in a dynamic way, eg.
imagePath: computed(function() {
  return this.get('assetMap').resolve(`${this.get('image')}.png`);
})

Installation

ember install ember-cli-ifa

Configuration

Enable addon in environment.js for specific environment.

module.exports = function(environment) {
  var ENV = {
    ...
    ifa: {
      enabled: true,
      inline: false,
    }
    ...
  };

In case you use s3 and manifest module for ember-cli-deploy, update there configurations in config/deploy.js to include json as a valid file.

module.exports = function(environment) {
  var ENV = {
    ...
    s3: {
      filePattern: function(context, pluginHelper) {
        let filePattern = pluginHelper.readConfigDefault('filePattern');
        return filePattern.replace('}', ',json}');
      },
      ...
    },
    manifest: {
      filePattern: function(context, pluginHelper) {
        let filePattern = pluginHelper.readConfigDefault('filePattern');
        return filePattern.replace('}', ',json}');
      },
      ...
    },
    ...
  };

Configure fingerprinting in ember-cli-build.js. Refer to the documentation of ember-cli for asset-compilation

fingerprint: {
  enabled: true, // set to true only in required environments
  generateAssetMap: true,
  fingerprintAssetMap: true
}

Note that if you use fastboot, this addon is automatically forced into inline: true mode. This is necessary, as otherwise fastboot could not easily access that data.

Usage

asset-map helper

If name is tomster-under-construction:

<img src={{asset-map (concat "assets/" name ".png")}} />

then it will generate something like assets/tomster-under-construction-da524c8bc9283f759ae640b68db81f24.png base on assetMap.json.

asset-map service

import Component from 'ember-component';
import service from 'ember-service/inject';

export default Component.extend({
  assetMap: service('asset-map'),

  key: null, // key passed as 'tomster-under-construction'

  // result will be assets/tomster-under-construction-da524c8bc9283f759ae640b68db81f24.png
  image: computed('key', function() {
    return this.get('assetMap').resolve(`assets/${this.get('key')}.png`);
  })
});

Storing assets in a sub-directory

If prepend option is added in fingerprint configuration block, it will be prepended into generated asset path in the index.html.

// ember-cli-build.js
// ...
var app = new EmberApp(defaults, {
  fingerprint: {
    prepend: '/blog/'
  }
});

/blog will be prepended to the assetMap file path in the index.html.

inline option

If inline: true is specified in config, contents of assetMap file will be inline into index.html.

<script>
...

var __assetMapPlaceholder__ = {
  "assets": {
    "assets/assetMap.json": "assets/assetMap-0a0447ba419421fa257963a718324fa8.json",
    "assets/failed.png": "assets/failed-836936cf32381ff14d191d7b10be9a89.png",
    "assets/passed.png": "assets/passed-b8506cbc195c8b9db541745aee267c48.png",
    "assets/tomster-under-construction.png": "assets/tomster-under-construction-da524c8bc9283f759ae640b68db81f24.png"
  },
  "prepend": ""
};
</script>

...

This might save one request to assetMap.json, but will increase overall size of index.html file, so use carefully.

ember-cli-ifa's People

Contributors

bnitobzh avatar drewtempelmeyer avatar jrjohnson avatar nullvoxpopuli avatar ruslanzavacky avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.